简体   繁体   中英

Submit form to different actions with in one form

I have one form:

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

And 2 button

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

OR

Click two button form post to index.php, after index.php post array to index2.php. I'm not use GET function!

remove action in your form This will only redirect to 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

<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:

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.

When the user clicks submit button two, the variable name=submit2 will be submitted to the server.

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 :)

Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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