用于注册表单的验证码不起作用

时间:2017-05-16 06:17:01

标签: javascript php jquery html mysql

我想在mysql中使用带有 <?php session_start(); ?> <!DOCTYPE html> enter code here<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Registration Form with PHP Captcha Demo</title> <meta name="title" content="Registration Form with PHP Captcha Demo"/> <meta name="description" content=""/> <meta name="keywords" content=""/> <link href="css/style_demo.css" rel="stylesheet" type="text/css" /> <script src="js/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.min.js"></script> <script language="javascript"> $(document).ready(function(){ $(".refresh").click(function () { $(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime())); }); $('#register').submit(function() { if($('#password').val() != $('#cpassword').val()){ alert("Please re-enter confirm password"); $('#cpassword').val(''); $('#cpassword').focus(); return false; } $.post("submit_demo_captcha.php?"+$("#register").serialize(), { }, function(response){ if(response==0){ $(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime())); clear_form(); alert("Data Submitted Successfully.") }else{ alert("wrong captcha code!"); } }); return false; }); function clear_form() { $("#fname").val(''); $("#lname").val(''); $("#username").val(''); $("#email").val(''); $("#dob").val(''); $("#gender").val(''); $("#password").val(''); $("#cpassword").val(''); $("#captcha").val(''); } }); </script> </head> <body> <div id="bodyfull"> <div id="bodyfull2"> <div id="center"> <div class="inner_right_demo"> <form name="register" action="#null" method="post" id="register"> <div class="form_box"> <div> <label>First Name</label> <input type="text" placeholder="Enter Your First Name" id="fname" name="fname" required="required"> </div> <div> <label>Last Name</label> <input type="text" placeholder="Enter Your Last Name" id="lname" name="lname" required="required"> </div> <div> <label>User Name</label> <input type="text" placeholder="Enter Your User Name" id="username" name="username" required="required"> </div> <div> <label>Email</label> <input type="email" placeholder="Enter Your Email Address" id="email" name="email" required="required"> </div> <div> <label>Date of birth</label> <input type="date" id="dob" name="dob"> </div> <div> <label>Gender</label> <div class="otherinputs"><input type="radio" value="Male" checked name="gender"> <span>Male</span> <input type="radio" value="Female" name="gender"> <span>Female</span> </div> </div> <div> <label>Password</label> <input type="password" placeholder="Enter Your Password" id="password" name="password" required="required"> </div> <div> <label>Confirm Password</label> <input type="password" placeholder="Enter Your Password Again" id="cpassword" name="cpassword" required="required"> </div> <div> <label>Captcha</label> <input type="text" placeholder="Enter Code" id="captcha" name="captcha" class="inputcaptcha" required="required"> <img src="demo_captcha.php" class="imgcaptcha" alt="captcha" /> <img src="images/refresh.png" alt="reload" class="refresh" /> </div> <div> <label>&nbsp;</label> <div class="otherinputs" ><input type="submit" value="Submit" name="B1" class="submit"> &nbsp; &nbsp;</div> </div> </div> </form> </div> </div> </div> </div> </body> </html> 数据的验证码创建一个注册表单。无论何时我提交表单,Captcha代码都会显示错误。但当我检查我的数据库连接代码是否正常工作。代码如下。

  

signup.php

<?php

session_start();

function getRandomWord($len = 5) {
    $word = array_merge(range('0', '9'), range('A', 'Z'));
    shuffle($word);
    return substr(implode($word), 0, $len);
}

$ranStr = getRandomWord();



$height = 35; //CAPTCHA image height
$width = 150; //CAPTCHA image width
$font_size = 24; 

$image_p = imagecreate($width, $height);
$graybg = imagecolorallocate($image_p, 245, 245, 245);
$textcolor = imagecolorallocate($image_p, 34, 34, 34);

imagettftext($image_p, $font_size, -2, 15, 26, $textcolor, 'fonts/mono.ttf', $ranStr);
//imagestring($image_p, $font_size, 5, 3, $ranStr, $white);
$_SESSION["vercode"] = $ranStr;
imagepng($image_p);
imagedestroy($image_p);

?>
  

demo_captcha.php

<?php
session_start();
$servername="localhost";
$username="root";
$password="1234";
$dbname="form";
$con=mysqli_connect($servername,$username,$password,$dbname);

if(($_REQUEST['captcha'] == $_SESSION['vercode'])){
    $lname  =$_POST['lname'];
    $fname  =   $_POST['fname'];
    $gender =   $_POST['gender'];
    $username   =$_POST['username'];
    $email  =$_POST['email'];
    $dob    =$_POST['dob'];
    $password   =$_POST['password'];

    //Here you can write your sql insert statement. 
    $sql = "INSERT INTO form.form1 (fname,gender,lname,mname,email,dob,password) VALUES('$fname','$gender','$lname','$username','$email','$dob','$password')";
     if(mysqli_query($con,$sql)){

        alert("Data Submitted Successfully.");
        }
        else{
        alert("wrong captcha code!");
            }
}


mysqli_close($con);
?>
  

submit_demo_captcha

       (weath)er is good.   // i want to mark this word
       (weather is go)od.   // i want to mark this word

1 个答案:

答案 0 :(得分:1)

  

submit_demo_captcha.php

<?php
session_start();
$servername="localhost";
$username="root";
$password="1234";
$dbname="form";
$con=mysqli_connect($servername,$username,$password,$dbname);

if(($_REQUEST['captcha'] == $_SESSION['vercode'])){
    $lname  =$_REQUEST['lname'];
    $fname  =   $_REQUEST['fname'];
    $gender =   $_REQUEST['gender'];
    $username   =$_REQUEST['username'];
    $email  =$_REQUEST['email'];
    $dob    =$_REQUEST['dob'];
    $password   =$_REQUEST['password'];

    //Here you can write your sql insert statement. 
    $sql = "INSERT INTO form.form1 (fname,gender,lname,mname,email,dob,password) VALUES('$fname','$gender','$lname','$username','$email','$dob','$password')";
     if(mysqli_query($con,$sql)){

echo 1;         }
        else{
echo 0;         }
}


mysqli_close($con);
?>

  

signup.php

 $.post("submit_demo_captcha.php?"+$("#register").serialize(), { }, function(response){
        if(response==1){
           $(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime()));
           clear_form();
           alert("Data Submitted Successfully.")
        }else{
           alert("wrong captcha code!");
        }
    });