I want to pass array value directly into the js fucntion which in loop and wanted to retrieve its query string id of selected
employee here is code

as i said in loop i wanted to pass array value

                    while($row = mysql_fetch_array($res)){                       
                    echo "<tr>".
                    "<td> <p><button onclick=func({$row[0]}) >View</button></p>  </td>" 
                        ."</tr>";

Here is js fucntion

<script>
function func()
 {
     var jsvar = <?php echo json_encode($row[0]); ?>;
    window.open("window.php"+jsvar,'Eployee Details','width=545,height=326,resizable=yes,scrollbars=yes,status=yes');

}
</script>

Recommended Answers

All 5 Replies

Your JS function needs to accept a parameter, and the PHP ouputs said parameter...

while($row = mysql_fetch_array($res)){                       
echo "<tr>".
"<td> <p><button onclick=func({$row[0]}) >View</button></p>  </td>" 
    ."</tr>";

is technically "fine" (assuming $row[0] actually prints something), though I do not think you should have a <button> tag inside a <p> tag. You may want to replace the paragraph tag with a <div> instead, but I don't know your markup or your particular needs.

however, your script should be something like...

<script>
function func(foo)
 {
    window.open("window.php?myParam="+foo,'Eployee Details','width=545,height=326,resizable=yes,scrollbars=yes,status=yes');
 }
</script>

and then window.php should accept a GET paremeter of myParam (or whatever you want to name it).

I want to ask if you are passing only one specific value?

If YES

As ryantroop was saying your js function must accept a parameter or an argument.

Your PHP codes will be

while ($row = mysql_fetch_array($res)) {                       
    echo "<tr>".
    "<td> <p><button onClick=\"func({$row[0]})\" >View</button></p>  </td>" 
    ."</tr>";
}

Then in your JS

<script>
    function func(param = <?php echo json_encode($row[0]); ?>) {
         var jsvar = param;
        window.open("window.php"+jsvar,'Eployee Details','width=545,height=326,resizable=yes,scrollbars=yes,status=yes');
    }
</script>

Hope it went well

function func(param = <?php echo json_encode($row[0]); ?>) {

This is most definately a syntax error in javascript. :(

Member Avatar for diafol

I would rethink as this:

while($row = mysql_fetch_array($res)){                       
    echo "<tr><td><button data-id='{$row[0]}'>View</button></td></tr>";
}

However, not sure if a single column table full of view buttons makes much sense.

You can then have an event listener (click) and a querySelector (or getAttribute for IE10-) to get the data-id value. It cleans up your HTML from all that horrible inline stuff.

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

If using jQuery, you can just use $(this).data('id')

Perhaps it's me but I really don't like to see PHP spitting out js code.

Eish sorry was an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.