Javascript 如何使用Ajax从select中获取值并在PHP中使用

Javascript 如何使用Ajax从select中获取值并在PHP中使用,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,正如我在标题中所写,我在page.php的子文件夹admin中有这部分代码。因此页面的路径是../admin/page.php: <select name="my_select" id="my_select" onchange="function(this.value);"> <?php include "../db/db_config.php"; $conn =

正如我在标题中所写,我在page.php的子文件夹admin中有这部分代码。因此页面的路径是../admin/page.php:

<select name="my_select" id="my_select" onchange="function(this.value);">
                <?php
                    include "../db/db_config.php";

                    $conn = mysql_connect($host,$user,$password)or die(mysql_error());

                    mysql_select_db($db, $conn);
                    $query="Query";
                    $res=mysql_query($query,$conn);
                    while($row=mysql_fetch_array($res)){
                        $id=$row['id'];
                        $text=$row['text'];
                        echo "<option value='$id'>$text</option>";
                        }
                    }
                ?>
              </select>
    $var = $_POST['my_select'];
    echo "I have selected $var";
我必须做什么才能获得$var的价值?因为我需要这个值来构建其他东西。可能吗

编辑: 也许我没有很好地解释我的问题。我对ajax的使用不是很好,因为我从未使用过它。我有一个最后期限,所以我现在不能学习。 现在我有这样的情况: 我有一个带有输入提交的select表单。单击按钮后,我使用$\u POST['myoption']并获得值。 然后我做:

if($var == 1)
//a query from database
else if($var == 2)
//another different query
else
//other but no query

这项工作做得很正确。我需要更改它并在同一页中使用。我怎么做才能做到这一点呢?

你不需要写文章就可以用jQuery来做

<?php
include "../db/db_config.php";

$conn = mysql_connect($host,$user,$password)or die(mysql_error());

mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
?>

<select name="my_select" id="my_select">

    <?php
    while($row=mysql_fetch_array($res)){
        $id=$row['id'];
        $text=$row['text'];
        echo "<option value='$id'>$text</option>";
    }
    ?>
</select>
<span id="selected"></span>

<script>
$("#my_select").change(function() {
    $("#selected").html($("#my_select option:selected").text());
});
</script>
这将为PHP提供select值:

<?php
include "../db/db_config.php";

$conn = mysql_connect($host,$user,$password)or die(mysql_error());

mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);

if (isset($_POST['my_select'])) {
    $var = $_POST['my_select'];
} else {
    $var = '';
}
?>
<form id="my_form" action="" method="POST">
    <select name="my_select" id="my_select">

        <?php
        while($row=mysql_fetch_array($res)){
            $id=$row['id'];
            $text=$row['text'];
            echo "<option value='$id'>$text</option>";
        }
        ?>
    </select>
</form>
<span id="selected">I have selected <?php echo $var; ?></span>

<script>
$("#my_select").change(function() {
    $('#my_form').submit();
});
</script>

$var1不应该是$I我不清楚你想达到什么目标。第一块代码是否在单独的页面上?如果是这样,你就不能做你想做的事。无法获取驻留在单独页面上的select值。谁在选择该页上的值???也许可以澄清您的意图以及希望看到的结果。javascript函数应该做什么?我想在同一页中获取值@是的,我犯了一个错误,但我改变了it@devlin我有一个精选的。我希望选择的选项值到达一个变量PHP。可能吗?好的!我可以把这个值放在$var PHP中吗?这样就可以了。我只是将db连接部分从中移出。要将所选选项返回给PHP,您必须通过ajax或表单执行POST或GET。@fluinc我尝试使用ajax。但可能是我弄错了。我发布的函数是ajax吗?我在网上搜索我发布的第二个
<?php
include "../db/db_config.php";

$conn = mysql_connect($host,$user,$password)or die(mysql_error());

mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);

if (isset($_POST['my_select'])) {
    $var = $_POST['my_select'];
} else {
    $var = '';
}
?>
<form id="my_form" action="" method="POST">
    <select name="my_select" id="my_select">

        <?php
        while($row=mysql_fetch_array($res)){
            $id=$row['id'];
            $text=$row['text'];
            echo "<option value='$id'>$text</option>";
        }
        ?>
    </select>
</form>
<span id="selected">I have selected <?php echo $var; ?></span>

<script>
$("#my_select").change(function() {
    $('#my_form').submit();
});
</script>