简体   繁体   English

jQuery按钮单击事件后,PHP脚本的响应消失

[英]response from php script disappears after jquery button click event

Here I posted one variable to PHP script. 在这里,我向PHP脚本发布了一个变量。 Response from php script I am writing to some div . 来自php脚本的响应我正在写一些div But after button click, instantly the response disappears: 但是单击按钮后,响应立即消失:

When I do alert(arabic); 当我alert(arabic); it displays but as I close prompt, it disappears. 它显示,但是当我关闭提示时,它消失了。

Why does it reload the page after response from php script? 为什么从php脚本响应后重新加载页面?

    $( "#submit" ).click(function() {    
            var cat = $("#cats option:selected").html();    
//          alert(test);    
            var arabic = document.getElementById("arabic").value;    
            //alert (arabic)    
            dataInsert(arabic);    
            });    
        function dataInsert(arabic)    
        {                   
            var xmlhttp;    
            //alert("hi");    
            show.innerHTML = '';    
            if (window.XMLHttpRequest)    
            {    
                // code for IE7+, Firefox, Chrome, Opera, Safari    
                xmlhttp=new XMLHttpRequest();    
            }    
            else    
            {    
                // code for IE6, IE5    
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");    
            }               
            xmlhttp.onreadystatechange=function()    
            {    
                //document.getElementById("old-records").innerHTML = "";                    
                if (xmlhttp.readyState == 4 && xmlhttp.status==200)    
                {    
                    var div2 = document.getElementById("show");    
                                div2.innerHTML = xmlhttp.responseText;    
                }    
            }         
            xmlhttp.open("POST","koove_insertpost_db.php");                 
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");    
            xmlhttp.send('arabic=' + arabic ) ;             
            //alert(arabic);    
        }           

It looks like your button is trying to submit something, thus overlapping your callback function. 看来您的按钮正在尝试提交某些内容,从而重叠了您的回调函数。 You might want to use the parameter event to stop it's propagation. 您可能想要使用参数事件来停止其传播。

Check this code: 检查此代码:

$( "#submit" ).click(function(event) {

    event.stopPropagation();

    var cat = $("#cats option:selected").html();    
    //alert("test");    
    var arabic = document.getElementById("arabic").value;    
    //alert (arabic)    
    dataInsert(arabic);

    return false;

    });    
}

You should also return false at the end of the callback function, in order to completely stop processing the event after your instructions. 还应该在回调函数的末尾return false ,以便完全按照指令进行处理。 This is the key to perform your dataInsert() function without interruption. 这是不间断地执行dataInsert()函数的关键。

Are you using jQuery? 您正在使用jQuery吗? looks like it with the $( "#submit" ).click(function() {}); 看起来像$(“ #submit”).click(function(){}); why not make life easy and just use 为什么不让生活变得轻松而只是使用

$( "#submit" ).click(function() { 

   var arabic = $("#arabic").value(); 

    $.post("koove_insertpost_db.php",{"arabic" : arabic}, function( data ) { 
        $('#show').html(data);
    });
});

http://api.jquery.com/jquery.post/ http://api.jquery.com/jquery.post/

you can also check the success status etc for data validation from the server. 您还可以从服务器检查成功状态等以进行数据验证。

I cant comment yet so i believe @feijones has the answer! 我还不能发表评论,所以我相信@feijones有答案! ( especially return false; ) (尤其是返回false;)

ps Alert works because it halts the execution of the JavaScript until you close the dialog, which then submits the form and reloads the page. ps Alert之所以有效,是因为它会停止JavaScript的执行,直到您关闭对话框,然后对话框才会提交表单并重新加载页面。

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

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