Jump to content

php posting twice on the same page


paulonline2501

Recommended Posts

hi, should be a straight forwrd answer for this one.is it possible to post twice to the same page? eg: you want the person to select an item from a drop down list, click a button to submit their choose (post #1), and then do something else like update the details of the item and then submit again (post #2). i mean i could go to a different page with the post#1 id after the first post, but i was wondering if i could do it all on the same page? i've had a stab at doing this and the code is below but when i click the 2nd post button it resets the whole page. ive got a sneaky recolection that this normal as this isnt possible but cant quite remember. any help would be great. CODE:

<?phpif (isset($_POST['post1'])){echo"Form 1 has been submitted.";/**********post 2*******************************/  if (isset($_POST['post2']))  {   echo"Form 2 has been submitted.";  }   else   {  //render form 2  ?>  <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="form2"><fieldset>  <table id="site-form">   <tr>	<td class="one_of_three"> </td>	<td class="two_of_three"><input name="post2" id="post2" type="submit" value="Click Me (2)"/> <a href="">Cancel</a>.</td>	<td> </td>   </tr>  </table>  </fieldset></form>  <?php  }/********** end post 2*******************************/}else {//render form 1?><form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="form1"><fieldset><table id="site-form"><tr>  <td class="one_of_three"> </td>  <td class="two_of_three"><input name="post1" id="post1" type="submit" value="Click Me (1)"/> <a href="">Cancel</a>.</td>  <td> </td></tr></table></fieldset></form><?php}?>

if you want to see whats happening its here:http://paulyouthed.c...p-two-posts.php

Edited by as_bold_as_love
Link to comment
Share on other sites

You can chain as many forms as you want. The reason your code shows form 1 again after submitting form 2 is because the first if statement where you're checking if $_POST['post1'] exists fails, form 2 does not submit an item called post1. Your first if statement should either check if either post1 or post2 are set, or give both forms the same name and give them different values. You can give them the same input name, like "form_name", and give it values like "form1", "form2", etc for the different forms. Then if you check if $_POST['form_name'] is set it will be set regardless of which form was submitted and you can check the value of $_POST['form_name'] to figure out which of the forms was submitted.

Link to comment
Share on other sites

<?php  if (isset($_POST['post2']))  {   echo"Form 2 has been submitted.";  }else{if (isset($_POST['post1'])){echo"Form 1 has been submitted.";/**********post 2*******************************/  //render form 2  ?>  <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="form2"><fieldset>  <table id="site-form">   <tr>	    <td class="one_of_three"> </td>	    <td class="two_of_three"><input name="post2" id="post2" type="submit" value="Click Me (2)"/> <a href="">Cancel</a>.</td>	    <td> </td>   </tr>  </table>  </fieldset></form>  <?php/********** end post 2*******************************/}else{//render form 1?><form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="form1"><fieldset><table id="site-form"><tr>  <td class="one_of_three"> </td>  <td class="two_of_three"><input name="post1" id="post1" type="submit" value="Click Me (1)"/> <a href="">Cancel</a>.</td>  <td> </td></tr></table></fieldset></form><?php}}//  if (isset($_POST['post2']))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...