Home > Backend Development > PHP Tutorial > ajax 从一个页面传递错误信息至另一个页面?怎么解决?

ajax 从一个页面传递错误信息至另一个页面?怎么解决?

WBOY
Release: 2016-06-06 20:46:10
Original
1002 people have browsed it

这是一个页面的代码 index.php

<code>   <script>
    $("#yollash").click(function() {    
        var isim=$("#uyisim").val();
        var mezmun=$("#qay_num").val();
        if( isim.length>=2 & mezmun.length>0 ){            
            var formData = $("#jediwel").serializeArray();
            $.ajax({
                url : "yollash.php",
                type: "POST",
                data : formData,
                success: function(data, textStatus, jqXHR)
                {
                    $(".modal-body").html("<h2>قۇشۇلدى!")
                    $('#myModal').modal('toggle')
                 },
                 error: function(jqXHR, textStatus, errorThrown)
                {
                    $(".modal-body").html("<h2>قايتۇرۇش نۇمۇرى بار ئىكەن!")
                    $('#myModal').modal('toggle')
                }
            });
        } else {
            $(".modal-body").html("<h2>تۇلۇق تولدۇرۇڭ")
            $('#myModal').modal('toggle')
        }
    });
 </script>
</code>
Copy after login
Copy after login

我做了另一个页面,就是把表单上的数据插入到数据库中,然后就在那个页面判断数据库中某个数据存在不存在,如果存在就会传递给前面的页面一个错误信息,下面的是另一个页面的代码片段 yollash.php

<code>$yengi = mysql_query("SELECT qay_num FROM wx_milodiye WHERE qay_num= '$qay_num'"); 
if(mysql_num_rows($yengi)) {  

} else { 
    $yollash_sql= mysql_query("INSERT INTO wx_milodiye(uy_ismi,tur,musicurl,hqmusicurl,qay_num,waqit) VALUES('$uyisim','$tur','$musicurl','$hqmusicurl','$qay_num',now())") or die (mysql_error());   
}
</code>
Copy after login
Copy after login

就在那个判断的地方怎么写?(新学者,可能表达不清楚)

回复内容:

这是一个页面的代码 index.php

<code>   <script>
    $("#yollash").click(function() {    
        var isim=$("#uyisim").val();
        var mezmun=$("#qay_num").val();
        if( isim.length>=2 & mezmun.length>0 ){            
            var formData = $("#jediwel").serializeArray();
            $.ajax({
                url : "yollash.php",
                type: "POST",
                data : formData,
                success: function(data, textStatus, jqXHR)
                {
                    $(".modal-body").html("<h2>قۇشۇلدى!")
                    $('#myModal').modal('toggle')
                 },
                 error: function(jqXHR, textStatus, errorThrown)
                {
                    $(".modal-body").html("<h2>قايتۇرۇش نۇمۇرى بار ئىكەن!")
                    $('#myModal').modal('toggle')
                }
            });
        } else {
            $(".modal-body").html("<h2>تۇلۇق تولدۇرۇڭ")
            $('#myModal').modal('toggle')
        }
    });
 </script>
</code>
Copy after login
Copy after login

我做了另一个页面,就是把表单上的数据插入到数据库中,然后就在那个页面判断数据库中某个数据存在不存在,如果存在就会传递给前面的页面一个错误信息,下面的是另一个页面的代码片段 yollash.php

<code>$yengi = mysql_query("SELECT qay_num FROM wx_milodiye WHERE qay_num= '$qay_num'"); 
if(mysql_num_rows($yengi)) {  

} else { 
    $yollash_sql= mysql_query("INSERT INTO wx_milodiye(uy_ismi,tur,musicurl,hqmusicurl,qay_num,waqit) VALUES('$uyisim','$tur','$musicurl','$hqmusicurl','$qay_num',now())") or die (mysql_error());   
}
</code>
Copy after login
Copy after login

就在那个判断的地方怎么写?(新学者,可能表达不清楚)

<code>echo json_encode(array('code'=>201,'message'=>'数据已存在'));
</code>
Copy after login

js里面的success回调函数里面就可以

<code>if(data.code == 201){
    alert(data.message);
}
</code>
Copy after login

yollash.php 页面直接输出需要的内容就 ok 了

其实你需要定的是前后端交互的一个数据格式,到底是文本内容还是json数据。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template