jcjst21 Posted June 20, 2012 Share Posted June 20, 2012 Hello, I have a while loop that prints the contents of a field in one of the tables in my database. I would like the user to have the option to delete that record from the website. I created a button that is displayed with an icon image, but would like to call a php function onclick. Here is my code for the while loop generating a row in the table for each record: <table> <?php while($row = mysql_fetch_array($result)) { echo "<tr><td>" .$row['Name']. "</td><td><input type=\"image\" src=\"1340120170_edit.ico\"></td><td><input type=\"image\" src=\"1340120267_delete.ico\" onclick=\"deleteRecord(" .$row['nameID']. ")\"></td></tr>"; } mysql_close($con); ?> </table> I'm calling "deleteRecord()" from the last button..I want to pass in $row['nameID'] into the function deleteRecord().... am I setting up my echo or input statement incorrectly? Here is my deleteRecord() function: function deleteRecord(&$recordNum) { $nameID=$recordNum; $sql3=mysql_query("DELETE FROM utilityNames WHERE nameID=$nameID"); } When I click the button; nothing happens? Any advise? Thanks:) in advance. Quote Link to comment https://forums.phpfreaks.com/topic/264473-pass-variable-into-php-function-from-button/ Share on other sites More sharing options...
Skewled Posted June 20, 2012 Share Posted June 20, 2012 I'm pretty sure PHP doesn't work this way, but I may be wrong. I'm not familiar with (&$variable) in php, now c++ I know your passing a reference but that's off topic so I'll stop there. Typically you'll be using $_GET and $_POST to deal with form data, and you'd be smart to secure input and validate everything or your going to be really mad at yourself when someone takes advantage of it. Either way you face it, you will be passing the information to a function but not the way your doing it in the code you posted. I don't know how your structuring your web application, and right now I'm super sleepy to continue on this one. I'd suggest googling some tutorials on functions in PHP, and even more using them with classes. Quote Link to comment https://forums.phpfreaks.com/topic/264473-pass-variable-into-php-function-from-button/#findComment-1355337 Share on other sites More sharing options...
requinix Posted June 20, 2012 Share Posted June 20, 2012 JavaScript code cannot call PHP functions. They are very, very separate things. Look into AJAX if you don't want any page reloads or just a normal POSTed form if you don't mind them. And that reference thing you've got going with deleteRecord()? Get rid of it. You won't be using it. Just stick with normal variables until you understand what references are and, more importantly, when you should use them. Quote Link to comment https://forums.phpfreaks.com/topic/264473-pass-variable-into-php-function-from-button/#findComment-1355342 Share on other sites More sharing options...
Skewled Posted June 20, 2012 Share Posted June 20, 2012 http://php.net/manual/en/language.references.pass.php Been awhile since I looked at new additions to PHP but yeah that's pretty sweet! Quote Link to comment https://forums.phpfreaks.com/topic/264473-pass-variable-into-php-function-from-button/#findComment-1355361 Share on other sites More sharing options...
trq Posted June 20, 2012 Share Posted June 20, 2012 http://php.net/manual/en/language.references.pass.php Been awhile since I looked at new additions to PHP but yeah that's pretty sweet! There is nothing new about them. Quote Link to comment https://forums.phpfreaks.com/topic/264473-pass-variable-into-php-function-from-button/#findComment-1355364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.