2013-03-07 127 views
0

singleselect.html静态下拉菜单使用AJAX,PHP

<html> 
    <script> 
    function singleselect(str) 
    { 
     var xmlhttp; 
     if(str.length == 0) 
     { 
     document.getElementById("sing").innerHTML=""; 
     return; 
     } 

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

     xmlhtttp.onreadystatechange = function() 
     { 
     if(xmlhttp.readyState == 4) 
     { 
      document.getElementById("sing").innerHTML = xmlhttp.responsetext; 
     } 
     } 
     xmlhttp.open("POST","singleselect.php?s=" +str,true); 
     xmlhttp.send(); 
    } 
    </script> 

    <style> 
    div { font-family:verdana; font-size:13px; margin-left:400px; margin-top:100px; } 
    </style> 
    <body> 
    <div> 
    Select a country : 
     <select onchange="document(this.value)"> 
     <option>Select a country</option> 
     <option value="0">INDIA</option> 
     <option value="1">United States of America</option> 
     <option value="2">United Kingdom</option> 
     <option value="3">Australia</option> 
    </select> 
    </div> 

    <div id="sing"></div> 
    </body> 
</html> 

singleselect.php

<?php  

$store[0] = "Please select a country"; 
$store[1] = "Andhra Pradesh"; 
$store[2] = "New York"; 
$store[3] = "London"; 
$store[4] = "Austraila"; 

$s = $_REQUEST['s']; 

?> 

当我点击一个国家,应该显示关系到一个国家的状态列表,我想在PHP中使用静态数据库来完成。因为我是jQuery和AJAX。我需要你的指导。

+0

你是什么意思静态?如果您从selectbox传送值,则需要

+0

但我想使用静态内容而不是从数据库中使用 – ronbosc 2013-03-07 07:52:10

+0

您写道“我想在PHP中使用静态数据库来完成,但使用数据库”,所以你让我感到困惑。那么你将不得不在文本文件或CSV或任何其他国家的这个国家的名单,并以这种方式格式化,以便您可以轻松阅读。例如,如果你使用文本文件,也许你可以像这样格式化国家:城市;然后读取文件explode(),以便获取数组并搜索所需的值。搜索网络阅读txt文件和爆炸()函数 – vodich 2013-03-07 07:57:21

回答

0

如果你希望你在arrauy城市数据也许你可以做这样的事情:

$store[0] = "Please select a country"; 
$store[1][0] = "Andhra Pradesh"; 
$store[2][0] = "New York"; 
$store[3][0] = "London"; 
$store[4][0] = "Austraila"; 
$c= $_POST['s']; 
if(isset($store[$c]) && is_array($store[$c])){ 
    foreach($store[$c] as $k=>$v){ 
     echo $v; 
    } 
} 
0

我觉得这是你想要你的状态是什么。此外,使用国家代码而不是数字索引会更好。

// states.php 

$states = array (
'IN' => array (
    'indian state', 
    'indian state 2'    
    ), 
'US' => array (
    'US state', 
    'US state 2' 
    ) 
); 

$select = ''; 
if(isset($_POST['s']) && isset($states[$_POST['s']])) { 
$select .= '<select name="states">'; 
foreach($states[$_POST['s']] as $state) { 
     $select .= '<option value="' . $state . '">' . $state . '</option>'; 
} 
$select .= '</select>';  
echo $select; 
} 
0

我不知道你想要什么,但我知道你想要一个国家下拉列表根据国家。所以你需要做到这一点singleselect.php

$state_list = array(
'0' => array(
    'state 1', 
    'state 2', 
    'state 3' 
    ), 
'1' => array(
    'state 4', 
    'state 5', 
    'state 6' 
    ), 
'2' => array(
    'state 7', 
    'state 8', 
    'state 9' 
    ) 
); 

$select = '<select name="state">'; 
foreach($state_list[$_REQUEST['s']] as $key => $value) 
{ 
    $select .= '<option value="'.$key.'">'.$value.'</option>'; 
} 
$select .= '</select>'; 

echo $select; 

也许它会帮助你。