开发者

How to POST to 2 pages by help of button or link in HTML?

开发者 https://www.devze.com 2023-01-23 09:36 出处:网络
Hey Friends i am having one forms and two button, and some text fields,what i need it if i click button 1 then the details in the text box should be POST to Page1.php if i click Button2 the details in

Hey Friends

i am having one forms and two button, and some text fields,what i need it if i click button 1 then the details in the text box should be POST to Page1.php if i click Button2 the details in the text box should be POST to Page2.php, i am having 8 text boxes to do the in form, how can开发者_运维问答 i do that?


Let's suppose your button one id is btn1 and second has btn2 and form name is frm, you can do something like this:

var btn1 = document.getElementById('btn1');
var btn2 = document.getElementById('btn2');

btn1.onclick = function(){
  document.forms['frm'].action = 'page1.php'
  document.forms['frm'].submit(); // submit the form
};

btn2.onclick = function(){
  document.forms['frm'].action = 'page2.php'
  document.forms['frm'].submit(); // submit the form
};


A PHP solution would be:

<form action='' method='post'>
  <input name='inputText' /><br />
  <button value='1' name='whichOption'></button><br />
  <button value='2' name='whichOption'></button><br />
</form>

At the top of the page this form is in, put this:

<?php
  if(isset($_POST['whichOption']) {
    switch($_POST['whichOption']) {
      case 1: /* do something */ break;
      case 2: /* do something else */ break;
    }
  }
?>

"something" is an include, a session variable set, or whatever you like.


Try this:

<input type="button" value="page1" onclick="this.form.action='Page1.php'; this.form.submit();" />
<input type="button" value="page2" onclick="this.form.action='Page2.php'; this.form.submit();" />


Why not just use the same page? When I need multiple submit alternatives I simply use submit buttons with different names:

<input type="submit" name="submit-save" value="Save" />
<input type="submit" name="submit-delete" value="Delete" />

When I need to know what action it is, I just check which data is sent:

if(isset($_POST['submit-save']))
{
   //Do something
}
elseif(isset($_POST['submit-delete']))
{
   //Do something else
}


Html:
<input id="Button1" value="Button1" type="button" /><br />
<input id="Button2" value="Button2" type="button" /><br />
<textarea id="TextBox1" cols="40" rows="5"><br />
<textarea id="TextBox2" cols="40" rows="5">

jQuery:

function PostToPage(DOOMid,uri) {
    var vDOOMEl = $("#" + DOOMid);

    $.ajax({
        type: 'POST',
        url: uri,
        data: ({text : vDOOMEl.innerHTML}),
        success: function() {},
        dataType: "html"
    });
}

function init() {

    $('#Button1').click(function(e){
        PostToPage('TextBox1','Page1.php');
    });

    $('#Button2').click(function(e){
        PostToPage('TextBox2','Page2.php');
    });
}

window.onload=init;

PHP:

<?php
    //do something with $_POST['text'];
?>

You must download jQuery, and use it for this solution

0

精彩评论

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

关注公众号