php form data send to two different action

2019-08-16 19:29发布

问题:

i have a form, i want to send form data to two different action, can I do that in php

回答1:

No it's not possible to send form data to two different actions simultaneously (you can send form data by calling other method from the action called in the form ) using PHP or any other programming langauage



回答2:

I think the only way would be something like this:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    switch ($_POST['action']) {
        case 'action1':
             // do something
        break;
        case 'action2':
             // do something
        break;
    }
}

Where you would have a hidden input in your form, which would contain type of the action you want to do. Then after the form is submited, you switch the action and do what you need.

The only other way, without PHP, would be to use AJAX as many of the answers in related questions suggest.



回答3:

No that is impossible because how would the browser know which page to send the user to (that is all an action is).

However, you could have it post to a PHP page that then fires the form submit to two separate actions (only makes sense for different webapps - if it's the same app could just pass to a function call).



回答4:

It is possible! Your HTML form:

<form  name="Form1"  method="post">
<input name="username" value="ramiz" type="username">
<input name="password" value="0" type="hidden">
<input name="url" value="http://google.com" type="hidden">
<button onclick="open_win();send1();send2();closepopup();javascript:timedRefresh(5000);" name="send">Send </button>
</form>

JavaScript:

function open_win()
{mywindow1=window.open('','popup1','width=400,height=100');
mywindow2=window.open('','popup2','width=400,height=100');}function send1()
{document.Form1.action = "http://firstaction.php";
document.Form1.target = 'popup1';
document.Form1.submit(); }
function send2()
{document.Form1.action = "http://secondaction.php"
document.Form1.target = 'popup2';
document.Form1.submit(); }
function closepopup ({setTimeout('mywindow1.close();',5000);setTimeout('mywindow2.close();',5000);}

Details: When you click the submit button it opens two small popup windows, sends data to two different actions in those windows and then closes the popup windows in 5 seconds.

You can use iframes instead of popup windows and hide them using display:none. You have to set target to your iframe name.



标签: php html forms