Click here to Skip to main content
15,868,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am sending some value from my form but the problem is that when i refresh the page some value automatically inserted in database which are send by me in hidden field. and i also want to send the product key by email on the user when it register to our site. is my mail function is right.
the code is here

XML
<?php
include('header.php');
include('menu.php');
@$obj = new MyClass();

$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$key=substr(number_format(time() * rand(),0,'',''),0,8);
$status='0';
$query  =   mysql_query("INSERT INTO `user` (`username`, `email`, `telephone`, `ProductKey`, `CreatedDate`,`ActiveStatus`) VALUES ('$name', '$email', '$phone', '$key',now(),'$status')");
//or die (mysql_error());
if($query)
    {     $query1       =   "SELECT * FROM `clearomizer` WHERE query.id='Sno'";

            $db_query   =   mysql_query($query1);
            $row        =   @mysql_fetch_array($db_query);

            $name       =   $row['username'];
            $key        =   $row['ProductKey'];
            $username   =   $row['email'];


     $to=$username;
           $subject="Your Key For The product";
           $msg="Dear $name,<br /><br />";
        $msg.="Welcome to live.com.  Your Activation Key Is $key. <br /><br />";
        $msg.="Thank you once again for purchasing this product. If you have any issue with our service or have any questions, you can reach us by following methods.";
            $msg.="<br /><br />Email: support@live.com <br /><br /><br/>";
            $msg.="Happy to serve you<br/>Team Live ";
            $headers= 'MIME-Version: 1.0' . "\r\n";
           $headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
           $headers.= "From:www.live.com\r\n";
           mail($to,$subject,$msg,$headers);
    }
    else {

        echo "thanks";
        }


?>

<script  type="text/javascript">
 function validateForm()
{


  var x=document.forms["myForm"]["name"].value;
if (x==null || x=="")
  {
  alert("Please Enter First Name");
  return false;
  }
  var iChars = "abcdefghijklmnopqrstuvwxyz.ABCDEFGHIJKLMNOPQRSTUVWXYZ.  ";
var x=document.forms["myForm"]["name"].value;
for (var i = 0; i < x.length; i++)
{
if (iChars.indexOf(x.charAt(i)) == -1)
{

alert("Enter Alpha Value In Name");
return false;
}
} var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Please Enter a valid e-mail address");
  return false;
  }
  var x=document.forms["myForm"]["phone"].value;
if (x==null || x=="")
  {
  alert("Please Enter Contact Number");
  return false;
  }
  var iChars = "0123456789.-";
var x=document.forms["myForm"]["phone"].value;
for (var i = 0; i < x.length; i++)
{
if (iChars.indexOf(x.charAt(i)) == -1)
{

alert("Please Enter Numeric value In Contact");
return false;
}}

}
</script>


<form name="myForm"  onSubmit="return validateForm()" method="post">


 <table width="280" border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td width="128">Name</td>
    <td width="145"><label for="name"></label>
      <input type="text" name="name" id="name"></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><label for="email"></label>
      <input type="text" name="email" id="email"></td>
  </tr>
  <tr>
    <td>Contact No</td>
    <td><label for="phone"></label>
      <input type="text" name="phone" id="phone"></td>
  </tr>
  <tr>
    <td><input type="button" name="submit" id="submit" value="Submit"></td>
    <td>&nbsp;</td>
  </tr>
</table></form>
</div>
Posted
Updated 16-Aug-12 10:41am
v3
Comments
StianSandberg 16-Aug-12 9:30am    
don't know PHP but after reading your javascript code I would really recommend you to have a look at jQuery.. Just a tip :)

1 solution

the best practice or atleast the practice that i follow whenever i m working with PHP and mysql, is that i have my form and database script in separate pages. So lets say the page with form is called index.php and the page with which stores the value in the database is called register.php.

Then I use jQuery AJAX in index.php to send my form values in a GET or POST asynchronous request to register.php and let it save the values in the database and send an e-mail to the user. The beauty of it all is that you don't have to show i.e. redirect to the register.php page at all, you can do all this without ever leaving the index.php page. Also your javascript could really benefit from jQuery have a look at: Jquery[^].

the problem with your code seems to be the fact that both your form and PHP script is on the same page and there is no check for a postback to self. And in the absence of a "action" attribute in form tag, the default is to postback to itself and that's why when u refresh the same values get re-posted.

Hope this helps.

EDIT: Also, use Regular Expressions for input validation and don't defer the validations until the user clicks the submit button. Try doing as soon as the control loses focus (jquery will manifest its power here and you will love it) :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900