Solved

using 2 forms on one page

Posted on 2014-09-08
9
215 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
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…

821 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