使用Ajax和3个连续的下拉列表中视海誓山盟 [英] using Ajax with 3 consecutive drop down lists depending on eachother

查看:112
本文介绍了使用Ajax和3个连续的下拉列表中视海誓山盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平安与你同在所有的,我是新来使用Ajax,问题是,我有3下拉连接到数据库列表,第一个是名,第二个是年龄和第三一个是国家!所以,我已经连接到数据库,并在第一个列表名称中检索数据,然后使用Ajax,我已成功地检索选择的第一个列表中的任何选项之后匹配的数据,并把它们放到名为时代的第二份名单,问题是,当我使用一个非常精确的相同的方式被称为年龄第二个列表检索匹配数据到名为榜第三的国家这是行不通的!所以请帮助我的Cuz,我用这个例子来学习Ajax的,然后应用在更大的实际项目! 这里是code: - 首先,home.php页面: -

 < PHP
包括config.php文件;
?>
< HTML>
< HEAD>
<脚本类型=文/ JavaScript的>
功能agematch(){
 如果(window.XMLHtt prequest){
  XMLHTTP =新XMLHtt prequest();
}
 其他 {
 XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
 }

xmlhttp.onreadystatechange =功能(){
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){

  的document.getElementById(时代)的innerHTML = xmlhttp.responseText。
  }

}
xmlhttp.open(GET,?connection.inc.php名='+ document.ajax.name.value,真正的);
xmlhttp.send();

}

功能countrymatch(){
 如果(window.XMLHtt prequest){
  XMLHTTP =新XMLHtt prequest();
}
 其他 {
 XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
 }

xmlhttp.onreadystatechange =功能(){
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){

  的document.getElementById(国家)的innerHTML = xmlhttp.responseText。
  }

}
xmlhttp.open(GET,country.inc.php年龄=?'+ document.ajax.age.value,真正的);
xmlhttp.send();

}

< / SCRIPT>
< /头>
<身体GT;
<形式ID =AJAXNAME =AJAX>
选择你的名字:<选择名称=名称ID =名称中选择=选择的onchange =agematch();> <选项> < /选项>
 < PHP

   $查询=SELECT DISTINCT的名字从信息;
   $结果= mysql_query($查询);
   而($行= mysql_fetch_array($结果)){
   回声<期权价值='。$行[0]。'>'@ $行[0]。'< /选项>中;
                                }
 ?>
< /选择>
年龄:其中,选择ID =时代NAME =年龄的onchange =countrymatch();> < /选择>
国家:LT;选择ID =国家名称=国家> <选项> < /选项> < /选择>

< /形式GT;
< /身体GT;
< / HTML>
 

现在,对于第一个Ajax调用的页面: -

 < PHP
包括config.php文件;
回声<选项>< /选项>中;
如果(使用isset($ _ GET ['名称'])){
  @ $名称= $ _GET ['名称'];
  }

  $查询=选择年龄信息,其中name ='@ $名字。';
  $结果= mysql_query($查询);
    而($ query_row = mysql_fetch_array($结果)){

      回声<期权价值='。$ query_row [0]。'> $ query_row [0] LT; /选项>中;
      }

 ?>
 

现在,随着第二Ajax调用第三个下拉菜单页面: -

 < PHP
包括config.php文件;

  如果(使用isset($ _ GET ['年龄'])){
     @ $年龄= $ _ GET ['年龄'];
   }

  $查询=选择国家FROM信息,其中name ='@ $名字。'和年龄='@ $时代。';
  $结果= mysql_query($查询);
  而($ query_row = mysql_fetch_array($结果)){

   回声<期权价值='。$ query_row [0]。'> $ query_row [0] LT; /选项>中;

  }

 ?>
 

所以你看,这里是code,当然我连接到通过一个名为config.php文件页的数据库,所以我要你帮我解决这个问题,并检索数据从数据库到第三个下拉列表中的国家。 在此先感谢!

好了,穆萨在这里编辑: -

 函数countrymatch(){
 如果(window.XMLHtt prequest){
  XMLHTTP =新XMLHtt prequest();
}
 其他 {
 XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
 }

xmlhttp.onreadystatechange =功能(){
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){

  的document.getElementById(国家)的innerHTML = xmlhttp.responseText。
  }

}
VAR年龄= EN codeUriComponent(document.ajax.age.value)
VAR名称= EN codeUriComponent(document.ajax.name.value)
xmlhttp.open(GET,country.inc.php年龄=?'+年龄+'和;名称+名,真实);
xmlhttp.send();

}
 

和也: -

 < PHP
包括config.php文件;
  如果(使用isset($ _ GET ['年龄'],$ _ GET ['名称'])){
     @ $年龄= $ _ GET ['年龄'];
     @ $名称= $ _GET ['名称'];
   }

  $查询=选择国家的信息,其中名称=@ $名字。'和年龄='@ $时代。';
  $结果= mysql_query($查询);
  而($ query_row = mysql_fetch_array($结果)){

   回声<期权价值='。$ query_row [0]。'> $ query_row [0] LT; /选项>中;

  }

 ?>
 

我没有得到任何错误信息,我相信你的观点是正确的,但这个解决方案没有奏效不幸!非常感谢你对我的帮助:)

解决方案

您没有发送名称,在第二个Ajax请求,但你需要为你的数据库查询,所以你需要发送的名称,以及年龄在Ajax请求。你也不会消毒的输入,必须始终验证用户输入的,我也想建议不要用mysql _ *

  

