Solved

using 2 forms on one page

Posted on 2014-09-08
9
211 Views
Last Modified: 2014-09-10
I'm using 2 forms on one page. All the code seems to be working except when I hit the button without entering data, I get error messages.  This code: if (isset($_POST['submit'])=="Register") doesn't seem to be working. It's looking for the form field names and I get an error - Undefined index: email etc...
$username="";
$password="";
$email="";
$pass="";
$userConfirm="";
$passConfirm="";
		
if (isset($_POST['submit'])=="Register"){
	$email = $_POST["email"];
	$pass = $_POST["pass"];
	$userConfirm = $_POST["userconfirm"];
	$passConfirm = $_POST["passconfirm"];
}

/*enter information*/
  	if (isset($_POST['submit'])=="Sign In"){
	$username = trim($_POST["username"]);
	$password = trim($_POST["password"]);	
}
 

           <form name="form1" method="post" action="page.php">
           <input name="username" type="text" id="email" size="35">
           <input name="password" type="password" id="password">           	
          <input type="submit" name="submit" alt="submits" value="Sign In" class="submit">
   	   </form>

         <form name="form2" method="post" action="page.php">     
          <input name="email" type="text" id="email" value="email" >      
           <input name="userconfirm" type="text" id="password" value="confirm email">  
           <input name="pass" type="password" id="email" size="35" value="password">      
           <input name="passconfirm" type="password" id="password" value="confirm password">
	<input type="submit" name="submit" value="Register" class="submit">
	</form>

Open in new window

0
Comment
Question by:designaire
9 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 40311313
It looks that it's working properly.
if (isset($_POST['submit'])=="Register") means: if the submit button was clicked/pushed on the Register form.
Even if you "hit the button without entering data", you still hit the button, so that evaluates to true.

You'll need to check if the other fields are not empty:
if(isset( $_POST["email"])) $email = $_POST["email"];
and so on.

Or, a bit more compact:
$email = (isset($_POST['email'])) ? $_POST['email'] : '';

HTH,
Dan
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 40311478
'form2' has some problems.  'id's should be unique and you have duplicated some and used them in odd places too.  Maybe something like this.
<form name="form2" method="post" action="page.php">    
          <input name="email" type="text" id="email" value="email" >      
           <input name="userconfirm" type="text" id="email2" value="confirm email">  
           <input name="pass" type="password" id="password" size="35" value="password">      
           <input name="passconfirm" type="password" id="password2" value="confirm password">
      <input type="submit" name="submit" value="Register" class="submit">
      </form> 

Open in new window

