检查验证码不起作用

时间:2017-10-15 15:32:57

标签: php jquery ajax session

帮助请与CAPTCHA打交道。在我看来它是正常的,通过Ajax输入的代码被发送到文件insert.php,在我的表单处理程序。但检查在那里不起作用。什么不输入输入字段,一切都被认为是正确的

我的表格和ajax

<form action="/data/insert.php" method="POST" id="guest-form" name="reg">
    <input id="username" type="text" name="name" placeholder="Username">
    <input id="email" type="text" name="email" placeholder="E-mail">
    <textarea id="message" type="text" name="text" placeholder="Your message"></textarea>
    <img src="data/captcha.php" alt="Картинка" /><br />
    <input id ="captcha" type="text" name="captcha" /><br />

    <input id="submit" class="btn btn-default" type="button" value="Submit">

</form>

的Ajax

$(document).ready(function() {
            $('#submit').click(function() {
                var username = $('#username').val();
                var email = $('#email').val();
                var message = $('#message').val();
                var captcha = $('#captcha').val();
                if(username || email || message || captcha  === ''){
                    alert('Please input data in all fields');
                }
                else{
                $.ajax({
                    type: "POST",
                    cache: false,        
                    url: '/data/insert.php',
                    data: {username: username, email: email, message: message, captcha: captcha},
                    success: function(data) {
                    }
                });
            }
            });
        });

php处理程序

 require_once 'db.php';
require_once 'captcha.php';
session_start();
if (isset($_SESSION["captcha"]) && $_SESSION["captcha"]===$_POST["captcha"]){ echo "Текст введен верно"; }
else { 
    echo "Текст введен не верно";
}
unset($_SESSION["captcha"]);

if (isset($_POST['username']) && isset($_POST['email']) && isset($_POST['message'])){

    $username = $_POST['username'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $db_host = "localhost"; 
    $db_user = "alekspvn"; // Логин БД
    $db_password = "123"; // Пароль БД
    $db_table = "book"; // Имя Таблицы БД

$connect_db=mysql_connect(HOST, MYSQL_USER, MYSQL_PASS)   
    or die("No connection with SQL"); 

    mysql_select_db("guests_db",$connect_db);

    mysql_query("SET NAMES 'utf8'",$connect_db);

    $result = mysql_query ("INSERT INTO ".$db_table." (username,email,message) VALUES ('$username','$email','$message')");

    if ($result = 'true'){
        echo "Информация занесена в базу данных";
    }else{
        echo "Информация не занесена в базу данных";
    }
}
?>

0 个答案:

没有答案