ajax调用简单的问题

时间:2013-05-12 05:55:15

标签: php jquery ajax

这个简单的代码我需要你的帮助.. 我点击这个按钮(editsubmit)然后转到另一个名为trailnew.php的页面。我想检查结果是否成功......

我把它放在了trailnew.php

    <?php
    $e=isset($_POST['editsubmit']) && $_POST['editsubmit'];
    if($e)
    {
        echo "inserted";
    }
    ?>

脚本..

<script>
    $("#editsubmit").click(function(event) {
        event.preventDefault();
        $("#form2").submit()
    });

    $("#form2").validate({
        debug: false,
        rules: {
            plnoedit:"required",
            pldtedit:"required",
            noboxedit:"required",
            glidedit:"required",
        },
        messages: {
            plnoedit: "Please select a pack list id..",
            pldtedit: "Please select a item id id..",
            noboxedit: "Please select a quantity id..",
            glidedit: "Please select a value id..",
        },

        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "trailnew.php",
                data: $('#form2').serialize(),
                cache: false,
                success: function(data) {
                    alert(data);
                }
            });

            return false;
        }
      });
</script>

第一页(trailnew.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 name id..",
    },
    
    submitHandler: function(form) {
 
    $.ajax
   ({
  
type: "POST",
url: "aanew.php",
data: $('#form1').serialize(),
cache: false,

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

  </script>
  
</head>
<body>
<div class="result3"></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 Name--</option>';
$tempholder = array();
$sql="SELECT CONCAT( p.pl_no,  '_', DATE_FORMAT( p.pl_dt,  '%d/%m/%Y' ) ,  '_', g.acname ) AS plname, g.gl_id,p.pl_no,p.pl_dt,p.no_box,p.pl_id,g.acname FROM packlist p, glmast g WHERE g.gl_id = p.gl_id ORDER BY pl_dt DESC , pl_no DESC LIMIT 30";


$query = mysql_query($sql) or die(mysql_error());
$nr = mysql_num_rows($query);
for ($i=0; $i<$nr; $i++){
$r = mysql_fetch_array($query);
if (!in_array($r['plname'], $tempholder)){
$tempholder[$i] = $r['plname'];
echo "<option value='$r[pl_id]'>".$r["plname"]."</option>";
}
}

echo '</select>';

?><br/>

<input type="submit" name="delete" id="delete" value="Delete"/><br/>
<input type="submit" name="edit" id="edit" value="Edit"/><br/>
</form>


<form>
<input type="button" name="new" id="new" class="new" value="New" /><br/>
</form>

<?php
$e=isset($_POST['editsubmit']) && $_POST['editsubmit'];
if($e)
{
echo "inserted!";
}
?>

<div id="result1"></div>  

</body>

</html>

第二页(aanew.php)

<?php
session_start();
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>
$("#editsubmit").click(function(event) {
    event.preventDefault();
     $("#form2").submit()
});
 $("#form2").validate({
         debug: false,
   rules: {

    plnoedit:"required",
    pldtedit:"required",
    noboxedit:"required",
    glidedit:"required",


       },
   messages: {

    plnoedit: "Please select a pack list id..",
    pldtedit: "Please select a item id id..",
    noboxedit: "Please select a quantity id..",
    glidedit: "Please select a value id..",

        },

         submitHandler: function(form) {

    $.ajax
   ({


type: "POST",
url: "trailnew.php",
data: $('#form2').serialize(),
cache: false,
success: function(data)
        {
            window.alert(data);
        }


        });
        return false;
        }



  });


</script>
<script>

 $("#form3").validate({
         debug: false,
   rules: {

    plnodelete:"required",
    pldtdelete:"required",
    noboxdelete:"required",
    gliddelete:"required",


       },
   messages: {

    plnodelete: "Please select a pack list id..",
    pldtdelete: "Please select a item id id..",
    noboxdelete: "Please select a quantity id..",
    gliddelete: "Please select a value id..",

        },

         submitHandler: function(form) {

    $.ajax
   ({


type: "POST",
url: "trailnew.php",
data: $('#form3').serialize(),
cache: false,
success: function() {
    alert('Deleted!'); 

        }


        });
        return false;
        }



  });

</script>
</head>

        <body>
        <div id="#result1"></div>
        <?php

    $e=isset($_POST['plid']) && $_POST['plid'];
    $f=isset($_POST['edit']) && $_POST['edit'];
    if($e&&$f)
    {


    $id=$_POST['plid'];
    $query5=mysql_query("SELECT g.gl_id, p.pl_no, p.pl_dt, p.no_box, p.pl_id,g.acname
        FROM packlist p, glmast g
        WHERE g.gl_id = p.gl_id
        AND p.pl_id ='".$id."'
        LIMIT 0 , 30") or die(mysql_error());

    $row=mysql_fetch_array($query5);
    $pl_no=$row['pl_no'];
    $pl_dt=$row['pl_dt'];
    $no_box=$row['no_box'];
    $acname=$row['acname'];



    ?>
        <form name="form2" id="form2" method="post" action="">
        <P>
            <LABEL for="plnoedit">PackList No 
                      <INPUT type="text" id="plnoedit" name="plnoedit" value= <?php echo $pl_no; ?> /></LABEL><BR><BR>
                      <input type="hidden" name="myFormsName" value="form5id" id="myFormsName">
                      <input type="hidden" name="pdidedit" value=<?php echo $id; ?> id="pdidedit">
            <LABEL for="pldtedit">Date 
                      <INPUT type="text" id="pldtedit" name="pldtedit" value= <?php echo $pl_dt; ?> /></LABEL><BR><BR>

            <LABEL for="noboxedit">Box No
                      <INPUT type="text" id="noboxedit" name="noboxedit" value= <?php echo $no_box; ?> /></LABEL><BR><BR>
            <LABEL for="glidedit">Party Name
                      <INPUT type="text" id="glidedit" name="glidedit" value= <?php echo $acname; ?> /></LABEL><BR><BR>

            <INPUT type="button" id="editsubmit" name="editsubmit" value="Submit"> <INPUT type="reset">
            </P> 

        </form>
        <?php

              }

             ?>


        </body>
        </html>

代码转到trainnew.php我想,但是警报已经加载了所有的html(全新的trainnew.php页面)..有什么建议吗?

来自Trainnew.php

我使用编辑提交按钮从aanew.php(由ajax)获取表单

现在我在trailnew.php中验证这个新表单(在trailnew.php中使用验证码)

现在问题是在提交处理程序上从这个表单调用成功ajax给了我

trailnew.php的整个新页面。我做错了什么?

我试图从昨天开始计算....我在这里错过了一些重要的东西我猜...或者ajax零件..任何帮助高度赞赏!!非常感谢提前..

2 个答案:

答案 0 :(得分:0)

 success: function() {
    alert('Success');  
  }

为什么在不需要数据时提醒数据!

答案 1 :(得分:0)

您必须停止trailnew.php页面才能执行完整

您的代码

<?php
$e=isset($_POST['editsubmit']) && $_POST['editsubmit'];
if($e)
{
    echo "inserted";
}
?>

你必须这样做

<?php
if(isset($_POST['editsubmit']) && $_POST['editsubmit'])
{
    die('inserted'); // this stop the page right here
}
?>