简体   繁体   English

表单验证码不刷新页面

[英]Form captcha without refreshing page

Hello i have the following code, it works i mean it validates captcha good but it doesnt POST values in the sign_form.php 您好我有以下代码,它有效,我的意思是它验证了验证码,但在sign_form.php中没有POST值

<script>

$(document).ready(function() { 

     $('#Send').click(function() {  

            $.post("sign_form.php?"+$("#sign_form").serialize(), {

            }, function(response){

            if(response==1)
            {
                $("#after_submit").html('');
                $("#Send").after('<div id="after_submit">Your message has been submitted.</div>');
                change_captcha();
                clear_form();
            }
            else
            {
                $("#after_submit").html('');
                $("#Send").after('<div id="after_submit">Error ! invalid captcha code .</div>');

            }

        });

        return false;
     });

     // refresh captcha
     $('img#refresh').click(function() {  

            change_captcha();
     });

     function change_captcha()
     {
        document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
     }

     function clear_form()
     {
        $("#name").val('');
        $("#email").val('');
        $("#message").val('');

     }
});

form code 表格代码

<form id="sign_form" name="sign_form" method="POST" action="#">
                        <fieldset class="step">
                            <legend>Podepsat tuto petici</legend>
                            <p>
                                <label for="first_name">Křestní jméno:</label>
                                <input type="text" name="first_name"  id="first_name">
                            </p>
                            <p>
                                <label for="last_name">Příjmení:</label>
                                <input type="text" name="last_name"  id="last_name">
                            </p>

                            <p>
                                <label for="email">Email:</label>
                                <input type="text" name="email"  id="email">
                            </p>
                             <p>
                                <label for="email">Mesto:</label>
                                <input type="text" name="city"  id="city">
                            </p>
                            <p>
                            <div id="wrap" align="center">
        <img src="get_captcha.php" alt="" id="captcha" />

        <br clear="all" />
        <input name="code" type="text" id="code">
    </div>
    <img src="refresh.jpg" width="25" alt="" id="refresh" />


    </p>
                            <p>

                                <input type="hidden" value="'.$id.'" name="id" id="id">
                            </p>
                            <p>
                             Emailová adresa nebude zveřejněna a nebude <br>předána žádné třetí straně.
                            </p>
                            <p>
                            <button  type="submit" value="send" id="Send"></button>
                            <div id="status"></div>
                            </p>
                            </form>

sign_form.php code sign_form.php代码

session_start();
 require_once('engine/db.php');
 mysql_query("SET NAMES utf8");
if(($_REQUEST['code'] == $_SESSION['random_number']) || @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']) )
{

$first_name=$_POST['first_name'];
            $last_name=$_POST['last_name'];
            $email=$_POST['email'];
            $city=$_POST['city'];
$id=$_POST['id'];

            $insert = "INSERT INTO petition_vote
         (id_petition,first_name,last_name,email,city)

        VALUES ('".$id."','".mysql_real_escape_string($first_name)."','".mysql_real_escape_string($last_name)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($city)."')";
            //$add_member = mysql_query($insert);
            $result = mysql_query($insert)
                or die("query failed: " . mysql_error());
          echo 1;
}
else
{
echo 0;
}

I came with an update to the code but then it refreshes my page 我对代码进行了更新,但随后刷新了我的页面

if(response==1)
{
submitform();
}
else
…

function submitform()
{
document.forms.sign_form.submit();
}

Replace your index.php to 将您的index.php替换为

<?php
session_start();
?>
<!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=utf-8" />

<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script>

$(document).ready(function() { 

     $('#Send').click(function() {  

        serializedData=$("#MYFORM").serialize();
        $.ajax({
        url: "post.php",
        type: "post",
        data: serializedData,
        // callback handler that will be called on success
        success: function(response){
            alert(response);
            if(response==1)
            {
                $("#after_submit").html('');
                $("#Send").after('<label class="success" id="after_submit">Your message has been submitted.</label>');
                change_captcha();
                clear_form();
            }
            else
            {
                $("#after_submit").html('');
                $("#Send").after('<label class="error" id="after_submit">Error ! invalid captcha code .</label>');
            }
        }       

        });

        return false;
     });

     // refresh captcha
     $('img#refresh').click(function() {  

            change_captcha();
     });

     function change_captcha()
     {
        document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
     }

     function clear_form()
     {
        $("#name").val('');
        $("#email").val('');
        $("#message").val('');
     }
});



</script>        

<style>
body{ font-family:Georgia, "Times New Roman", Times, serif; font-size:14px}
#wrap{
    border:solid #CCCCCC 1px;
    width:203px;
    -webkit-border-radius: 10px;
    float:left;
    -moz-border-radius: 10px;
    border-radius: 10px;
    padding:3px;
    margin-top:3px;
    margin-left:80px;
}

