OnClick event variable not being set in loop

Discussion in 'PHP' started by noelde, Mar 30, 2007.

  1. #1
    Hi - I'm new to the forum and php and could use some help.

    I'm trying to correct the following erroneous code (excerpted below) and am hoping that I can be set straight. The problem I'm having is with the onclick event.

    My hope was to have the href be assigned to the same page ("test_loop_hrefs.php") for each of the listed items, but when the user clicks a listed item, a session var ($_SESSION['recordid']) is set to a unique recordid ($row['prop_id']) that then determines the database content for the page.

    The browser renders the hyperlinks in the bulleted list fine. But for the onclick event, the $_SESSION['recordid'] is set to the last $row['prop_id'] from the while loop for every hyperlink...please see below.

    $tbl_name="tb_content";
    $sql="SELECT * FROM $tbl_name order by price asc";
    $result=mysql_query($sql);

    echo "<ul>";
    while($row=mysql_fetch_assoc($result)){
    ?>
    <li><a href="test_loop_hrefs.php" onclick="<?php $_SESSION['recordid']= $row['prop_id']; ?>"><?php echo $row['address1']; ?>[/a]</li>
    <?php
    } // End while loop.
    echo "</ul>";

    Thanks in advance for your much needed help!!
     
    noelde, Mar 30, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Onclick is a Javascript event. It's impossible to use with PHP since it runs on the client's browser, and now the server where PHP is located.
     
    nico_swd, Mar 30, 2007 IP
  3. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ...But, I was successful using the following code:

    <p>This will change the recordid: <a href="test_properties.php" onclick="<?php $_SESSION['recordid'] = 4; ?>">Click Me</a></p>
     
    noelde, Mar 30, 2007 IP
  4. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code you posted sets the recordid before any click happens, and has nothing happen when the anchor is clicked.
     
    giraph, Mar 30, 2007 IP
  5. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    is there a way i can get what I want accomplished?

    I want the href to set the session variable onclick and then reload the page. I guess I'm stuck because the mysql_fetch_assoc() that sets up the hrefs doesn't leave me anything to work with.

    do I need to learn javascript?

    Thanks...I'm starting to stress a bit.
     
    noelde, Mar 30, 2007 IP
  6. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you could do
    
    <a href="test_properties.php?recordid=4">Click Me</a>
    
    Code (markup):
    And then handle the 4 in the test_properties.php
     
    giraph, Mar 30, 2007 IP
  7. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think my problem is that I'm rendering the hrefs through a while loop using the mysql_fetch_assoc(). I would still want to do something like:

    while... mysql_fetch_assoc($result) {
    < href: test_loop_hrefs.php? recordid=$row['prop_id'] .....
    }

    and so it's the $row['prop_id'] problem that I can't find a solution for. I.e. the last $row['prop_id'] value from the $result query ends up being the value for each of the hrefs.

    Wouldn't hurt to give it a shot, ...I'm kinda skunked right now.

    Thanks a bunch for your responses giraph ...sometimes you feel all alone dealing with these bugs.
     
    noelde, Mar 31, 2007 IP
  8. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well if I understand you correct, what you want to do is:
    
    while($x = mysql_fetch_assoc($result))
    {
    	echo "<a href=\"test_loop_hrefs.php?recordid=$x['prop_id']\">Click Here</a><br />";
    }
    
    Code (markup):
    If that's not what you mean, then I guess I'd need more of an explanation.
     
    giraph, Mar 31, 2007 IP
  9. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    http://www.....com/test_loop_hrefs.php?recordid=

    I guess the $row['prop_id'] that comes from mysql_fetch_array($result) doesn't leave a value once the loop is outputted in the browser?
     
    noelde, Mar 31, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    I'd be surprised if that worked at all. Try taking out the single quotes in the array keys, or place curly brackets around the variables.

    
    echo "<a href=\"test_loop_hrefs.php?recordid=$x[prop_id]\">Click Here</a><br />";
    
    PHP:
    Or
    
    echo "<a href=\"test_loop_hrefs.php?recordid={$x['prop_id']}\">Click Here</a><br />";
    
    PHP:
     
    nico_swd, Mar 31, 2007 IP
  11. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm seeing the correct url return value, which is very cool. Now, how do I set a $_SESSION['recordid'] with it and then render the page using the session variable?

    I appreciate it. It feels like it's getting closer.
     
    noelde, Mar 31, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    Why do you use sessions? Why don't you catch the variable with $_GET['recordid'] ?
     
    nico_swd, Mar 31, 2007 IP
  13. noelde

    noelde Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks a bunch....it's working
     
    noelde, Mar 31, 2007 IP