1

您好,我有以下代码,它可以工作,我的意思是它可以很好地验证验证码,但它不会在 sign_form.php 中发布值

<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 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 代码

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;
}

我对代码进行了更新,但随后它刷新了我的页面

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

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

2 回答 2

1

将您的 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>

和 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
    }
    ?>
于 2012-11-12T11:57:11.153 回答
0

我已将其测试为:

表单页面为:

<?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 页面 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;
}
?>

提交时它会提醒所有值。那么你的问题是什么?

于 2012-11-12T10:45:18.613 回答