簡體   English   中英

選擇onchange更新php值

[英]select onchange update php value

我想用輸入文本“連接”選擇而不刷新頁面

這是我的例子

<select name="name_select" id="my_select">
  <option value="1">first</option>
  <option value="2">first</option>
  <option value="3">first</option>
</select>

<?php ...query(SELECT name FROM table WHERE id = value) ?>

然后在var $ name中有db的值

<input type="text" value="<?php echo $name; ?>" />

我想我必須使用jquery post / get,但是如何僅更新需要檢查數據庫的var?

就像Thamizhan在評論中說的那樣,您需要使用AJAX。

這是一個簡單的例子:

HTML (index.html)

<select name="name_select" id="my_select">
  <option value="1">first</option>
  <option value="2">first</option>
  <option value="3">first</option>
</select>

JS

$('#my_select').on('change', function () {
    var selectData = $(this).val();
    $.ajax({
        type: "POST",
        url: "custom.php",
        data: {'selectData': selectData },
        success: function (json) {
            // do things
        }
    });
}

PHP (custom.php)

if (isset($_POST['selectData'])) {
    var_dump($_POST['selectData']); // $_POST['selectData'] is the selected value
    // query here
    // and you can return the result if you want to do some things cool ;)
}
$("#my_select").on("change", function () {
   $.ajax({
        url: "phpfile.php",
        method: 'POST',
        data: {val: $(this).val()},
        success: function (data) {
            $("[type='text']").val(data);

        }
    });
});

在phpfile.php中

include("db.php");

// ... your code ...

// execute sql(SELECT name FROM table WHERE id = value) and return value.

請在此處清除一件事,為什么您需要sql查詢。

<option value="1">first</option>

首先是名稱,那里是1。 那么我們可以在不使用sql和ajax的情況下將first或1放入輸入框。

為此只使用jQuery

$(document).ready(function(){
  $("#my_select").change(function(){

    var thisval = $(this).val();    
    $("input").val(thisval);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM