Jump to content

Exciting code when form is submitted.


bobby317

Recommended Posts

I am working on an event calendar and right now I am trying to create the registration form. I am going to use php to validate the form and also to send the form info to my data base.  The problem I am having now is when you first go to my page it is trying to validate the form and I only want the code to run when the form is submitted.  I think I need to create a function with my code and then have it run when submitted but I can’t seem to figure it out.

 

Here is what I have so far. I have marked out some things as I work through it. Also could you please expain what and why you are doing what you are doing so I have a better understanding thank.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Register</title>

 

<style type="text/css">

 

body {

text-align: center;

}

 

form {

text-align: left;

}

 

label

{

width: 6em;

float: left;

text-align: right;

margin-right: 0.5em;

 

}

 

.submit input {

margin-left: 6.5em;

}

 

.error {

color: #F00;

}

 

</style>

 

</head>

 

<body>

<?php

 

//connects to database

//include 'config.php';

//include 'opendb.php';

 

//Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash

//$email1 = mysql_real_escape_string($_POST['email1']);

//$email2 = mysql_real_escape_string($_POST['email2']);

//$password1 = md5($_POST['pass1']);

//$password2 = md5($_POST['pass2']);

 

//create error message for blank fields

$errorBlank = "Please fill out all fields!";

//check if email1 is blank and matches email 2

if (empty($_POST['email1'])) {

echo "<p class=\"error\">$errorBlank</p>";

} else {

$email1 = mysql_real_escape_string($_POST['email1']);

}

 

?>

 

<form name="register" action="register.php" method="post">

   

    <p>

        <label for="email">Email Adress:</label>

        <input type="text" name="email" maxlength="30" />

        </p>

       

        <p>

        <label for="email2">Email Adress Again:</label>

        <input type="text" name="email2" maxlength="30" />

        </p>

       

        <p>

        <label for"password">Password:</label>

        <input type="password" name="pass1"  />

        </p>

       

        <p>

        <label for"password2">Password Again:</label>

        <input type="password" name="pass2"  />

        </p>

       

        <p class="submit">

        <input type="submit" value="Register" />

        </p>

   

    </form>

 

</body>

</html>

 

 

Link to comment
Share on other sites

give this a shot:

<?php
if(isset ($submit)){

//connects to database
include 'config.php';
include 'opendb.php';

//Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash
$email1 = mysql_real_escape_string($_POST['email1']);
$email2 = mysql_real_escape_string($_POST['email2']);
$password1 = md5($_POST['pass1']);
$password2 = md5($_POST['pass2']);

//create error message for blank fields
$errorBlank = "Please fill out all fields!";
//check if email1 is blank and matches email 2
if (empty($_POST['email1'])) {
   echo "<p class=\"error\">$errorBlank</p>";
} else {
   $email1 = mysql_real_escape_string($_POST['email1']);
}
}
?>

   <form name="register" action="PHP_SELF?$submit='true'" method="post">

Link to comment
Share on other sites

Muddy_Funster, just posting 'fixed' code without stating what you fixed in it or why you changed it usually does not help anyone. Also, $submit won't exist so the 'fixed' code will skip over all the form processing code anyway.

Link to comment
Share on other sites

ok, change the form to:

<form method="post" action="register.php?$submit='true'" type="submit">

and see if that helps.  And just to keep everyone happy here, I was going to wait untill I knew it worked before going into the mechanics of it but basicly I am atempting to alter the code so that, the first time it is loaded the $submit variable is, as PFM.. rightly said, not set and so the validation code is skiped, however, click the button on the form and it reloads the page at the same time assigning the value of 'true' to the $submit variable which will then be registered and run the validation code.  I just preffer to have a solution before I explain how it fixes things...just in case it doesn't.

Link to comment
Share on other sites

Ok now that I have changed the action I dont get the http 404 error but if I submit the form with email1 empty I dont get my error message just the for shows back up. 

 

Here is what my code looks like now if that helps.  Thanks for everything.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Register</title>

 

<style type="text/css">

 

body {

text-align: center;

}

 

form {

text-align: left;

}

 

label

{

width: 6em;

float: left;

text-align: right;

margin-right: 0.5em;

 

}

 

.submit input {

margin-left: 6.5em;

}

 

.error {

color: #F00;

}

 

</style>

 

</head>

 

<body>

 

<?php

if(isset ($submit)){

 

//connects to database

//include 'config.php';

//include 'opendb.php';

 

//Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash

//$email1 = mysql_real_escape_string($_POST['email1']);

//$email2 = mysql_real_escape_string($_POST['email2']);

//$password1 = md5($_POST['pass1']);

//$password2 = md5($_POST['pass2']);

 

//create error message for blank fields

$errorBlank = "Please fill out all fields!";

 

//check if email1 is blank and matches email 2

if (empty($_POST['email1'])) { 

echo "<p class=\"error\">$errorBlank</p>";

} else { 

$email1 = mysql_real_escape_string($_POST['email1']);

}

}

?>

 

<form name="register" action="register.php?$submit='true'" type="submit" method="post">

   

    <p>

        <label for="email">Email Adress:</label>

        <input type="text" name="email" maxlength="30" />

        </p>

       

        <p>

        <label for="email2">Email Adress Again:</label>

        <input type="text" name="email2" maxlength="30" />

        </p>

       

        <p>

        <label for"password">Password:</label>

        <input type="password" name="pass1"  />

        </p>

       

        <p>

        <label for"password2">Password Again:</label>

        <input type="password" name="pass2"  />

        </p>

       

        <p class="submit">

        <input type="submit" value="Register" />

        </p>

   

    </form>

 

