Jump to content

identical fields in php


ilikephp

Recommended Posts

Hello, my code is listed below and it is working properly, but how can I force the 2 fields of password to be identical?

 

Thanks in advance...

<?php
if (array_key_exists('ewComments', $_POST)) {
  // mail processing script
  // remove escape characters from POST array
if (get_magic_quotes_gpc()) {
  function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    return $value;
    }
  $_POST = array_map('stripslashes_deep', $_POST);
  }
  // validate the input, beginning with name
  $name = trim($_POST['name']);
  if (empty($name)) {
    $error['name'] = '* required';
    } 
$email = $_POST['email'];
  // check for valid email address
  $pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
  if (!preg_match($pattern, trim($email))) {
    $error['email'] = '* required';
    }

$password = trim($_POST['password']);
  if (empty($password)) {
    $error['password'] = '* required';
    } 

  }
?>
<!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=iso-8859-1" />
<title>Registration Form</title>
<?php include('styles/style_rules.php'); ?>
<script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
//-->
</script>
</head>

<body>
<div id="wrapper">
  <div id="maincontent">
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm" id="contactForm">
      <p>       </p>
      <table width="507" border="1" bgcolor="#cccccc">
        <tr>
          <td width="135">Full Name:</td>
          <td width="356"><label for="name"></label>            <span class="warning">
            <input type="text" name="name" id="name"
	<?php if(isset($error)) {echo "value='$name'";} ?> />
          </span><span class="warning">
          <?php if (isset($error['name'])) { ?>
              <?php echo $error['name']; ?>
              <?php } ?>
          </span><br /></td>
        </tr>
        <tr>
          <td>Address</td>
          <td><label>
            <textarea name="textarea" id="textarea" cols="25" rows="4"></textarea>
          </label></td>
        </tr>
        <tr>
          <td>Email</td>
          <td><span class="warning">
            <input type="text" name="email" id="email"
	<?php if(isset($error)) {echo "value='$email'";} ?> />
            <?php if (isset($error['email'])) { ?>
            <?php echo $error['email']; ?>
            <?php } ?>
          </span></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><label><span class="warning">
            <input type="text" name="password" id="password"
	<?php if(isset($error)) {echo "value='$password'";} ?> />
            <?php if (isset($error['password'])) { ?>
            <?php echo $error['password']; ?>
            <?php } ?>
          </span></label></td>
        </tr>
        <tr>
          <td>Confirm Password</td>
          <td><span class="warning">
            <input type="text" name="password" id="password"
	<?php if(isset($error)) {echo "value='$password'";} ?> />
            <?php if (isset($error['password'])) { ?>
            <?php echo $error['password']; ?>
            <?php } ?>
          </span></td>
        </tr>
      </table>
      <p> </p>
      <p>
        <label for="email"></label>
        <br />
      </p>
      <p>
        <input name="ewComments" type="submit" id="ewComments" value="Send comments" />
      </p>
    </form>
  </div>

</div>
</body>
</html>

 

EDITED BY WILDTEEN88: Please wrap code within code tags (


)

 

Link to comment
https://forums.phpfreaks.com/topic/157582-identical-fields-in-php/
Share on other sites

you need to give the second input a different name, so instead of name="password" use name="password2"

 

Then when you're checking the form

 

$password = trim($_POST['password']);

$password2 = trim($_POST['password2']);

if($password != $password2){

//set an error

}

  if (empty($password)) {

    $error['password'] = '* required';

    }

 

  }

I found it:

it should be:

 

  <input type="text" name="password2" id="password2"

<?php if(isset($error)) {echo "value='$password2'";} ?> />

            <?php if (isset($error['password'])) { ?>

            <?php echo $error['password']; ?>

            <?php } ?>

 

 

this is the check box that I have, but how can it be validated please?

 

<input type="checkbox" name="accept" id="accept"
	<?php if(isset($error)) {echo "value='$accept'";} ?> />
          </span><span class="warning">
          <?php if (isset($error['accept'])) { ?>
          <?php echo $error['accept']; ?>
          <?php } ?>

 

 

 

checking the form:

  $accept = trim($_POST['accept']);
  if (empty($accept)) {
    $error['accept'] = '* required';
    } 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.