How do I get multiple buttons in a form going to different places?

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
I was wondering how forums have two buttons "Submit" and "Preview" which go to different places from the same form. I would like to duplicate that effect for a review posting site. Could someone tell me how that works and how to do that?
 
Last edited:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
If you take a look at the HTML for, say, a standard submit button:

HTML:
<input type="submit" name="submit" value="Submit">
<input type="submit" name="submit" value="Preview">

you'll see that the button is actually a type of form input with a name and a value. The value sent to the server would depend on which of the buttons you click, and since both of the buttons have the same field name, there's only one value to test. You can pick that up, just as you can with any other type of input, using $_POST["submit"].

The value of a submit-type input element is also the text displayed on the button. If you use a button-type element, you can keep the value and the display text separate, just make sure to make the name attribute the same for all of your submit buttons. The different values will allow your target script (the URI in the action attribute) to process the page differently depending upon which button was clicked.
 
Messages
17
Reaction score
0
Points
1
The above example may work, but strictly speaking it's not obeying any of the standards. They only standards compliant way to do it is to either have multiple forms - which is a hack, and is a pain to get to render the same in all browsers. Or to use the button element and use javascript to submit the form.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
@essellar I understand that. I just have to check the value on my php validation page, and I can understand that: I send it to the page I'm on and it checks a condition statement whether or not to pass to the submit page. What I don't understand is how to send the form $_POST[] to the validation page from going from the same page with the condition. If that's confusing, here's an example:

PHP:
<?php
if ($_POST['submit']) {
    header("location:validateScript.php");
} else if ($_POST['preview'])) {
// Display the page as it would look
}
<form action="">
<input name="name" />
<input type="submit" name="submit" />
<input type="submit" name="preview" />
</form>

How do I send POST form elements from the previous page if I redirect it like this?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The above example may work, but strictly speaking it's not obeying any of the standards.

Absolutely wrong. There's a reason why there is a value attached to the submit button, and that there always has been. It is a form input element, and is meant to be one. And while elements on a HTML page or in a form must be uniquely identified by an ID attribute, multiple elements with the same NAME attribute are not only standards-compliant but necessary, otherwise radio buttons and checkboxes could not work. Multiple forms have never been necessary for this; since the inception of HTML forms, the URI for the form's action has been the location of a script to process the form data. Don't confuse the METHOD (the HTTP verb) with the ACTION. (RESTful advocates will tell you that you should use POST for new data and PUT for edited data.)
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
How do I send POST form elements from the previous page if I redirect it like this?

You don't. There's no reason to redirect; you should do both types of processing from the same script URI. To keep things clean, you might want to conditionally include/require your processing code based on the value of the submit button, but there's no reason to redirect unless and until the form data has been accepted. Your validation should happen in situ, and you can return the user immediately to the original form with their data (and any failure notifications) added if validation fails.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
I'm still a little confused. Are you saying I should send the form to the page on which the form is located, then check whether the "Preview" or "Submit" buttons were pressed, then if it was preview, it will fill the form out and notify with errors and if it was submit, it will send it to the validation/post script?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Explain exactly what you want to do.

1. You have a page with a form on it
2. When the user is done filling out the form, you give him two buttons. Why?
3. Why not give him a radio button choice?
4. You want to process the form differently depending on which button he clicks? What if he presses the ENTER key?
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
@descalzo One button takes the user to a preview of what the post will look like, keep in mind this is a video game review site, and one takes the user to the validation/add script.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Do you want the preview to be done via JavaScript or via a trip to the server?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
I'm still a little confused. Are you saying I should send the form to the page on which the form is located, then check whether the "Preview" or "Submit" buttons were pressed, then if it was preview, it will fill the form out and notify with errors and if it was submit, it will send it to the validation/post script?

Not quite. There's no reason to "send" the user anywhere other than the target script (the ACTION attribute's URI) until the submission has been validated and accepted. You keep using the same page until that happens. The preview area and the validation script are included on the form as required; you don't need to send the data to a different location in order to do validation.

Keep returning the form to the user until validation passes or the user bails out, adding a prominent failure notification to the form. Keep whatever data can be kept by echoing the submitted data into the various fields (the users will crucify you if they've spent a long time entering data and you return a blank form to them) and mark all of the bad fields clearly.
 
Top