Jump to content

Pass variable into php function from button


jcjst21

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.