簡體   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