Jump to content

POST data according to a button


johny20

Recommended Posts

Hello!I have a form with a textbox. All I want to do is to post the data to a page according to which button was clicked.Suppose that I ask for the user to input an ID. Then there are two buttons: Edit and Delete. The edit button must send the ID to the edit.php and the delete button to the delete.php.

<form method = "post" action ="?"><input type = "text" id = "ID" /><input type = "submit" value = "Edit" onClick="window.location=''edit.php"/><input type = "submit" value = "Edit" onClick="window.location=''delete.php"/>

Well, I know it doesn't work but I want one form because if I am going to use two forms I will mess the layout (I will add more buttons.)Any idea please?

Link to comment
Share on other sites

  • 3 weeks later...

Sorry for the late reply but I didn't receive notifications about your replies..I resolved the problem by using Javascript as mentioned by Obi1 . Also, justsomeguy's post seems to be correct but, in my case, I don't like to use 1000 web pages.Scenario: We have stored some values in Mysql and then we present the values to user. On the same page we have some additional options like 'Delete the entry' or 'Edit the entry'. According to the will of the user we have to POST the data to the delete.php OR to the edit.php to perform the operation.

<html><head><script type="text/javascript">function test(){   if(document.pressed == 'Edit')    {     document.testform.action ="../edit.php";    }    else {document.testform.action = "delete.php";}  return true;  }  </script></head><body><!-- appropriate php code to SELECT the data from the mysql server and present them to the user --><form name = 'testform' method='post' onsubmit= 'return test();'><h3>Please input the <u>unique id</u> you would like to edit:</h3><input type="text" size = "4" id = "id" name = "id" /> <input type = "submit" value = "Edit" id = 'Edit' onclick="document.pressed=this.value" /><input type = "submit" value = "Delete" onclick="document.pressed=this.value" /></form></body></html>

So, I post the data to the appropriate php page. I haven't tested too much that approach but I don't think it is problematic.Hope that it will help some people!

Link to comment
Share on other sites

Looks like that technique will throw an error if the user hits enter/return while the focus is on the text input.
Yes and No. I did it before 2 minutes. It goes to delete.php and since the user hasn't specified any ID then it can't delete any entry. It is not the best behaviour but at least it works for me! The following code might resolves that issue:
function test(){   if(document.pressed == 'Edit')    {     document.testform.action ="../edit.php";  return true;  }    else if (document.pressed == 'Delete'){document.testform.action = "delete.php";return true;} else {  return false;}}

Link to comment
Share on other sites

Also, justsomeguy's post seems to be correct but, in my case, I don't like to use 1000 web pages.
What does that mean? The point is to have a single page that does all of the work. Why have dedicated pages for editing and deleting, or adding for that matter? Just have one page with all of the code to handle the database work, and have that page figure out what it's supposed to do.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...