简体   繁体   English

用一种形式将表单提交给不同的动作

[英]Submit form to different actions with in one form

I have one form: 我有一种形式:

<form method="POST" action="index.php">

And 2 button 和2按钮

<button name='button1'></button>
<button name='button2'></button>

What I want is on click button1 then form post to index.php, but on click of button2 then form post to index2.php. 我想要的是单击button1然后将表单发布到index.php,但是单击button2然后将表单形式发布到index2.php。

OR 要么

Click two button form post to index.php, after index.php post array to index2.php. 单击两个按钮将表单发布到index.php,将索引数组发布到index2.php。 I'm not use GET function! 我没有使用GET功能!

remove action in your form This will only redirect to index.php. 删除表单中的操作这将仅重定向到index.php。

<form method="POST" >
<button name='button1' onclick=my();></button>
<button name='button2'onclick=my1();></button>
</form>
<script>
 function my() {
  window.location.href = 'index.php';
  }   
  function my1() {
  window.location.href = 'index2.php';
  }
  </script>

you have to use this by using jquery like below 您必须通过使用如下jquery来使用它

<form method="POST" id="main_form">
    <button name='button1' id="btn_1"></button>
    <button name='button2' id="btn_2"></button>
</form>

$("#btn_1").click(function(){
   $("form#main_form").attr("action","index.php");
   $("form#main_form").submit();
});

$("#btn_2").click(function(){
   $("form#main_form").attr("action","index2.php");
   $("form#main_form").submit();
});

I am not sure why you submit form to two d/t urls but try doing something like this: 我不确定为什么您要向两个d / t URL提交表单,但是尝试执行以下操作:

Add these two submit buttons with in your form. 在表单中添加这两个提交按钮。

<input type = "submit"  name='submit1' value = "submit1" />
<input type = "submit"  name='submit2' value = "submit2" />

So when the user clicks submit button one, the variable name=submit1 will be submitted to the server. 因此,当用户单击“提交”按钮name=submit1 ,变量name=submit1将被提交到服务器。

When the user clicks submit button two, the variable name=submit2 will be submitted to the server. 当用户单击两个提交按钮时,变量name=submit2将被提交到服务器。

Then on your server side you can do something like this: 然后在服务器端,您可以执行以下操作:

if(is_set($_POST['submit1']) && $_POST['submit1'] == 'submit1'){
   //code
}else if(is_set($_POST['submit2']) && $_POST['submit2'] == 'submit2'){
   //code
}

I'm using 我正在使用

<script type="text/javascript">
    function doPreview()
    {
        form=document.getElementById('idOfForm');
        form.target='_blank';
        form.action='<?php echo Base_dir . 'email/create'; ?>';
        form.submit();
        form.action='#';
        form.target='';
    }
</script>

And use onclick in button :) 并在按钮上使用onclick :)

Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM