简单的ajax调用不起作用

时间:2013-05-11 08:37:43

标签: php jquery ajax

所以我做了简单的编辑/删除/新的mysql数据库更新。我正在使用ajax,因为我需要它在一个页面中。 因此,当我选中New按钮时,会出现一个新表单(从ajax调用到php文件)。我得到HTML格式。我想验证这一点。 但我不能。代码似乎是对的。我的代码在这里

trail.php

<?php include( 'connect.php'); ?>
<html>

<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>
    <script>
    $(document).ready(function() {
    $("#form1").validate({
        debug: false,
        rules: {

        plid: "required",
        },
        messages: {

        plid: "Please select a pack list id..",
        },
        submitHandler: function(form) {

        $.ajax({
           type: "POST",
            url: "aa.php",
            data: $('#form1').serialize(),
            cache: false,

            success: function(response) {
            $('#result1').html(response);

            }
        });
        }
    });

    });
    </script>
</head>

<body>
    <div id="result1"></div>Packing List</br>
    <form id="form1" name="form1" action="" method="post">
        <?php echo '<select  name="plid" id="plid">'; echo '<option value="" selected="selected">--Select the Pack List Id--</option>'; $tempholder=a rray(); $sql="SELECT `pl_id`
        FROM (
            SELECT `pl_id`
            FROM packlist
            ORDER BY `pl_id` DESC
            LIMIT 30
        ) AS t" ; $query=m ysql_query($sql) or die(mysql_error()); $nr=m ysql_num_rows($query); for ($i=0; $i<$nr; $i++){ $r=m ysql_fetch_array($query); if (!in_array($r[ 'pl_id'], $tempholder)){ $tempholder[$i]=$ r[ 'pl_id']; echo "<option>".$r[ "pl_id"]. "</option>"; } } echo '</select>'; ?>
        <br/>
        <input type="submit" name="new" id="new" value="New" />
        <br/>
        <input type="submit" name="delete" value="Delete" />
        <br/>
        <input type="submit" name="edit" id="edit" value="Edit" />
        <br/>
    </form>
</body>

我的ajax叫做php文件

<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#form2").validate({
                debug: false,
                rules: {

                    plidnew: "required",

                },
                messages: {

                    plidnew: "Please select a pack list id..",
                }
            });
        });
    </script>
</head>

<body>
    <?php $a=isset($_POST[ 'plid']) && $_POST[ 'plid']; $b=isset($_POST[ 'new']) && $_POST[ 'new']; if($a&&$b) { ?>
    <form name="form2" id="form2" method="post" action="">
        <P>
            <LABEL for="plidnew">PackList No
                <INPUT type="text" id="plidnew">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="itemidnew">Item Id
                <INPUT type="text" id="itemidnew">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="quannew">Quantity
                <INPUT type="text" id="quannew">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="potnew">Potency
                <INPUT type="text" id="potnew">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="sizenew">Size
                <INPUT type="text" id="sizenew">
            </LABEL>
            <BR>
            <BR>
            <INPUT type="submit" id="newsubmit" name="newsubmit" value="Submit">
            <INPUT type="reset">
        </P>
    </form>
    <?php }
$c=isset($_POST[ 'plid']) && $_POST[ 'plid'];
$d=isset($_POST[ 'delete']) && $_POST[ 'delete']; 
if($c&&$d) { 
    echo "delete!!"; 
} 
$e=isset($_POST[ 'plid']) && $_POST[ 'plid'];
$f=isset($_POST[ 'edit']) && $_POST[ 'edit']; 
if($e&&$f) { 
?>
    <form name="form3" id="form3" method="post" action="aa.php">
        <P>
            <LABEL for="plidedit">PackList No
                <INPUT type="text" id="plidedit">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="itemidedit">Item Id
                <INPUT type="text" id="itemidedit">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="quanedit">Quantity
                <INPUT type="text" id="quanedit">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="potedit">Potency
                <INPUT type="text" id="potedit">
            </LABEL>
            <BR>
            <BR>
            <LABEL for="sizeedit">Size
                <INPUT type="text" id="sizeedit">
            </LABEL>
            <BR>
            <BR>
            <INPUT type="submit" id="editsubmit" name="editsubmit" value="Submit">
            <INPUT type="reset">
        </P>
    </form>
    <?php } ?>
</body>

我对验证表格有疑问。我在两个页面都尝试了验证码。  但是没有像萤火虫那样的ajax效果。任何帮助表示感谢..非常感谢..

1 个答案:

答案 0 :(得分:0)

ajaxtype“post”一起使用时,您应该小心传递数据 datadata1是web方法或服务的参数。试试这个

var d1="asd";
$.ajax({
 type: "POST",
 url: "aa.php",
 data: "{data:'"+d1+"',data1:'"+d2+"'}",
 cache: false,
 success: function(response) {
    $('#result1').html(response);
 }
});