List:General Discussion« Previous MessageNext Message »
From:fbsd Date:April 20 2006 11:15pm
Subject:RE: Form value editing
View as plain text  


can someone please just send me or post a example of editing feilds
of a
mysql database within an html form and then updating the values
thanks alot


<?php   // start php code

//  main logic
if (isset($_POST['submit']))
  {
   process_form();
  }
 else
  {
   show_form($errors,$data);
  }

// define show_form code
function show_form($errors='',$data='')
{
  // the following html code is inplied to belong to the show_form
function
php?>

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>learning PHP </title>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<link rel="stylesheet" type="text/css"
href="00.00-web_style_sheet.css">
</head>
<body>
<?php include("00.00_Header.php"); ?>
<font FACE="Verdana" SIZE="2">

<?php
// This is the location in the web page where the error msgs will
display if there was
// any bad data entered in the form which the process_form function
found

  if ($errors != '')
    { foreach($errors as $msg)
      { echo $msg .'<br />';}
    }


// This checks if the form has been processed all ready and the
inputed data has
// been passed to the show_form function for re-display.
// The $data[names] come from the 'name=' fields of the form
definition.

  if($data != '')
  {
    $logonid      = $data['id'];
    $logonpw      = $data['pw'];
    $verifypw     = $data['pw2'];
    $email        = $data['email'];
    $firstname    = $data['firstname'];
    $lastname     = $data['lastname'];
    $addr1        = $data['address1'];
    $addr2        = $data['address2'];
    $city         = $data['city'];
    $state        = $data['state'];
    $zip          = $data['zip'];
    $phone_home   = $data['phone_home'];
    $phone_office = $data['phone_office'];
    $phone_cell   = $data['phone_cell'];
    $mothers_maiden_name = $data['mothers_maiden_name'];
  }
php?>

<!-- Form inside of table so it looks nice on page -->
<!-- Notice the php code in the value= tag that is what reshows the
data on bad input data -->
<!-- Notice the TITLE= tag that shows when the user moves the mouse
over the form input field -->

<form method="POST" action="<?php echo($PHP_SELF); php?>">

<table ALIGN="center" border="1" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#111111" width="85%">
   <tr><td width="100%"><p align="center"><b><font
size="2">Registration Information</font></b></td></tr>
   <tr><td width="100%"><font size="2">&nbsp;Items marked with * are
required.</font></td></tr>
   <tr><td width="100%"><font size="2">&nbsp;Mouse over input fields
for details.</font></td></tr>
</table>

<table ALIGN="center" border="1" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#111111" width="85%">

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Logon ID:
*</font></td>
      <td width="62%"><input type="text" name="id" size="30"
maxlength="15"
          value="<?php echo $logonid;?>" TITLE="Enter 7 to 15
Alpha-Numeric characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Logon
Password: *</td>
      <td width="62%"><input type="password" name="pw" size="30"
maxlength="15"
          value="<?php echo $logonpw;?>" TITLE="Enter 6 to 15
Alpha-Numeric characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Verify
Password: *</td>
      <td width="62%"><input type="password" name="pw2" size="30"
maxlength="15"
          value="<?php echo $verifypw;?>" TITLE="Enter 6 to 15
Alpha-Numeric characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Email
Address: *</td>
      <td width="62%"><input type="text" name="email" size="30"
maxlength="45"
          value="<?php echo $email;?>" TITLE="Enter up to 45
characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;First
Name: *</td>
      <td width="62%"><input type="text" name="firstname" size="30"
maxlength="30"
          value="<?php echo $firstname;?>" TITLE="Enter uo to 30
Alphabetical characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Last Name:
*</td>
      <td width="62%"><input type="text" name="lastname" size="30"
maxlength="30"
          value="<?php echo $lastname;?>" TITLE="Enter up to 30
Alphabetical characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Address
Line 1: *</td>
      <td width="62%"><input type="text" name="address1" size="30"
maxlength="30"
          value="<?php echo $addr1;?>" TITLE="Enter up to 30
Alpha-Numeric characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Address
Line 2:</td>
      <td width="62%"><input type="text" name="address2" size="30"
maxlength="30"
          value="<?php echo $addr2;?>" TITLE="Enter up to 30
Alpha-Numeric characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;City:
*</td>
      <td width="62%"><input type="text" name="city" size="30"
maxlength="20"
          value="<?php echo $city;?>" TITLE="Enter up to 20
Alphabetical characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana"
SIZE="2">&nbsp;State/Province: *</td>
      <td width="62%"><input type="text" name="state" size="30"
maxlength="20"
          value="<?php echo $state;?>" TITLE="Enter up to 20
Alphabetical characters"></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Zip/Postal
code: *</td>
      <td width="62%"><input type="text" name="zip" size="30"
maxlength="15"
          value="<?php echo $zip;?>" TITLE="Enter up to 15 Numeric
characters."></font></td></tr>

  <tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Home
Phone: *</td>
      <td width="62%"><input type="text" name="phone_home" size="30"
maxlength="15"
          value="<?php echo $phone_home;?>" TITLE="Enter Numeric
characters. Including area code."></font></td></tr>

<tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Office
Phone: </td>
      <td width="62%"><input type="text" name="phone_office"
size="30" maxlength="15"
          value="<?php echo $phone_office;?>" TITLE="Enter Numeric
characters. Including area code."></font></td></tr>

<tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Mobile
Phone: </td>
      <td width="62%"><input type="text" name="phone_cell" size="30"
maxlength="15"
          value="<?php echo $phone_cell;?>" TITLE="Enter Numeric
characters. Including area code."></font></td></tr>

<tr><td width="38%"><font FACE="Verdana" SIZE="2">&nbsp;Mothers
Maiden name: * </td>
      <td width="62%"><input type="text" name="mothers_maiden_name"
size="30" maxlength="30"
          value="<?php echo $mothers_maiden_name;?>" TITLE="Enter uo
to 30 Alphabetical characters">
          <font FACE="Verdana" SIZE="1">This is your secret
identification challenge code.</font></td></tr


<!-- This is the captcha Security Code Random-Noisy-Image  -->
  <tr><td width="38%">&nbsp;<font FACE="Verdana" SIZE="1"><img
width=120 height=30 src="button.php" border="1"> Security Code</td>
      <td width="62%"><input MAXLENGTH=5 SIZE=5 name="userdigit"
type="text"  value="">
      <font FACE="Verdana" SIZE="1">If you can not read it, submit
to get new one.</font></td></tr
</table>
<table ALIGN="center" border="1" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#111111" width="85%">
   <tr><td width="100%"><p align="center"><input type="submit"
value="Submit" name="submit"></td></tr>

</table>
</form>
</font>
<p>&nbsp;</p>
<?php

} // end of show_form


// define process_form function
function process_form()
{
  // for security purposes define all variables with default values
  $logonid       = '';
  $logonpw       = '';
  $verifypw      = '';
  $email         = '';
  $firstname     = '';
  $lastname      = '';
  $addr1         = '';
  $addr2         = '';
  $city          = '';
  $state         = '';
  $zip           = '';
  $phone_home    = '';
  $phone_office  = '';
  $phone_cell    = '';
  $mothers_maiden_name = '';

  $errors = array();         // used to hold error message from
editing each form input field


 // populate variables from the form input fields
  $logonid       = $_POST['id'];
  $logonpw       = $_POST['pw'];
  $verifypw      = $_POST['pw2'];
  $email         = $_POST['email'];
  $firstname     = $_POST['firstname'];
  $lastname      = $_POST['lastname'];
  $addr1         = $_POST['address1'];
  $addr2         = $_POST['address2'];
  $city          = $_POST['city'];
  $state         = $_POST['state'];
  $zip           = $_POST['zip'];
  $phone_home    = $_POST['phone_home'];
  $phone_office  = $_POST['phone_office'];
  $phone_cell    = $_POST['phone_cell'];
  $mothers_maiden_name = $_POST['mothers_maiden_name'];


// Check the data the user entered to validate if its acceptable
section follows.
// The starting and ending position is necessary for class tests
'digit. alpha, alnum' to work.

$len = "0,15";
 if (empty ($logonid))
     { $errors[] = "<font color=red>You didn't enter a Logon
ID.</font>"; }
   elseif (!ereg("^[[:alnum:]]{{$len}}$", $logonid))
     { $errors[] = "<font color=red>Logon ID: Can only contain
Alpha-Numeric characters.</font>"; }
     elseif ((strlen($logonid)<7))
     	 { $errors[] = "<font color=red>Logon ID: Is the wrong length!
It must be minimum of
            7 characters long.</font>"; }

 if (empty ($logonpw))
     { $errors[] = "<font color=red>You didn't enter a
password.</font>"; }
   elseif ((strlen($logonpw)<6) || (strlen($logonpw)>15))
      { $errors[] = "<font color=red>Logon Password is of the wrong
length! It must be
         between 6 - 15 characters long.</font>"; }
     elseif (!ereg("^[[:alnum:]]{{$len}}$", $logonpw))
     { $errors[] = "<font color=red>Logon Password: Can only contain
Alpha-Numeric characters.</font>"; }
       elseif($logonpw != $verifypw)
       { $errors[] = "<font color=red>Your password & verify
password do not match.</font>"; }

$len = "0,45";
if (empty ($email))
     { $errors[] = "<font color=red>You didn't enter a Email
Address.</font>"; }
    elseif (!ereg("^([[:alnum:]\.\_\-]+)(\@[[:alnum:]\.\-]+\.+)",
$email))
       { $errors[] = "<font color=red>Email Address format is
invalid. It must be
         this format name@stripped.</font>";
         $errors[] = "<font color=red>Alpha-Numeric characters
plus - and . and _
          and @ are valid in the address.</font>"; }
      else
        { // good email address so far now check for good dns mx for
domain name
          list($Username, $Domain) = split("@",$email);
          if(!getmxrr($Domain, $MXHost))
             { $errors[] = "<font color=red>Email Address Domain
name is invalid because
               it has no DNS mx records.</font>";}
         }

 $len = "0,30";
 if (empty ($firstname))
     { $errors[] = "<font color=red>You didn't enter a First
Name.</font>"; }
   elseif (!ereg("^[[:alpha:]]{{$len}}$", $firstname))
       { $errors[] = "<font color=red>First Name: Can only contain
Upper & Lower case
         Alphabetical characters.</font>"; }


 if (empty ($lastname))
     { $errors[] = "<font color=red>You didn't enter a Last
Name.</font>"; }
   elseif (!ereg("^[[:alpha:]]{{$len}}$", $lastname))
       { $errors[] = "<font color=red>Last Name: Can only contain
Upper & Lower case
         Alphabetical characters.</font>"; }


 if (empty ($addr1))
     { $errors[] = "<font color=red>You didn't enter a Address line
1.</font>"; }
   elseif (!ereg("^[[:alnum:]\ \-]{{$len}}$", $addr1))
     { $errors[] = "<font color=red>Address 1: Can only contain
Alpha-Numeric characters.</font>"; }

 if ((!empty ($addr2)) && (!ereg("^[[:alnum:]\ \-]{{$len}}$",
$addr2)))
     { $errors[] = "<font color=red>Address Line 2: Can only contain
Alpha-Numeric characters.</font>"; }

$len = "0,20";
 if (empty ($city))
     { $errors[] = "<font color=red>You didn't enter a
City.</font>"; }
   elseif (!ereg("^[[:alpha:]\ \-]{{$len}}$", $city))
       { $errors[] = "<font color=red>City: Can only contain Upper &
Lower case
         Alphabetical characters.</font>"; }

 if (empty ($state))
     { $errors[] = "<font color=red>You didn't enter a
State.</font>"; }
   elseif (!ereg("^[[:alpha:]\ \-]{{$len}}$", $state))
       { $errors[] = "<font color=red>City: Can only contain Upper &
Lower case
         Alphabetical characters.</font>"; }

 $len = "0,15";
 if (empty ($zip))
     { $errors[] = "<font color=red>You didn't enter a
Zip.</font>"; }
   elseif (!ereg("^[[:digit:]\-]{{$len}}$", $zip))
       { $errors[] = "<font color=red>Zip: Can only contain Numeric
characters, plus dash.</font>"; }

  if (empty ($phone_home))
     { $errors[] = "<font color=red>You didn't enter a Home Phone
Number.</font>"; }
   elseif (!ereg("^[[:digit:]\-]{{$len}}$", $phone_home))
     { $errors[] = "<font color=red>Home Phone: Can only contain
Numeric characters, plus dash.</font>"; }

if ((!empty ($phone_office)) && (!ereg("^[[:digit:]\-]{{$len}}$",
$phone_office)))
     { $errors[] = "<font color=red>Office Phone: Can only contain
Numeric characters, plus dash.</font>"; }

if ((!empty ($phone_cell)) && (!ereg("^[[:digit:]\-]{{$len}}$",
$phone_cell)))
     { $errors[] = "<font color=red>Cell Phone: Can only contain
Numeric characters, plus dash.</font>"; }

$len = "0,30";
if (empty ($mothers_maiden_name))
     { $errors[] = "<font color=red>You didn't enter your Mothers
Maiden Name.</font>"; }
   elseif (!ereg("^[[:alpha:]]{{$len}}$", $mothers_maiden_name))
       { $errors[] = "<font color=red>Mothers Maiden Name: Can only
contain Upper & Lower case
         Alphabetical characters.</font>"; }


// This is the Captcha Security Code Random-Noisy-Images user input
response routine.
// This uses a randomly generated graphic image to stymie
// auto-submission scripts created by spammers.

 session_start();
 $digit     = $_SESSION['digit'];  // security code saved in session
variable by buttom.php
 $userdigit = $_POST['userdigit']; // security code entered by user
 session_destroy();

 if ($digit == $userdigit)
   { // Visitor entered correct number
     //$errors[] = "<font color=red>Correct Security code
entered.</font>";
   }
  else
   { // Wrong number
     $errors[] = "<font color=red>Wrong Security Code entered, try
again.</font>";
   }


// check to see that there are no errors and send the user back to
the form if there are.
// $_POST contains all the fields passed by the form and now has to
be passed to the show_form
// function so entered data can be re-shown.

 if (sizeof($errors)!=0)
     { show_form($errors,$_POST); die(); }


// Fall through logic to here if no errors on entered form data
// Now prepare and add record to mysql DB.

$member_type         = '1';  // type 1 is for fsbo users
$email_verified      = 'N';
$counter             = 0;
$date_added          = date("Y-m-d");  //  2006-03-07
$ip_of_useratsignup  = $_SERVER["REMOTE_ADDR"];


/* The @ operator is used to suppress any error messages that
mysql_connect()
and mysql_select_db() functions may produce if an error occurred.
The die()
function is used to end the script execution and display a custom
error message.*/

//  mysql_connect("localhost","root","mypassword")

// Logon to sql server
  mysql_pconnect("localhost","root")
        or die("Unable to connect to MySQL server");

//  Select BD
  mysql_select_db("mls_membership") or die("Unable to select
database");


/* Execute the SQL insert statement against the currently selected
database.
The results will be stored in the $results variable.
After the mysql_query() command executes, the $results variable is
examined
to determine if the mysql_query() was successfully executed. */

 $request = "INSERT INTO members
VALUES('$logonid','$email','$member_type',

'$email_verified','$logonpw','$date_added',NULL,'$counter','$firstna
me',

'$lastname','$addr1','$addr2','$city','$state','$zip','$phone_home',

'$phone_office','$phone_cell','$mothers_maiden_name','$ip_of_userats
ignup')";

 $results = mysql_query($request);



 if($results)
   {
    // $results are empty means insert was successful
    // print("Successfully added ".$_POST['id']." to the file");
    }
  else
   {
    $err = mysql_error();
    //print(" $err ");

    if ($err == "Duplicate entry '$logonid' for key 1")
     {
      $errors[] = "<font color=red>Your Logon ID is not available,
Enter a different one.</font>";
      show_form($errors,$_POST);
      die();
     }
    elseif ($err == "Duplicate entry '$email' for key 2")
       {
        $errors[] = "<font color=red>Your Email address is
registered to an existing
        account, Enter a different one.</font>";
        show_form($errors,$_POST);
       die();
       }
      else
        {
         $errors[] = "<font color=red>$err</font>";
         $errors[] = "<font color=red>Notify
webmaster@stripped you got this error. </font>";
         show_form($errors,$_POST);
         die();
        }
   }



// This is the start of the send email to new uers to vaildate their
email address is good.

 $hash = base64_encode($logonid);
 $fqdn  = $_SERVER["SERVER_NAME"];
 list($www, $Domain, $ttl) = explode(".",$fqdn);
 $eaddr = "noreply@$Domain.$ttl";


 $message    =
"<html>
<body>
Dear $firstname,<br>
<br>
Thank you for your member registration. <br>
<br>
The purpose of this confirmation message is to verify that the email
<br>
address you submitted is valid and reachable. This is necessary in
order for us<br>
to complete the activation of your Account. Since we know that you
are eager <br>
to start using your account, we've made every effort to keep the
<br>
confirmation process as simple as possible. <br>
<br>
Do not reply to this email as the From email address is not designed
to receive email.<br>
<br>
<br>
To complete your membership registration, simply click on the link
below:<br>
<br>
<br>
<a
href=http://www.verify.domainname.com/mls_verifyemail.php?hash=$hash
>$fqdn/verifyemail</a><br>
<br>
<br>
-----------------------------------------------------------<br>
This confirmation serves to protect you in case someone forged
this<br>
membership request in your name. <br>
<br>
If you changed your mind about your membership, or<br>
you did not sign up for membership, simply ignore this email. <br>
<br>
<br>
</body>
</html>";

// This uses the open source http://phpmailer.sourceforge.net/ php
mailer class for HTML email
// The "postfix" email server is 300% faster than "sendmail" email
server sending HTML email.

 require("class.phpmailer.php");
 $mail = new PHPMailer();
 $mail->From = "$eaddr";
 $mail->FromName = "WebMaster";
 $mail->AddAddress("$email");
 $mail->WordWrap = 50;        // set word wrap to 50 characters
 $mail->IsHTML(true);         // set email format to HTML
 $mail->Subject = "Validation of your email address";
 $mail->Body    = "$message";


 if($mail->Send())
     {
      //print("Successfully emailed msg to  $email");
      session_start();
	  $_SESSION['mls_valid_signup'] = "Y";
	  session_write_close();
      header("Location: mls_Read_setup_verify_email.php");  //the
user will be sent to this page
      exit;
     }
    else
     { $errors[] = "<font color=red>Error in sending Email address
validation Msg.</font>";
       print("Emailed msg to  $email failed");
       show_form($errors,$_POST);
     }
 }
php?>

</body></html>

Thread
Form value editingBrian E Boothe20 Apr
  • Re: Form value editingPeter Brawley20 Apr
  • RE: Form value editingfbsd21 Apr
    • RE: Form value editingPat Adams21 Apr
  • RE: Form value editingNicolas Verhaeghe21 Apr
RE: Form value editingJay Blanchard20 Apr