Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One form, two insert buttons.

Status
Not open for further replies.

rahulpatel

Programmer
Jan 20, 2007
33
AU
I have a form on my page which has a button. The button inserts values from textboxes into the db. Easy peasy. I would like to have 2 buttons. One that inserts the values and simply returns to the same page so the user can add more values. The second button adds the values and moves to the next page.

Is it possible? I have to use php as I'm know zip about javascript/ajax.

Any ideas...
 
Why not have a couple radio group buttons. One for adding another record and one for continuing. That way only you can check which has been selected and if the continue one has then redirect to the next page.
 
sure it's possible. just create two submit buttons with different values

Code:
<form action="somepage.php" method="post">
<input type="submit" name="submit" value="save & continue"><br/>
<input type="submit" name="submit" value="save & return">
</form>

Code:
if (isset($_POST['submit'])){
  if ($_POST['submit'] === "save & continue"){
    savedata();
    // moveforward to the next page
  } else {
    savedata();
    //redisplay the current page
  }
}

function savedata(){
 //do something to save the data submitted in the $_POST variable.
}
 
Thanks both. I'll try the examples tomorrow as it's far too late here and let you know how I get on.
 
Code:
<?php

if (isset($_POST['submit'])){
  if ($_POST['submit'] === "save & continue"){
    savedata();
    header('Location:members.php');
  } else {
    savedata();
    //redisplay the current page
	header('Location:untitled.php');

  }
}

function savedata(){

// set your infomation.
$dbhost='localhost';
$dbusername='XXXXXX';
$dbuserpass='XXXXXX';
$dbname='XXXXXX';


 // connect to the mysql database server.
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);

 //do something to save the data submitted in the $_POST variable.

 $query="INSERT INTO body (bodymake1) VALUES ('$_POST[textfield]')";
if(!mysql_db_query($dbname,$query,$link_id)) die(mysql_error());
 
}
?>

How'd I do? (It works!!) Any need to make it tidier/sleeker?
 
only one thing that I can see (without knowing a great deal about your application): you are not validating and escaping the user submitted data before inserting it into your database. you should check whether textfield contains a value of a type or within the contraints that you expect and accept. and then you should apply the value to the mysql_real_escape_string() function to ensure that the database insert does not fail for unescaped characters.

i don't know what mysql_db_query() does: i guess it is some kind of abstraction layer. would it not be better to handle errors with the abstraction layer?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top