?
Solved

using 2 forms on one page

Posted on 2014-09-08
9
Medium Priority
?
247 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 35

Accepted Solution

by:
Dan Craciun earned 1000 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 111

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

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 84

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 111

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 84

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 111

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

612 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