In addition, 'isset' only returns a 'boolean' and can't be used like you are using it.  Maybe try this using two separate checks.
if ((isset($_POST['submit']) && ($_POST['submit']=="Register")){

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40311667
Learning HTML:
https://developer.mozilla.org/en-US/learn/html
http://www.w3schools.com/html/
http://www.codecademy.com/en/tracks/web

Learning PHP and HTML forms:
http://www.php.net/manual/en/tutorial.forms.php

Example of two forms on one page:
http://iconoun.com/demo/two_forms.php
<?php // demo/two_forms.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514198.html
// ACTION SCRIPT PROCESS A REQUEST FROM A WEB PAGE WITH TWO FORMS

// SHOW THE REQUEST DATA, IF ANY
if (!empty($_POST)) var_dump($_POST);

// CREATE THE HTML DOCUMENT WITH TWO FORMS
$htm = <<<EOD
<p>
My Form
<form method="post">
Enter something:
<input name="myData" />
<input name="mySignal" type="submit" value="Go!" />
</form>
</p>

<p>
My Other Form
<form method="post">
Enter another thing:
<input name="myOtherData" />
<input name="myOtherSignal" type="submit" value="Go!" />
</form>
</p>
EOD;

// WRITE THE HTML DOCUMENT
echo $htm;

Open in new window

The principle being illustrated here is the "meets-min" example showing how to distinguish between the data from one form vs the data from another form.  The input names are array keys in the $_POST array.  Since the submit control has a different name in each of the forms, you can test the name of the submit button to see which form was posted.  You can also test the names in the other input controls.  So long as one of the names gives you a clear signal of which form was used, your program logic can work correctly.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40311678
Here's another example.  In this example the two forms have the same input control names, but different submit control names.

<?php // demo/temp_designaire.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514198.html
// ACTION SCRIPT PROCESS A REQUEST FROM A WEB PAGE WITH TWO FORMS

// EVALUATE THE REQUEST DATA, IF ANY
if (!empty($_POST))
{
    // DETERMINE WHICH FORM WAS POSTED
    if (!empty($_POST['mySignal']))
    {
        echo '<br>YOU POSTED THE FIRST FORM WITH THIS: ';
    }
    elseif (!empty($_POST['myOtherSignal']))
    {
        echo '<br>YOU POSTED THE SECOND FORM WITH THIS: ';
    }
    else
    {
        trigger_error("INVALID REQUEST", E_USER_ERROR);
    }

    // SHOW THE DATA IN THE REQUEST
    echo '<br>' . htmlentities($_POST['myData']);

}

// CREATE THE HTML DOCUMENT WITH TWO FORMS
$htm = <<<EOD
<p>
My Form
<form method="post">
Enter something:
<input name="myData" />
<input name="mySignal" type="submit" value="Go!" />
</form>
</p>

<p>
My Other Form
<form method="post">
Enter another thing:
<input name="myData" />
<input name="myOtherSignal" type="submit" value="Go!" />
</form>
</p>
EOD;

// WRITE THE HTML DOCUMENT
echo $htm;

Open in new window

HTH, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40312777
Ray, as long as they do not have separate names in the <form>, any submit will submit ALL of the <input>s from both forms.  If you have separate names like <form name="form2"... , the submit button in that section will submit Only the items in that particular form.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40312974
Dave: I'm not finding that.  Here is the link and the script.  Tested in all major browsers.  Try it yourself and follow the undefined index messages
http://iconoun.com/demo/temp_designaire.php

<?php // demo/temp_designaire.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514198.html
// ACTION SCRIPT PROCESS A REQUEST FROM A WEB PAGE WITH TWO FORMS

// EVALUATE THE REQUEST DATA, IF ANY
if (!empty($_POST))
{
    // DETERMINE WHICH FORM WAS POSTED
    if (!empty($_POST['mySignal']))
    {
        echo '<br>YOU POSTED THE FIRST FORM WITH THIS: ';
    }
    elseif (!empty($_POST['myOtherSignal']))
    {
        echo '<br>YOU POSTED THE SECOND FORM WITH THIS: ';
    }
    else
    {
        trigger_error("INVALID REQUEST", E_USER_ERROR);
    }

    // SHOW THE DATA IN THE REQUEST
    echo '<br>' . htmlentities($_POST['myData1']);
    echo '<br>' . htmlentities($_POST['myData2']);
}

// CREATE THE HTML DOCUMENT WITH TWO FORMS
$htm = <<<EOD
<p>
My Form
<form method="post">
Enter something:
<input name="myData1" />
<input name="mySignal" type="submit" value="Go!" />
</form>
</p>

<p>
My Other Form
<form method="post">
Enter another thing:
<input name="myData2" />
<input name="myOtherSignal" type="submit" value="Go!" />
</form>
</p>
EOD;

// WRITE THE HTML DOCUMENT
echo $htm;

Open in new window

0
 

Author Comment

by:designaire
ID: 40312988
Thank you, I'm sure one of these solutions will work. Just got distracted with something else.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40313175
Ray, your form seems to be working as you say it should.  I will still strongly recommend that each form have a 'name' attribute because I have seen two forms on a page get confused.  If you have a newsletter signup on the same page as an order entry form, you really don't want them getting confused.

It could be when I used 'document.forms.form1....' in javascript that I had a problem because javascript won't know what to do without a form name when there are two or more forms on a page.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40313221
javascript won't know what to do without a form name
Yeah, that makes more sense now.  With jQuery it's a bit easier (at least the syntax is more coherent than plain old JavaScript), but a distinguishing attribute is still needed.  Typically you would use an id= attribute as a selector to identify the form.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question