views:

80

answers:

6

Hello all,

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?

+1  A: 

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>
Steve
agk! obtrusive JavaScript
Drew
A: 

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

hvgotcodes
A: 

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!

zod
i just can post for $sql1.i post my query.
klox
A: 

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

Trufa
it just mention one table.
klox
@klox sorry! there you go! (forgot to mark it as code)
Trufa
@trufa you selected two DB. i'm just want to post data into two different table but still in same DB
klox
@klox how about there? I´ve edited the code
Trufa
@trufa : i found the answer you can learn for that the DB connection syntax almost same, so i'm not mention it.
klox
@klox sorry did not understand!
Trufa
i will edit my answer, you just need use one connection.
klox
@klox ahh now I get it! great!
Trufa
A: 
<?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());  

?>
klox
A: 

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