Jump to content

how to use two buttons with php


doforumda

Recommended Posts

hi i have one form. there are three fields which are to be filled by the user. and then there are two buttons. ob clicking first button i want to save user's provided data to the database display the same page again. and on clicking second button the provided data will be saved and will be taken to next page.

 

my problem is with these buttons how can i set up these buttons to do the the action?

 

my code is here

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
  <label>Degree Name:
  <input type="text" name="textfield" id="textfield">
  </label>
  <p>
    <label>CGPA:
    <input type="text" name="textfield2" id="textfield2">
    </label>
  </p>
  <p>
    <label>Institute:
    <input type="text" name="textfield3" id="textfield3">
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="button" id="button" value="Save And Add Another">
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="button2" id="button2" value="Save And Proceed to Next Step">
    </label>
</p>
</form>
<?php
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
						(
							degree,
							cgpa,
							institute
						) 
							values
						(
							'".$degree."',
							'".$cgpa."',
							'".$institute."',
						)");
echo "Your data is added.";
?>
</body>
</html>

Link to comment
Share on other sites

No you can just use two submit buttons, but give them a different name so you can identify which was pressed. For example:

 

<form ...>
    <input type="submit" name="return" value="Save and return" />
    <input type="submit" name="next" value="Save and move on" />
</form>

 

if (isset($_POST['return']))
{
    // ...
}
elseif (isset($_POST['next']))
{
    // ...
}

 

Only the button that was clicked will be posted.

Link to comment
Share on other sites

i am doing using the idea of your's MrAdam but it is not working. can you tell me what i am doing wrong here

 

<?php
if(isset($_POST['return']))
{
$degree = $_POST['degree'];
$cgpa = $_POST['cgpa'];
$institute = $_POST['institute'];

echo $degree.'<br>';
echo $cgpa.'<br>';
echo $institute.'<br>';
}
else if(isset($_POST['next']))
{
echo '<form name="form1" method="post" action="next.php">';
}
?>

Link to comment
Share on other sites

this is what i am want to do.

the user will enter his qualification. he will enter more then one qualification step by step. when he enters first qualification then if he wants to enter second one then he will press the button with return name so his data will be entered into db and will face again the same form to enter second qualification(degree/certification). then if he wants to enter another then click the "Save And Add Another" button. save this data into db and then he will face the same form. once he finishes with this then he will click "Save And Proceed to Next Step" button to save the last qualification in db and will be passed to the next page but this time insert query will be on next page to enter the last qualification into db.

 

by using header() ftn this goes to next page but data is not passing there.

my next page is here

 

<?php
$degree = $_POST['degree'];
$cgpa = $_POST['cgpa'];
$institute = $_POST['institute'];

echo $degree.'<br>';
echo $cgpa.'<br>';
echo $institute.'<br>';
?>

Link to comment
Share on other sites

Using header redirects which will clear the $_POST array, you will either have to self post and simply check what stage your at and output the approriate information. Alternatively use the $_SESSION array to persist the data before calling the header function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.