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!!
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.
...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>
The code you posted sets the recordid before any click happens, and has nothing happen when the anchor is clicked.
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.
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
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.
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.
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?
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:
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.