繁体   English   中英

如何使用 Ajax 从选择中获取值并在 PHP 中使用

[英]How to get a value from select with Ajax and use in PHP

正如我在标题中所写的那样,我在子文件夹 admin 中的 page.php 页面中有这部分代码。 所以页面的路径是../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";

我使用我在互联网上找到的一个函数:

function fetch_select(val)
          {
             $.ajax({
               type: 'post',
               url: 'page.php',
               data: {
                 get_option:val
               },
               success: function (response) {
                 document.getElementById("my_select").innerHTML=response; 
               }
             });
          }

我必须做什么才能获得 $var 的价值? 因为我需要这个值来构建其他东西。 是否可以?

编辑:可能我没有很好地解释我的问题。 我对 ajax 不是很好,因为我从不使用它。 我有一个截止日期,所以我现在不能学习。 现在我有这种情况:我有一个带有输入提交的选择表单。 单击按钮后,我使用 $_POST['myoption'] 并获得值。 然后我这样做:

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

这项工作正确。 我需要更改它并在同一页面中使用。 我怎样才能做同样的事情?

你不需要做一个 POST 来做到这一点,你可以用 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 提供选择值:

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM