Jump to content

Recommended Posts

Hey guys this php form works fine but what I need to do is have them put in their email address then put it in again and make sure they match? kind of like a registration form for password re enter password.

 

  <?php
          {
            $first_name = $_POST[ 'first_name' ];
            $middle_name = $_POST[ 'middle_name' ];
		$last_name = $_POST[ 'last_name' ];
		$phone = $_POST[ 'phone' ];
		$email = $_POST[ 'email' ];
            $query = "insert into club values ( '$id', '$first_name', '$middle_name', '$last_name', '$phone', '$email')";
            $results = mysql_query( $query )
            or printf( "Query error: %s", mysql_error() );
         }

         mysql_close();

      ?>
      <form action="<?php $PHP_SELF; ?>" method="POST" onsubmit="return validate_form(this)">
         <table width="361" border="0" cellspacing="0" cellpadding="5">
           <tr>
             <td>First Name *</td>
             <td><input type="text" name="first_name" /></td>
           </tr>
           <tr>
             <td>Middle Name</td>
             <td><input type="text" name="middle_name" /></td>
           </tr>
           <tr>
             <td>Last/Family/Surname *</td>
             <td><input type="text" name="last_name" /></td>
           </tr>
           <tr>
             <td>Phone (xxx-xxx-xxxx)</td>
             <td><input type="text" name="phone" /></td>
           </tr>
           <tr>
             <td>Email Address *</td>
             <td><input type="text" name="email" /></td>
           </tr>
           <tr>
             <td>Confirm Email *</td>
             <td><input type="text" name="emailconfirm" /></td>
           </tr>
         </table>
         <p><br />
          
           <input type="submit" />
         </p>
      </form>
   

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/
Share on other sites

[pre]Here is an example:

 

<?php

// VARIABLES
$first_name = $_POST[ 'first_name' ];
$middle_name = $_POST[ 'middle_name' ];
$last_name = $_POST[ 'last_name' ];
$phone = $_POST[ 'phone' ];
$email = $_POST[ 'email' ];
#email_confirm = $_POST['emailconfirm']; //CREATE THE VARIABLE
            
// VALIDATE EMAIL ADDRESS
if ($email == $email_confirm) {


// DATABASE QUERY
$query = "insert into club values ( '$id', '$first_name', '$middle_name', '$last_name', '$phone', '$email')";
$results = mysql_query( $query )
or printf( "Query error: %s", mysql_error() );


         mysql_close();

else {
echo 'Your email address was not confirmed, please try again.';
}

      ?>
      <form action="<?php $PHP_SELF; ?>" method="POST" onsubmit="return validate_form(this)">
         <table width="361" border="0" cellspacing="0" cellpadding="5">
           <tr>
             <td>First Name *</td>
             <td><input type="text" name="first_name" /></td>
           </tr>
           <tr>
             <td>Middle Name</td>
             <td><input type="text" name="middle_name" /></td>
           </tr>
           <tr>
             <td>Last/Family/Surname *</td>
             <td><input type="text" name="last_name" /></td>
           </tr>
           <tr>
             <td>Phone (xxx-xxx-xxxx)</td>
             <td><input type="text" name="phone" /></td>
           </tr>
           <tr>
             <td>Email Address *</td>
             <td><input type="text" name="email" /></td>
           </tr>
           <tr>
             <td>Confirm Email *</td>
             <td><input type="text" name="emailconfirm" /></td>
           </tr>
         </table>
         <p><br />
          
           <input type="submit" />
         </p>
      </form>

 

 

My only other concern is that in your current code you don't open the $dbc variable for the mySql.  I was thinking you left it off so we don't see it.  But do be sure you have it.  Also, using this validation you may want to think about adding a few more codes in to make your text fields sticky.  So in case the two don't match the user does not have to retype the WHOLE form over again.

[/pre]

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890799
Share on other sites

get a blank page

 

Well you need to add in a isset statement as well.  Basically, you need to tell the from since it is a self processing php that at open all variables are empty and thats okay, shoe the html.  Then when the user clicks submit that the form needs to process.  Add an isset statement for the basic html form to show with empty fields. 

 

Example:

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

ADD VARIABLES
$output_form = false;

/* POST Validity - Security */
if($_SERVER['REQUEST_METHOD'] != "POST"){
   		echo("Unauthorized attempt to access page.");
   		exit;
}

}	
  else {
    $output_form = true;
  }

ADD PROCESSING CODE AND VALIDATION CODE

if ($output_form == true) {
end php code and INPUT FORM

<?php
}
?>

 

This should point you in the right direction.  While you are debugging add this to the very top of the php scripting area.

 

<?php

  ini_set("display_errors", "1");
  error_reporting(E_ALL);

  session_start();
?>

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890810
Share on other sites

<?php
// first up there was no statement here ?
        if (isset($_POST['first_name'])){ // lets check if a value of the form is there
            $first_name = isset($_POST[ 'first_name' ])?mysql_real_escape_string($_POST[ 'first_name' ]):null; // repeat this for anything coming in from the form
		// it will prevent sql injection.
            $middle_name = $_POST[ 'middle_name' ]; // repeat here
		$last_name = $_POST[ 'last_name' ]; // and here
		$phone = $_POST[ 'phone' ]; // and here
		$email = $_POST[ 'email' ]; // and here

		if ($_POST['email'] == $_POST['emailconfirm']) { // the email fields checked out
			$query = "insert into club values ( '$id', '$first_name', '$middle_name', '$last_name', '$phone', '$email')";
			$results = mysql_query( $query )
			or printf( "Query error: %s", mysql_error() );	
		}else {
			// some type of error message generated
		}
         }
         // mysql_close(); // really not necessary unless you are working with persistent connections

      ?>
      <form action="<?php $PHP_SELF; ?>" method="POST" onsubmit="return validate_form(this)">
         <table width="361" border="0" cellspacing="0" cellpadding="5">
           <tr>
             <td>First Name *</td>
             <td><input type="text" name="first_name" /></td>
           </tr>
           <tr>
             <td>Middle Name</td>
             <td><input type="text" name="middle_name" /></td>
           </tr>
           <tr>
             <td>Last/Family/Surname *</td>
             <td><input type="text" name="last_name" /></td>
           </tr>
           <tr>
             <td>Phone (xxx-xxx-xxxx)</td>
             <td><input type="text" name="phone" /></td>
           </tr>
           <tr>
             <td>Email Address *</td>
             <td><input type="text" name="email" /></td>
           </tr>
           <tr>
             <td>Confirm Email *</td>
             <td><input type="text" name="emailconfirm" /></td>
           </tr>
         </table>
         <p><br />
          
           <input type="submit" value="Submit" />
         </p>
      </form>

 

Give that a try and see if it helps. I commented the code where I modified it.

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890811
Share on other sites

okay here is the entire code but it does not show error when emails are not the same

 

<?php

  ini_set("display_errors", "1");
  error_reporting(E_ALL);

  session_start();
?>

<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(first_name,"First Name must be filled out!")==false)
  {first_name.focus();return false;}
  }

}
</script>

      <?php
         $host = '';
         $user = '';
         $pw = '';
         $db = '';

         $link = mysql_connect( $host, $user, $pw )
            or die( 'Could not connect: ' . mysql_error() );
         
         mysql_select_db( $db );
      ?>


   <?php
// first up there was no statement here ?
        if (isset($_POST['first_name'])){ // lets check if a value of the form is there
            $first_name = isset($_POST[ 'first_name' ])?mysql_real_escape_string($_POST[ 'first_name' ]):null; // repeat this for anything coming in from the form
         // it will prevent sql injection.
            $middle_name = $_POST[ 'middle_name' ]; // repeat here
         $last_name = $_POST[ 'last_name' ]; // and here
         $phone = $_POST[ 'phone' ]; // and here
         $email = $_POST[ 'email' ]; // and here
         
         if ($_POST['email'] == $_POST['emailconfirm']) { // the email fields checked out
            $query = "insert into club values ( '$id', '$first_name', '$middle_name', '$last_name', '$phone', '$email')";
            $results = mysql_query( $query )
            or printf( "Query error: %s", mysql_error() );   
         }else {
            // some type of error message generated
         }
         }
         // mysql_close(); // really not necessary unless you are working with persistent connections
      
      ?>
      <form action="<?php $PHP_SELF; ?>" method="POST" onsubmit="return validate_form(this)">
         <table width="361" border="0" cellspacing="0" cellpadding="5">
           <tr>
             <td>First Name *</td>
             <td><input type="text" name="first_name" /></td>
           </tr>
           <tr>
             <td>Middle Name</td>
             <td><input type="text" name="middle_name" /></td>
           </tr>
           <tr>
             <td>Last/Family/Surname *</td>
             <td><input type="text" name="last_name" /></td>
           </tr>
           <tr>
             <td>Phone (xxx-xxx-xxxx)</td>
             <td><input type="text" name="phone" /></td>
           </tr>
           <tr>
             <td>Email Address *</td>
             <td><input type="text" name="email" /></td>
           </tr>
           <tr>
             <td>Confirm Email *</td>
             <td><input type="text" name="emailconfirm" /></td>
           </tr>
         </table>
         <p><br />
         
           <input type="submit" value="Submit" />
         </p>
      </form>

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890816
Share on other sites

           $query = "insert into club values (NULL, '$first_name', '$middle_name', '$last_name', '$phone', '$email')";

 

Then you do not need to but $id in there, as it does not have a value and causes issues as you noticed. Try putting NULL in the id spot. The auto_increment should pick it up and work just fine.

 

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890848
Share on other sites

better way to do it then with javascript?

 

PHP would be better as it cannot be disabled.

 

  if (!isset($_POST['first_name']) || empty($_POST['first_name'])) {
                 echo 'First name is required.';
                 exit; 
         }          

         $first_name = isset($_POST[ 'first_name' ])?mysql_real_escape_string($_POST[ 'first_name' ]):null; // repeat this for anything coming in from the form
         // it will prevent sql injection.
            $middle_name = $_POST[ 'middle_name' ]; // repeat here
         $last_name = $_POST[ 'last_name' ]; // and here
         $phone = $_POST[ 'phone' ]; // and here
         $email = $_POST[ 'email' ]; // and here

         

 

The checks can be done a bit nicer with nicer error messages and without exiting the script. That is just a rough example of one way it could be done.

Link to comment
https://forums.phpfreaks.com/topic/168836-two-fields-same/#findComment-890858
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.