.error{ color:#CC0000; font-size:12px; margin:4px; font-style:italic; width:200px;}

.success{ color:#009900; font-size:12px; margin:4px; font-style:italic; width:200px;}

img#refresh{
    float:left;
    margin-top:30px;
    margin-left:4px;
    cursor:pointer;
}

#name,#email{float:left;margin-bottom:3px; height:20px; border:#CCCCCC 1px solid;}

#message{ width:260px; height:100px;float:left;margin-bottom:3px; border:#CCCCCC 1px solid;}

label{ float:left; color:#666666; width:80px;}

#Send{ border:#CC0000 solid 1px; float:left; background:#CC0000; color:#FFFFFF; padding:5px;}

</style>
</head>
<body>


<br clear="all" />

<br clear="all" />
<br clear="all" />

<div align="left" style="padding:30px;">

<form action="#" name="MYFORM" id="MYFORM">

    <label>Name</label>
    <input name="name" size="30" type="text" id="name">
    <br clear="all" />
    <label>Email</label>
    <input name="email" size="30" type="text" id="email">
    <br clear="all" />
    <label>Message</label>
    <textarea id="message" name="message"></textarea>
    <br clear="all" />


    <div id="wrap" align="center">
        <img src="get_captcha.php" alt="" id="captcha" />

        <br clear="all" />
        <input name="code" type="text" id="code">
    </div>
    <img src="refresh.jpg" width="25" alt="" id="refresh" />

    <br clear="all" /><br clear="all" />
    <label>&nbsp;</label>
    <input value="Send" type="submit" id="Send">


</form>

</div>



<br clear="all" /><br clear="all" /><br clear="all" /><br clear="all" />
<br clear="all" /><br clear="all" /><br clear="all" /><br clear="all" />
<br clear="all" /><br clear="all" />              

</body>
</html>

and post.php as 和post.php为

    <?php
    session_start();
//print_r($_SESSION);   


    if(($_REQUEST['code'] == $_SESSION['random_number']) || @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']) )
    {


            //$email=$_REQUEST['email'];


        $first_name=$_POST['first_name'];
            $last_name=$_POST['last_name'];
            $email=$_POST['email'];
            $city=$_POST['city'];
           $id=$_POST['id'];

            echo $insert = "INSERT INTO petition_vote
         (id_petition,first_name,last_name,email,city)

        VALUES ('".$id."','".mysql_real_escape_string($first_name)."','".mysql_real_escape_string($last_name)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($city)."')";
            //$add_member = mysql_query($insert);
            $result = mysql_query($insert)or die("query failed: " . mysql_error());
          echo 1;



        echo 1;// submitted 
        //echo''.$email.'';
    }
    else
    {
        echo 0; // invalid code
    }
    ?>

I have tested it as: 我已经测试为:

form page as: 表单页面为:

<?php
session_start();
?>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"     type="text/javascript"></script>
    <script>

    $(document).ready(function() { 

         $('#Send').click(function() {  

                $.post("sign_form.php?"+$("#sign_form").serialize(), {

                }, function(response){
                 alert(response);
                if(response==1)
                {
                    $("#after_submit").html('');
                    $("#Send").after('<div id="after_submit">Your message has been submitted.</div>');
                    change_captcha();
                    clear_form();
                }
                else
                {
                    $("#after_submit").html('');
                    $("#Send").after('<div id="after_submit">Error ! invalid captcha code .</div>');

                }

            });

            return false;
         });

         // refresh captcha
         $('img#refresh').click(function() {  

                change_captcha();
         });

         function change_captcha()
         {
            document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
         }

         function clear_form()
         {
            $("#name").val('');
            $("#email").val('');
            $("#message").val('');

         }
    });

    </script>

    <form id="sign_form" name="sign_form" method="POST" action="#">
                            <fieldset class="step">
                                <legend>Podepsat tuto petici</legend>
                                <p>
                                    <label for="first_name">K?estní jméno:</label>
                                    <input type="text" name="first_name"  id="first_name">
                                </p>
                                <p>
                                    <label for="last_name">P?íjmení:</label>
                                    <input type="text" name="last_name"  id="last_name">
                                </p>

                                <p>
                                    <label for="email">Email:</label>
                                    <input type="text" name="email"  id="email">
                                </p>
                                 <p>
                                    <label for="email">Mesto:</label>
                                    <input type="text" name="city"  id="city">
                                </p>
                                <p>
                                <div id="wrap" align="center">
            <img src="get_captcha.php" alt="" id="captcha" />

            <br clear="all" />
            <input name="code" type="text" id="code">
        </div>
        <img src="refresh.jpg" width="25" alt="" id="refresh" />


        </p>
                                <p>

                                    <input type="hidden" value="'.$id.'" name="id" id="id">
                                </p>
                                <p>
                                 Emailová adresa nebude zve?ejn?na a nebude <br>p?edána žádné t?etí stran?.
                                </p>
                                <p>
                                <button  type="submit" value="send" id="Send"></button>
                                <div id="status"></div>
                                </p>
                                </form>

php page sign_form.php php页面sign_form.php

<?php
session_start();
 //require_once('engine/db.php');
 //mysql_query("SET NAMES utf8");

 echo print_r($_REQUEST);
if(($_REQUEST['code'] == $_SESSION['random_number']) || @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']) )
{

$first_name=$_POST['first_name'];
            $last_name=$_POST['last_name'];
            $email=$_POST['email'];
            $city=$_POST['city'];
$id=$_POST['id'];

            $insert = "INSERT INTO petition_vote
         (id_petition,first_name,last_name,email,city)

        VALUES ('".$id."','".mysql_real_escape_string($first_name)."','".mysql_real_escape_string($last_name)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($city)."')";
            //$add_member = mysql_query($insert);
            $result = mysql_query($insert)
                or die("query failed: " . mysql_error());
          echo 1;
}
else
{
echo 0;
}
?>

On submit it is alerting all the values.then what is your problem? 在提交时,它会警告所有值。那么您的问题是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM