2012-10-20 130 views
2

我有一些按钮被按下时重定向的AJAX代码。AJAX奇怪的重定向?

它重定向到members.php

这是AJAX代码:

<script language="javascript" type="text/javascript"> 

    function ajaxFunction(){ 
      var ajaxRequest; // The variable that makes Ajax possible! 

      try{ 
       // Opera 8.0+, Firefox, Safari 
       ajaxRequest = new XMLHttpRequest(); 
      } catch (e){ 
       // Internet Explorer Browsers 
       try{ 
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
       } catch (e) { 
        try{ 
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (e){ 
         // Something went wrong 
         alert("Please update your browser."); 
         return false; 
        } 
       } 
      } 
     } 

     // Create a function that will receive data sent from the server 
     ajaxRequest.onreadystatechange = function(){ 
      // We still need to write some code here 
       if(ajaxRequest.readyState == 4){ 
        // Get the data from the server's response 
        response = ajaxRequest.responseText; 
       } 
     } 
     ajaxRequest.open("GET", "accept.php?id=<?php echo $articleid; ?>&state=accept$p=a9dafdd0fe68c6f64841e265e1c8832a", true); 
     ajaxRequest.send(null);     
    }  

</script> 

这是按钮:

<input type="submit" onChange="ajaxFunction();" /> 

这是accept.php的内容:

<?php 
    echo "ACCEPTED"; 
?> 

想法?

+0

在'form'标签中添加'onsubmit =“返回false;”''。 – Leri

+0

所以它重定向,这是错误的,或者应该是重定向...?它应该怎么做?如果你不想让它在任何地方重定向,就像在其他人提到的那样,在按钮上或onsubmit上添加“返回false”。 – eis

+0

这是重定向,我不希望它。 PLB我在表单标签中尝试了这些代码,但没有运气。 – KriiV

回答

0
<input type="submit" onChange="ajaxFunction();" /> 

should be 

<input type="submit" onclick="ajaxFunction();" /> 

,并在你的函数

的末尾使用return false;,也可以使用简单的jQuery

$("#submit").click(function(){ 
// or $("#form").submit(function(){ 
$.get("accept.php?id=<?php echo $articleid; 
?>&state=accept&p=a9dafdd0fe68c6f64841e265e1c8832a",function(data){ 
rasponce=data; 
    }) 

return false; 
}) 
+0

它仍然重定向到members.php :( – KriiV

0

这是什么?

"&state=accept$p=a9dafdd0fe68c6f64841e265e1c8832a", 

...它应该像

"&state=accept&p=a9dafdd0fe68c6f64841e265e1c8832a", 
+0

它只是一个数据我想传递给accept.php – KriiV

+0

但是哪一部分是数据?如果您打算将'a9dafdd0fe68c6f64841e265e1c8832a'传递给一个查询参数,'$ p'不会在那里工作 – eis

0

members.php是你的窗体上的行动呢?如果是这样,我会删除该动作并再次尝试,看看会发生什么。

+0

nah表单没有动作 – KriiV

+0

表单必须有动作才能使其成为有效的HTML – eis

+0

我已经对动作=“”做了动作,仍然重定向到成员.php – KriiV