duanbi3151 2012-09-08 15:17
浏览 47
已采纳

如何使用ajax将变量发送到php?

<!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 consists of:

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

The problem I have is I want to store what the user selects from the drop down box and use it in php to do a mysql query using the variable from the user select form to form the WHERE part of the mysql statement. Then use ajax to put new data in "divtest".

How can I store the user selection into a variable then send it to be used in step2.search.php?

  • 写回答

1条回答 默认 最新

  • douxingti9307 2012-09-08 15:35
    关注
    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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势
  • ¥100 求连续两帧图像在水平和垂直上偏移
  • ¥15 mysql全文索引查找指定必须关键词word无效
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题