1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

jQuery: Fetch Button Value.

Discussion in 'jQuery' started by cancer10, Sep 8, 2008.

  1. #1
    ok, Let me be as simple as I can with my question.

    I have an index.php page.

    
    <HTML>
    <head>
    <TITLE>Welcome</TITLE>
    <script type="text/javascript" src="jquery.js"></script> 
    <script>
    	$(document).ready(function(){
    	$("#populate_tables").load("ajax.php?id=10");
    	});
    </script>	
    </head>
    
    
    <div id="populate_tables"></div>
    
    </html>
    
    Code (markup):
    The index.php loads all the information in the <div id="populate_tables"></div> tag when the page loads. This part works fine.


    Now, Lets see the code for ajax.php

    
    <?php $id =$_GET['id'];	 
    $attachments = mysql_query("select * from tbl where id=$id order by id desc");
    		 $num_rows = count($attachments);
    		 ?>
    		<table width="50%" border="1" cellspacing="0" cellpadding="3" class="border" style="border-collapse:collapse">
    		<?php 
    		if($num_rows>0){
    			for($x=0;$x<$num_rows;$x++){?>
    	  <tr>
    		<td><input type="submit" id="att_del" name="att_del" value="<?php echo $attachments[$x]['id'];?>" /></td>
    	  </tr>
    	  <?php }//end for loop
    	  }else{//ifelse condition
    	  ?>
    	  <tr>
    	  <td align="center">No Attachments</td>
    	  </tr>
    	  <?php
    	  }//end else
    	  ?>
    	</table>
    
    PHP:
    Everything works well so far.

    Lets go back to the index.php page :)

    When the list loads under the <div id="populate_tables"></div> tag, and when I click on the buttons (att_del), it should alert me with their button value.

    Please help.


    Thanx
     
    cancer10, Sep 8, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Okay, a couple things about your script.

    1) You forgot the body tags in index.php
    2) You should really use addslashes() on your $id before putting it in a query like that, you want to avoid SQL injections.
    3) Doing $attachments = mysql_query() won't have it store the rows in $attachments; you have to do something like $sqlres = mysql_query(); $num_rows = mysql_num_rows(); and then for your loop do while($attachment = mysql_fetch_array($sqlres)){}
    4) Once you update to the above method, you'd do <?=$attachment['id']?> for your button's value (<?= ?> is a shorthand way of doing <?php echo ;?>).
    5) I don't even see any code that should make it alert you the button value when clicked unless you're actually asking us what you should do. I'd say something like onclick="alert($(this).value());" or another jQuery alternative, linking the alert function to the click event after loading the data.

    <HTML>
    <head>
    <TITLE>Welcome</TITLE>
    <script type="text/javascript" src="jquery.js"></script> 
    <script>
    	$(document).ready(function(){
    	$("#populate_tables").load("ajax.php?id=10", "", function(){
              $("#att_del").click(function(){alert($(this).value());});
             });
    	});
    </script>	
    </head>
    <body>
    <div id="populate_tables"></div>
    </body>
    </html>
    HTML:
    <?php 
        $id = addslashes($_GET['id']);
        $sqlres = mysql_query("select * from tbl where id=$id order by id desc");
        $num_rows = mysql_num_rows();
    ?>
        <table width="50%" border="1" cellspacing="0" cellpadding="3" class="border" style="border-collapse:collapse">
    <?php 
        if($num_rows>0) {
            while($attachment = mysql_fetch_array($sqlres)) { ?>
            <tr>
                <td><input type="submit" id="att_del" name="att_del" value="<?=$attachment['id']?>" /></td>
            </tr>
    <?php
            } //end while loop
        } else { //ifelse condition
    ?>
            <tr>
                <td align="center">No Attachments</td>
            </tr>
    <?php
        } //end else
    ?>
        </table>
    PHP:
    Hope that helped (and that I didn't make any errors :p).
     
    zerxer, Sep 10, 2008 IP