2011-03-01 160 views
0

我需要一个提示,如何使用mootools进行AJAX请求,当从下拉列表中选择某个值时,我的意思是捕获该事件+向外部php页面发出ajax请求。在这个PHP页面上我需要运行一个mysql查询。谢谢。Mootools ajax请求

<form name ="f1" action=""> 
    <select id="myr" NAME ="s1" onChange = "GetSelectedItem()"> 
      <OPTION VALUE = "meshed" selected >-- Please Select --</OPTION> 
      <OPTION VALUE = "girls">Male seeking Female</OPTION> 
      <OPTION VALUE = "mens">Female seeking Male</OPTION> 
      <OPTION VALUE = "mens">Male seeking Male</OPTION> 
      <OPTION VALUE = "girls">Female seeking Female</OPTION> 
    </select> 
</form> 

PHP

$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');  
$dbname = 'ratings'; 
mysql_select_db($dbname) or die('Error connecting to database'); 
$sql = "TRUNCATE TABLE rabid_ratings"; 
$re = mysql_query($sql) or die(mysql_error()); 
echo "done"; 

mootools的

<script type="text/javascript"> 
     window.addEvent('domready',function(){ 
      var myRequest = new Request({ 
       url: 'truncate.php', 
       method: 'post', 
       onRequest: function(){ 
       }, 
       onSuccess: function(responseText){ 
        alert("done!"+ responseText); 
       }, 
       onFailure: function(){ 
        alert("failed"); 
       } 
      }); 

      $('myr').addEvent('change', function(event){ 
       event.stop(); 
       myRequest.send(); 
      }); 
     }); 
</script> 

回答

2

添加的改变,甚至你的选择下拉。让我们将它命名为“mydropdown”:

$('mydropdown').addEvent('change', function(){ 
    //do your request (AJax) here 
}); 

这里是Mootools的演示。您也可以使用Request.HTML或Request.JSON取决于您要返回的内容。

Simple Request W/ Mootools

更新:例如基于关闭您目前所

首先,让我们看看你的AJAX是设置正确使用以下简单的Ajax代码。一旦你得到这个工作,那么我们可以探索PHP方面。所以,请尝试以下代码:

HTML:

<select id="myr" NAME ="s1"> 
    <OPTION VALUE = "meshed" selected >-- Please Select --</OPTION> 
    <OPTION VALUE = "girls">Male seeking Female</OPTION> 
    <OPTION VALUE = "mens">Female seeking Male</OPTION> 
    <OPTION VALUE = "mens">Male seeking Male</OPTION> 
    <OPTION VALUE = "girls">Female seeking Female</OPTION> 
</select> 

通用截断的PHP回声无论职位VAR我们发送:

<?php 
    echo $_POST['s1']; 
?> 

Mootools的,当成功应该输出“完成了! “:

window.addEvent('domready', function(){ 
    var myRequest = new Request({ 
     url: 'truncate.php', 
     method: 'post', 
     onRequest: function() { 
     }, 
     onSuccess: function(responseText) { 
      alert("done! " + responseText); 
     }, 
     onFailure: function() { 
      alert("failed"); 
     } 
    }); 

    $('myr').addEvent('change', function(event) { 
     event.stop(); 
     var data = this.name + '=' + this.value; //s1=<option value>, which is the post data we are sending to the php script 
     myRequest.send(data); 
    }); 
}); 
+0

我不想返回任何东西,我只需要运行一个查询,这是外部的PHP文件,这种方法是应该使用? – Doolkin 2011-03-01 16:41:28

+0

@Doolkin你可以做一个普通的帖子,或者如果你不想要任何返回的东西,但请求会做。 – kjy112 2011-03-01 16:42:58

+1

要么会做。但是,如果您在服务器上运行的查询所做的不仅仅是获取某些信息(更新/删除/创建记录),还应该使用POST - 否则某些错误的Web Spider可能会绊倒您的PHP脚本并删除所有内容。 – 2011-03-01 16:43:25