Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36591

Two actions two submit button in php?

I've form tag like this

sample name:register.php page

<form id="formElem" name="formElem" action="form10.php" method="post">
<input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
<input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
6 more input boxes
<button name="submit" type="submit">Register</button>
<button name="preview" type="submit">Preview</button>

</form>

I'm sending this info to next form10.php page and displaying all the 10 input values on that page I'm using $pd= htmlentities($_POST['pd']); $fname= htmlentities($_POST['fname']); to fetch values from form tag and such 10 variables and I'm echoing those entered value on form10.php file after successful submit button.

like i entered fname, mname, lname came from form tag and displayed on form10.php page.

first name <?echo $fname?> 

but now problem is user can see the next page (form10.php) after entering only 10 textboxes values inside form tag. but I want to give preview option to user so that user can preview that next page either filling any of 1 to 10 textbox values. means he has filled fname and lname but not rest of 8 fields and he clicks on preview button I want to open form10_preview.php which same as form10.php but as user has entered only fname and lname so echo only those values which he as supplied.

Now problem is how can i can have two submit button and two actions in one form?

Upvotes: 0

Views: 593

Answers (2)

larsAnders
larsAnders

Reputation: 3813

You could have the form point to its own page and handle each submit value separately. At the top of the file with the form, you'll need to start the output buffer and a session. This allows the use of header() to redirect, and storage of session variables.

<?php 
    ob_start(); 
    session_start();
?>

The form will point to itself by removing the action attribute:

<form id="formElem" name="formElem" method="post">
    <input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
    <input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
    <input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
    <input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
    6 more input boxes
    <button name="submit" type="submit">Register</button>
    <button name="preview" type="submit">Preview</button>
</form>

We process each of the buttons via their name in the POST array:

<?php
    if(isset($_POST['submit'])){
        foreach ($_POST as $key => $value) {
            $_SESSION[$key] = $value;
        }
        header("Location: form10.php");
    }
    if(isset($_POST['preview'])){
        foreach ($_POST as $key => $value) {
            $_SESSION[$key] = $value;
        }
        header("Location: form10_preview.php");
    }
?>

And at the very end of the file, we flush the output buffer:

<?php ob_end_flush(); ?>

So, essentially the form has one action, which is to submit the values to itself. Finally, both form10.php and form10_preview.php will need session_start(); at the top of the file to access the Session variables we've created, like so:

<?php 
session_start();

$inputs = array("pd", "fname", "mname", "lname", etc...);

foreach ($inputs as $input) {
    echo $_SESSION[$input];
}

?>

Upvotes: 0

davidkonrad
davidkonrad

Reputation: 85518

I think it is better to control form submit rules clientside. Remove the action from your form, and change the button type to be button :

<form id="formElem" name="formElem" action="" method="post">
<input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
<input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
6 more input boxes
<button id="register" type="button">Register</button>
<button id="preview" type="button">Preview</button>
</form>

Then let javascript control the flow of the submitting :

var formElem = document.getElementById('formElem'),
    btnSubmit = document.getElementById('register'),
    btnPreview = document.getElementById('preview');

function formSubmit() {
    switch (this.id) {
        case 'register' :
            formElem.action='post10.php';
            break;
        case 'preview' :                    
            formElem.action='preview10.php';
            break;
    }
    formElem.submit();
}

btnSubmit.onclick = formSubmit;
btnPreview.onclick = formSubmit;

Upvotes: 3

Related Questions