2012-09-08 72 views
0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 
    <script type="text/javascript"> 
    function load(thediv, thefile) 
    { 
     if (window.XMLHttpRequest) 
     { 
     xmlhttp = new XMLHttpRequest(); 
     } 
      else 
      { 
      xmlhttp = new ActiveXObject('Microsoft.XMLHTTP)'); 
      } 

       xmlhttp.onreadystatechange = function() 
       { 
        if (xmlhttp.readyState==4 && xmlhttp.status==200) 
        { 
        document.getElementById(thediv) .innerHTML = xmlhttp.responseText; 
        } 
       } 

       xmlhttp.open('GET', thefile, true); 
       xmlhttp.send(); 

    } 
    </script> 
</head> 

    <body> 

<?php 

//connection to db and mysql query 


    $result = mysql_query($query) or die(mysql_error()); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

$id=$row["idProducts"]; 
$thing=$row["country"]; 
$options.="<OPTION VALUE=\"$id\">".$thing.'</option>'; 
} 
mysql_close(); 
?> 


<SELECT id="countrySearch" NAME=countrySearch onchange="load('divtest', 'step2.search.php')";> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT> 

<div id="divtest"> 
test 
</div> 

</body> 

step2.search.php包括:如何通過ajax發送變量到php?

<?php 
echo "I want it to store the users selection as a variable for php to use"; 
?> 

我已經是我想要存儲什麼用戶從下拉框中選擇在PHP中使用它用做一個MySQL查詢的問題從用戶選擇表單變量來形成mysql語句的WHERE部分。然後使用ajax將新數據放入「divtest」中。

如何將用戶選擇存儲到變量中,然後將其發送到step2.search.php中使用?

回答

0
See Code Below 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 
    <script type="text/javascript"> 
    function load(thediv, thefile,selectvalue) 
    { 
     if (window.XMLHttpRequest) 
     { 
     xmlhttp = new XMLHttpRequest(); 
     } 
      else 
      { 
      xmlhttp = new ActiveXObject('Microsoft.XMLHTTP)'); 
      } 

       xmlhttp.onreadystatechange = function() 
       { 
        if (xmlhttp.readyState==4 && xmlhttp.status==200) 
        { 
        document.getElementById(thediv) .innerHTML = xmlhttp.responseText; 
        } 
       } 


       xmlhttp.open("GET",thefile+"?q="+selectvalue,true); 
       xmlhttp.send(); 

    } 
    </script> 
</head> 

    <body> 
    <form> 

<?php 

//connection to db and mysql query 


    $result = mysql_query($query) or die(mysql_error()); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

$id=$row["idProducts"]; 
$thing=$row["country"]; 
$options.="<OPTION VALUE=\"$id\">".$thing.'</option>'; 
} 
mysql_close(); 
?> 


<SELECT id="countrySearch" NAME=countrySearch onchange="load('divtest', 'step2.search.php', this.value)";> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT> 

<div id="divtest"> 
test 
</div> 
</form> 

step2.search.php的

<?php 
    Simple grab this variable 
    $id = $_GET['q']; 

    //do your query stuffhere 
?> 
+0

'this.options [this.selectedIndex] .value'代替'this.value'? – Peter

+0

非常感謝你的工作我認爲! – Dee1983