开发者

Two different destination of submit in one form

I have one form which need to submit data into two different tables in same DB. at past time, i have asked how to submit form into two tables,开发者_运维知识库 but it impossible.

Then, I try to submit this form use two submit buttons with different destination. Could you give some advise to submit this form after click them?


Javascript:

function button1click() {
  yourForm.action = "ajax1.php";
  yourForm.submit();
}

function button2click {
  yourForm.action = "ajax2.php";
  yourForm.submit();
}

HTML:

<form action='' method='post'>
  <input type='input' id='blah' name='blee' />
  <button type='button' onclick='button1click()'>Button 1</button>
  <button type='button' onclick='button2click()'>Button 2</button>
</form>


why dear ?

if($_POST['submit']) {

$sql1="insert into table 1"; mysql_query($sql1);

$sql2="insert into table 2"; mysql_query($sql2);

}

This should work..

one submit button only. OK!


You can do this many different ways. Just because the data is on one form, doesn't mean it has to go into one table. Basically you need to learn how to write some server side code that parses the incoming data and puts it where it needs to be.

So the simplest way is to just submit your form, and then on the server save the data to where it needs to go.

Having 2 buttons could be clunky, unless thats how it was designed...


<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("MYDB") or die(mysql_error());

// Insert a row of information into the table "example"
$sql="INSERT INTO example 
(name, age) VALUES('Timmy Mellowman', '23' ) ") 
or die(mysql_error());  

// Insert a row of information into the table "example"
$sql=mysql_query("INSERT INTO example1 
(name, age) VALUES('Timmy Mellowman', '23' ) ") 
or die(mysql_error());  

?>


You can insert into different tables like this:

EDIT (new code):

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("double") or die(mysql_error());

// Insert a row of information into the table "example"
mysql_query("INSERT INTO example 
(name, age) VALUES('Timmy Mellowman', '23' ) ") 
or die(mysql_error());

// Insert a row of information into the table "example"
mysql_query("INSERT INTO example1 
(name, age) VALUES('Timmy Mellowman', '23' ) ") 
or die(mysql_error());  

?>

Hope this answers you question because toy cant have double destination (i think) but this inserts into two different MySQL tables...

Good luck


To summarize what others have said above,

  • You could submit to two different locations, using javascript to change the action attribute of the form. You might wanna use it if the two destinations are not on the same server.
  • Or you could submit it to one destination only, massage your data, and then insert into two tables using php. It'll be particularly advantageous as you wouldn't need to sanitize or validate the data twice on server-side.


Very Very Simple...

You can insert in two tables by only one submit button..

Example:

<?php
if(isset($_POST['submit']))
{
  $value1 = (!empty($_POST['form_name_value1']))?$_POST['form_name_value1']:null;
  $value2 = (!empty($_POST['form_name_value2']))?$_POST['form_name_value2']:null;

  $str = "INSERT INTO table_name1(db_value1, db_value2)VALUES('$value1','$value2')
  $sql = mysql_query($str) or die(mysql_error());


  $str1 = "INSERT INTO table_name2(db_value1, db_value2)VALUES('$value1','$value2')
  $sql1 = mysql_query($str1) or die(mysql_error());

  if($str)
  {
  echo "Successful";
  }else{
  echo "Unsuccessful";
  }


  if($str1)
  {
  echo "Successful";
  }else{
  echo "Unsuccessful";
  }
}

This question was asked 4 years ago. Not the time to answer now I know. But still answering to help others searching for the same problem.

Vote Up if you find this helpful. :D

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