开发者

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 subm开发者_C百科it to save.php

</form>

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


<?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.


Give each submit button a name (e.g button1 and button2). Submit your form to a single php script. On this php script, check the $_POST vars to see which button was clicked. Then act accordingly, processing your form values and redirecting to whatever page you want afterwards.

To check which button was clicked, do something like:

if ($_POST["button1"]) {
  // do stuff
} elseif ($_POST["button2"]) {
  // do other stuff
}


If your using CodeIgniter there are better ways to do this. (another post for CodeIgniter points to this as being duplicate so thought I'd add a better solution for CI.)

In my case I have a shopping cart form (for example). On that form I might have buttons : checkout, update cart, and clear.

Form:

Here is the form buttons on the cart form screen:

<input type="submit" value="Update">
<input type="submit" value="Clear">
<input type="submit" value="Checkout">

Controller:

The form action points to a controller with a method save ("cart/save/"). Save simply determines which button was clicked and forwards to the appropriate cart function to handle form submission.

cart/save()

public function save() {
    $submit_button = $this->input->post('submit_button');
    if ($submit_button == 'Update')
        $this->update();
    else if ($submit_button == 'Checkout')
        $this->checkout();
    else if ($submit_button == 'Clear')
        $this->clear();
}

Within the controller (cart) I simply then have update(), clear() and checkout() functions which the save() method calls. There is no need to redirect, via CI's redirect() or php header redirects etc.

I hope that helps someone


you can do it from the server side (as answered before me), you can do it from client side as my answer:(you can even do both):

<form action="" id="frm" method="post">
   <textarea></textarea>
   <input type='button' onclick="submitMe(this)" value='Preview'>
   <input type='button' onclick="submitMe(this)" value='Save'> 
</form>

<script>
function submitMe(obj){
  if(obj.value == "Preview"){
   document.getElementById('frm').action = 'preview.html'
  }else{
   document.getElementById('frm').action = 'save.html'
  }
 document.getElementById('frm').submit();
}
</script>


Do it on the server side via a redirect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