此扩展pcated作为PHP 5.5.0中去$ P $,并且将在未来被删除。相反,的MySQLi 或的 PDO_MYSQL 扩展程序应该被使用。另请参见 MySQL的:选择一个API 指南和的相关FAQ 了解详情。

  VAR年龄= EN codeURIComponent(document.ajax.age.value)
VAR名称= EN codeURIComponent(document.ajax.name.value)
xmlhttp.open(GET,country.inc.php年龄=?'+年龄+'和;名称+名,真实);
 

如果(使用isset($ _ GET ['年龄'],$ _ GET ['名称'])){     $年龄= $ _GET ['年龄'];     $ NAME = $ _GET ['名称'];     ... }

peace be with you All, i am new to using Ajax , the issue is, i am having 3 drop down lists connected to a database, the first one is "name" and the second one is "age" and the third one is "country"! so, i have connected to the database, and retrieved data from it in the first list "name" and then using Ajax, i have successfully retrieved matching data after selecting any option of first list and put them into the second list called "age", the problem is that when i use a very exact same way with the second list called "age" to retrieve matching data into third list called "country" it doesn't work! so please help me cuz, i am using this example to learn Ajax and then apply on a larger real project! here is the code :- firstly, the home.php page:-

<?php
include "config.php";
?>
<html> 
<head> 
<script type="text/javascript">
function agematch() {
 if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
} 
 else {
 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
 }

xmlhttp.onreadystatechange = function() {
  if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

  document.getElementById('age').innerHTML = xmlhttp.responseText;
  }

} 
xmlhttp.open('GET', 'connection.inc.php?name='+document.ajax.name.value, true );
xmlhttp.send();

}

function countrymatch() {
 if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
} 
 else {
 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
 }

xmlhttp.onreadystatechange = function() {
  if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

  document.getElementById('country').innerHTML = xmlhttp.responseText;
  }

} 
xmlhttp.open('GET', 'country.inc.php?age='+document.ajax.age.value, true );
xmlhttp.send();

}

</script>
</head>
<body> 
<form id="ajax" name="ajax" > 
Choose your name : <select name="name" id="name" select="selected"  onchange="agematch();"> <option>  </option> 
 <?php

   $query = "SELECT DISTINCT name FROM info";
   $result = mysql_query($query);
   while($row = mysql_fetch_array($result)){
   echo"<option  value ='".$row[0]."'> '".@$row[0]."'</option>";
                                }   
 ?>
</select>
Age : <select id="age" name="age" onchange="countrymatch();">  </select>
country : <select id="country" name="country"> <option> </option> </select>

</form>
</body>
</html>

now, the page for first Ajax call :-

<?php
include "config.php";
echo " <option>  </option> " ;
if(isset( $_GET['name']) ) {
  @$name = $_GET['name'];
  }

  $query = "SELECT age FROM info WHERE name = '".@$name."' "; 
  $result = mysql_query($query);
    while ($query_row = mysql_fetch_array($result)) {

      echo " <option  value ='".$query_row[0]."'> $query_row[0]</option> ";
      }

 ?>

Now, with the page for the second Ajax call for the third drop menu :-

<?php
include "config.php"; 

  if (isset( $_GET['age']) ) {
     @$age=$_GET['age'];
   }     

  $query = "SELECT country FROM info WHERE name='".@$name."' AND age='".@$age."'  ";
  $result= mysql_query($query);
  while  ($query_row = mysql_fetch_array($result)) {

   echo " <option value = '".$query_row[0]."'> $query_row[0] </option> ";

  }

 ?>

so as you see, here is the code, and of course i am connected to the database through a page called "config.php", so i want you to help me to solve this issue and retrieve the data from database into the third drop down list "country". Thanks in Advance!

Ok, Musa here is the edit :-

function countrymatch() {
 if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
} 
 else {
 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
 }

xmlhttp.onreadystatechange = function() {
  if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

  document.getElementById('country').innerHTML = xmlhttp.responseText;
  }

} 
var age = encodeUriComponent(document.ajax.age.value),
var name = encodeUriComponent(document.ajax.name.value),
xmlhttp.open('GET', 'country.inc.php?age='+age+'&name'+name, true );
xmlhttp.send();

}

and also :-

<?php
include "config.php";
  if (isset($_GET['age'], $_GET['name']) ) {
     @$age=$_GET['age'];
     @$name = $_GET['name'];
   }     

  $query = "SELECT country from info where name='".@$name."' AND age='".@$age."'  ";
  $result= mysql_query($query);
  while  ($query_row = mysql_fetch_array($result)) {

   echo " <option value = '".$query_row[0]."'> $query_row[0] </option> ";

  }

 ?>

I don't get any error messages, i am sure your point is right but this solution didn't work unfortunately! thank you so much for helping me :)

解决方案

You didn't send the name in the second ajax request but you need it for your database query, so you'll need to send the name as well as the age in the ajax request. Also you aren't sanitizing your input, you must always validate user input, I'd also suggest not using mysql_*

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

var age = encodeURIComponent(document.ajax.age.value),
var name = encodeURIComponent(document.ajax.name.value),
xmlhttp.open('GET', 'country.inc.php?age='+age+'&name'+name, true );

if (isset($_GET['age'], $_GET['name']) ) {
    $age = $_GET['age'];
    $name = $_GET['name'];
    ...
}     

这篇关于使用Ajax和3个连续的下拉列表中视海誓山盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