Jump to content

javascript inside php while loop


funbinod

Recommended Posts

(sorry if I had to ask it on php forum)

 

inside a php while loop can I run jquery (or javascript) for each row???

 

I tried the following ---------------

while ($row = ...................) {    echo "    <tr>        <td>".$row['abc']."</td>        <td><a id='delete' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td>        <script>        $(document).ready(function(e) {            $('#delete').click(function(e) {		var conf = confirm('Sure Delete!?')		if (!conf) {		    event.preventDefault();		}            });        });        </script>    </tr>    ";}

but this jquery script works for the first row only and not to other rows. how can I run this confirm script for each row generated by while loop?????

 

thank u in advance....

Link to comment
Share on other sites

You can output anything you want with PHP, including Javascript code. That Javascript code attaches a ready event to the document, which looks for an element with the ID delete and attaches a click handler to it. In HTML, IDs need to be unique, only one element can have any ID. That's why the code is only working on one element. Maybe use a class instead and just output a single piece of Javascript code at the end to attach the handler to all elements with that class.

Link to comment
Share on other sites

Agreed. Outputting elements / HTML in a loop is a good idea, but attaching event handles in a loop like that is a bad idea. Use the loop to generate your HTML, and then outside the loop output your JS snippet for attaching events.

Link to comment
Share on other sites

oh yes! thank u! :)

 

I understood what u pointed. but I didn't what u suggested (coz I know less on using class). but I made one logic and solved it. I generated an unique id for each row and attached it to the 'id' and modified the jquery with respect to it. below is what I did-----

$rn = 0;while ($row = ...................) {    $rn++;    echo "    <tr>        <td>".$row['abc']."</td>        <td><a id='delete".$rn."' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td>        <script>        $(document).ready(function(e) {            $('#delete".$rn."').click(function(e) {		var conf = confirm('Sure Delete!?')		if (!conf) {		    event.preventDefault();		}            });        });        </script>    </tr>    ";}

what u think, this would be good to use or not?

Edited by funbinod
Link to comment
Share on other sites

Just break it down for each recommendation

  1. Use a class instead of IDs
  2. Move the event handler outside of the loop

$rn = 0;while ($row = ...................) {  $rn++;  echo "    <tr>     <td>" . $row['abc'] . "</td>     <td><a class='delete' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td>    </tr>  ";}echo "  <script>    $(document).ready(function(e) {      $('.delete').click(function(e) {        var conf = confirm('Sure Delete!?')          if (!conf) {            event.preventDefault();          }      });  });  </script>";
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...