PHP: how to use two different submit buttons for the same form<

liunx

Guest
I searched this forum for a solution but didn't find anything I thougth was useful.

What I'm trying to do is to create a form where the user inputs into a textarea box, and then gets the choice to either 'Submit' to make their changes permanent, or to 'Preview' so that they can see what their new changes look like.

So far, from what I gleaned from googling this subject, I have this:

if($_POST['Preview']) {
then do preview...
}
else if($_POST['Submit']) {
then do submit...
}

<form action='?page=manager/edit_operation_logistics.php' method=POST >
<table>
<tr>
<td width='400' align=left><textarea class='wide3' name='email_body'></textarea></td>
</tr>
<tr>
<td width='100' align=right> <input type=submit name='submit' value='Submit'></td>
<td width='200' align=right> <input type=submit name='preview' value='Preview'>td>
</tr>
</table>
</form>


but obviously this will not work.

Does anyone know how to have two separate submit buttons that can get processed differently depending on which button was pushed?

Thanks.possibly create an action for each button?Josh,

But won't that mean that I would have to have to seperate <form> </form> sections? And then I would have to have two separate input fields for the input field email_body, but that would look wierd...

Or am I misunderstanding your suggestion altogether?If you put the php script in a seperate file you can point the action of the submit button to that file.Originally posted by kfiralfia
I searched this forum for a solution but didn't find anything I thougth was useful.

but obviously this will not work.

Does anyone know how to have two separate submit buttons that can get processed differently depending on which button was pushed?

Thanks.
why won't that work? you just need to show the form again with submitted variables when they preview.

if($_POST['Submit']) {
then do submit...
}
else if($_POST['Preview']) {
then do preview...
}
<form action='?page=manager/edit_operation_logistics.php' method=POST >
<table>
<tr>
<td width='100' align=right> <textarea>{$_POST['name_of_textarea']}</textarea></td>
<td width='100' align=right> <input type=submit name='submit' value='Submit'></td>
</tr>
<tr>
<td width='200' align=right> <input type=submit name='preview' value='Preview'>td>
</tr>
</table>
</form>

load the page once of course it should be empty, load the preview and it wil get what was posted. just like this forum does.scoutt,

So you are saying that my code above should work fine? But it doesn't work...

I thought that the code

if($_POST['Submit']) {
then do submit...
}
else if($_POST['Preview']) {
then do preview...
}

wouldn't work for sure, because I don't think I am posting a variable called 'Submit', since this button is of type submit.

Any ideas?Scoutt,

Never mind, I figured out what I was doing wrong...

I was testing for 'Submit' instead of 'submit'

name='submit' value='Submit'

Thanks.yes it will work, you jsu tneed to get the correct names

<input type=submit name='submit' value='Submit'></td>
<input type=submit name='preview' value='Preview'>

you are looking for those, so naturally since they are both submits they still go by the name. if you want the values then you can get that as well.

if($_POST['submit']) {
then do submit...
}
else if($_POST['preview']) {
then do preview...
}transfering thread to the PHP forum
 
Back
Top