Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - How to submit a form to two different pages depending on the button clicked, without javascript

I have a form with a text area and 2 buttons, i need one of them to submit to the same page its on and the other to submit to another php file. Both buttons need to allow the text areas to be referenced by post. How can i do this.

For Example:

<form action="" method="post">

   <textarea></textarea>

   <input type='submit' value='Preview'> //I want this to submit to the same page

   <input type='submit' value='Save'> // I want this to submit to save.php

</form>

Note: All my html is generated by php through different scripts that change depending on users previous actions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<?php

if (isset($_POST['action1']) || isset($_POST['action2'])) {
    // handle textarea

    if (isset($_POST['action1'])) {
        header('Location: /action1.php');
        exit();
    }

    header('Location: /action2.php');
    exit();
}

?>

<form>
  <fieldset>
    <textarea name="text"></textarea>
    <input type="submit" name="action1" value="Action1">
    <input type="submit" name="action2" value="Action2">
  </fieldset>
</form>

You only have to be cautious about what happens when the user presses the enter key to submit the form. I.e. what submit will be triggered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...