Solved

using 2 forms on one page

Posted on 2014-09-08
9
205 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 82

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 108

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
 
LVL 108

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 82

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 108

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 82

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 108

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now