</body>

</html>

Link to comment
Share on other sites

ok, fist let's tidy up the echo line...

  if (empty($_POST['email1'])) { 
      echo '<p class="error">'.$errorBlank.'</p>';
   } else {   
   $email1 = mysql_real_escape_string($_POST['email1']);
   }
}

Now look at your form input for the email fields.  On the form the names are "email" and "email2", you are POSTing "email1" instead of "email".  Fix this to your preference and give it another shot.

Link to comment
Share on other sites

Ok I made changes and double and triple checked them and still just reloads form and no error when email1 is blank.  Also could you tell me why my echo line was wrong why use singe parenthesis instead of double and also why the dots b4 and after the variable? Thanks for everything. And here is my updated code again in case I missed something.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Register</title>

 

<style type="text/css">

 

body {

text-align: center;

}

 

form {

text-align: left;

}

 

label

{

width: 6em;

float: left;

text-align: right;

margin-right: 0.5em;

 

}

 

.submit input {

margin-left: 6.5em;

}

 

.error {

color: #F00;

}

 

</style>

 

</head>

 

<body>

 

<?php

if(isset ($submit)){

 

//connects to database

//include 'config.php';

//include 'opendb.php';

 

//Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash

//$email1 = mysql_real_escape_string($_POST['email1']);

//$email2 = mysql_real_escape_string($_POST['email2']);

//$password1 = md5($_POST['pass1']);

//$password2 = md5($_POST['pass2']);

 

//create error message for blank fields

$errorBlank = "Please fill out all fields!";

 

//check if email1 is blank and matches email 2

if (empty($_POST['email1'])) { 

echo '<p class="error">'.$errorBlank.'</p>';

} else { 

$email1 = mysql_real_escape_string($_POST['email1']);

}

}

?>

 

<form name="register" action="register.php?$submit='true'" type="submit" method="post">

   

    <p>

        <label for="email1">Email Adress:</label>

        <input type="text" name="email1" maxlength="30" />

        </p>

       

        <p>

        <label for="email2">Email Adress Again:</label>

        <input type="text" name="email2" maxlength="30" />

        </p>

       

        <p>

        <label for"password">Password:</label>

        <input type="password" name="pass1"  />

        </p>

       

        <p>

        <label for"password2">Password Again:</label>

        <input type="password" name="pass2"  />

        </p>

       

        <p class="submit">

        <input type="submit" value="Register" />

        </p>

   

    </form>

 

</body>

</html>

Link to comment
Share on other sites

Ok I got it wohoo!!!!  I used a hidden field to pass the value. Thanks for all the help with you pointing me in the right direction I got it. Thanks again..

 

Here is the code that did it...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Register</title>

 

<style type="text/css">

 

body {

text-align: center;

}

 

form {

text-align: left;

}

 

label

{

width: 6em;

float: left;

text-align: right;

margin-right: 0.5em;

 

}

 

.submit input {

margin-left: 6.5em;

}

 

.error {

color: #F00;

}

 

</style>

 

</head>

 

<body>

 

<?php

 

$submit = ($_POST['submit']);

 

if($submit == true ){

 

//connects to database

//include 'config.php';

//include 'opendb.php';

 

//Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash

//$email1 = mysql_real_escape_string($_POST['email1']);

//$email2 = mysql_real_escape_string($_POST['email2']);

//$password1 = md5($_POST['pass1']);

//$password2 = md5($_POST['pass2']);

 

//create error message for blank fields

$errorBlank = "Please fill out all fields!";

 

//check if email1 is blank and matches email 2

if (empty($_POST['email1'])) { 

echo '<p class="error">'.$errorBlank.'</p>';

} else { 

$email1 = mysql_real_escape_string($_POST['email1']);

}

}

?>

 

<form name="register" action="register.php" method="post">

   

    <p>

        <label for="email1">Email Adress:</label>

        <input type="text" name="email1" maxlength="30" />

        </p>

       

        <p>

        <label for="email2">Email Adress Again:</label>

        <input type="text" name="email2" maxlength="30" />

        </p>

       

        <p>

        <label for"password">Password:</label>

        <input type="password" name="pass1"  />

        </p>

       

        <p>

        <label for"password2">Password Again:</label>

        <input type="password" name="pass2"  />

        </p>

       

        <p class="submit">

        <input type="submit" value="Register" />

        <input type="hidden" name="submit" value="true" />

        </p>

   

    </form>

 

</body>

</html>

Link to comment
Share on other sites

Or you can just do this -

Change your <form tag back to what it originally was -

<form name="register" action="register.php" method="post">

 

Add a name="submit" attribute to your submit button.

 

Change if(isset ($submit)){ to -

 

if(isset ($_POST['submit'])){

Link to comment
Share on other sites

With the echo line, enclosing the main part of the text in the single quote allowed the native double quotes to be echoed for the html tag.  the dots before and after the variable are links.  They tell the echo line to add what's after the dot to the output as well.  While you can echo "this $var" it is better to echo "this ".$var  I'm going to come at this from a different angle and see what we get.

<?php
if(isset ($_POST['submit'])){
...
?>
   <form name="register" action="register.php?submit='true'" type="submit" method="post">

ahh well, even better,  you got it yourself.  Nice one good luck with the rest.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.