Jump to content
Larry Ullman's Book Forums

Posting Data From 2 Forms To The Same Page?


Recommended Posts

I am on the last part of chapter 9 and doing my own thing in the last part of Persue.

 

Is it possible to have 2 forms post to the same page (post to self) without interfering with each other?

What I am trying to describe is tricky but it is something like this:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

if (a certain condition happens) {

Create a new SECOND FORM form & post it back to the same php page.
<form action="" method="post">
<input  NEW STUFF >
<input type="submit" name="submit2" value="select">
</form>

Problem now is that the first form validation is cleared and all the errors from

the first form are generated.

}

}

FIRST FORM:
<form action="" method="post">
<input>
<input type="submit" name="submit1" value="submit">
</form>

The source code is is a bit long to post.

Let me know if not clear enough & I'll try to get the sample up and running online.

Link to comment
Share on other sites

Yes, absolutely it is possible, and there are any number of ways to accomplish this. The easiest solution would be to add a hidden field to both forms with the same name but different value. Then you can write a simple if/else if statement at the top of your script to test if one or the other (or neither) was submitted.

 

If you don't want to do that, all you have to do is find some difference between the two forms and test for that. For example, if the label used for the two submit buttons is different, just test for that, etc., etc.

  • Upvote 1
Link to comment
Share on other sites

Here's some basic PHP/HTML demonstrating my point:

<?php
  
  if (isset($_POST['form_type']) && $_POST['form_type'] == 'form1') {
    
    // Code here for form #1
    
  } else if (isset($_POST['form_type']) && $_POST['form_type'] == 'form2') {
    
    // Code here for form #2
    
  }
  
?>


<form action="" method="post">
  
  <!-- Input fields here -->
  
  <input type="hidden" name="form_type" value="form1">
  
</form>


<form action="" method="post">
  
  <!-- Input fields here -->
  
  <input type="hidden" name="form_type" value="form2">
  
</form>

 

That's the most basic shell, and it's missing a lot of essentials, but I think you get the point.

Link to comment
Share on other sites

Yes, I see how that works. I actually ended up trying something similar that went kinda like this:

<?php
  
  if ( isset($_POST['submit1']) == true) {
    
    // Code here for form #1
    
  } else if ( isset($_POST['submit2']) == true) {
    
    // Code here for form #2
    
  }
  
?>
<form action="" method="post">
  
  <!-- Input fields here -->
  
  <input type="submit" name="submit1" value="submit">
  
</form>


<form action="" method="post">
  
  <!-- Input fields here -->
  
  <input type="submit" name="submit2" value="submit">
  
</form>

I think that the above code would achieve something similar to yours? [or am I wrong?] [ is there something wrong with the method i used]

 

Anyways, the conditionals got a bit too complicated because trying to apply them to a big splash of procedural code after it's all

written was really cumbersome & I was getting loads of logic errors. I should have thought it out more at the start.

 

So in the end to get around it, I removed the above conditionals & just appended all the previously submitted data from form1 into the hidden inputs in form2.This meant all the errors went away at least. I don't know if there is anything wrong with this method.

(ugly code) or more issues??

 

Anyways the solution I opted for went something like this:

 

FORM 2:


 for ($i =0; $i<$count;$i++){
echo 
' Account Number: ' . $data[$i]['account_id'] . ', &nbsp Account Type: '. $data[$i]['type']  .'&nbsp
<input type="radio" name="which_acc" value="'.$data[$i]['account_id'].'--'.$data[$i]['type'].'"><br>';
}
 echo '
 <input type="hidden" name="outgoing" value="'.$_POST['outgoing'].'">
<input type="hidden" name="from_first_name" value="'.$_POST['from_first_name'].'">
<input type="hidden" name="from_last_name" value="'.$_POST['from_last_name'].'">
<input type="hidden" name="to_first_name" value="'.$_POST['to_first_name'].'">
<input type="hidden" name="to_last_name" value="'.$_POST['to_last_name'].'">
';
echo '<input type="submit" name="submit2" value="select2">';
echo '</p></form>';

FORM 1:

echo
'<form action="" method="POST">
Transfer:<input type="number" name="outgoing" value="';
if( isset( $_POST['outgoing'] ) && is_numeric( $_POST['outgoing'] ) ) echo $_POST['outgoing'];
echo '> € <br>
From: First Name: <input type="text" name="from_first_name" value="';
if( isset( $from_first_name ) ) echo $from_first_name;
echo '>Last Name: <input type="text" name="from_last_name" value="';
if( isset( $from_last_name ) ) echo $from_last_name;
echo '> </p> <p> <span style="font-size:150%;"> To: </span> First Name: <input type="text" name="to_first_name" value="';
if( isset( $to_first_name ) ) echo $to_first_name;
echo '>Last Name: <input type="text" name="to_last_name" value="';
if( isset( $to_last_name ) ) echo $to_last_name;
echo '>' . "\n";
echo '<p><input type="submit" name="submit1" value="Transfer Funds"></p>';
echo '</form>';

by submitting all the data from form 1 again in form 2 all the validation errors went away.

 

but next time I am going to try the conditional method --> it looks a lot cleaner and probably more secure?.

Link to comment
Share on other sites

I'm assuming that you have a two-part form, and some of the values from the first form must be used for the second form, yeah? Your solution of testing for true to submit1 and submit2 is perfectly fine. As for the logic for the forms, I'd do the following:

- If nothing is submitted, you should load an empty form 1.

- If form 1 is submitted with errors, then you should load a sticky form 1.

- If form 1 is submitted all okay, you should load an empty form 2 (perhaps with some stickiness from form 1).

- If form 2 is submitted with errors, you should load a sticky form 2.

 

That make sense?

Link to comment
Share on other sites

Actually, no information from form 1 is required a second time. This is why the hidden inputs method

is rather silly.

Yes, this makes sense. Yesterday, I was trying a combination of:

1. the conditional statements as above +

2. the logic on your line 3: If form 1 is submitted all okay, you should load an empty form 2 (No stickiness from form 1 actually needed).

 

So this morning, I just loathed my solution of throwing all the data back into hidden inputs. So I started again, fresh & this time it worked.

The general logic for the solution went as follows:

 if (submission = true ){
	 
	 if (users submits form 1) {
		 1. Validation exercises.
		       2. if (no errors) {
		              3. if (certain data from form 1 occurs), then
		                     create form 2 and present it to user for
                                     submission. 
		                     else {
								 just continue on.
							 }
	              }
                      2. else (if errors) {
                      Tell the user about the errors and force them to go back to a 
                       sticky form 1
                       }
	       } // end of form 1 submission
	 
	 if (user submits form 2) {
		 now the validation errors from form 1 should
		 not show up and we can validate form 2 happily.
	 } // end of form 2 submission
	 
 } // End of Main submission
 
 form 1:

So, onto the next step.

Thanks again.

:)

Link to comment
Share on other sites

Well played. And generally, I use flag variables to denote what part of the submission process I am in and whether everything was submitted properly or not. That way, I don't have to mix all my HTML markup with my validation logic.

Link to comment
Share on other sites

 Share

×
×
  • Create New...