using 2 forms on one page

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

designaireAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
'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
Ray PaseurCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
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
Dave BaldwinFixer of ProblemsCommented:
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
Ray PaseurCommented:
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
designaireAuthor Commented:
Thank you, I'm sure one of these solutions will work. Just got distracted with something else.
0
Dave BaldwinFixer of ProblemsCommented:
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
Ray PaseurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.