Fir*_*xII 0 php jquery select onchange
我想将选择与输入文本“连接”而不刷新页面
这是我的例子
<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) ?>
Run Code Online (Sandbox Code Playgroud)
然后在 var $name 中有来自 db 的值
<input type="text" value="<?php echo $name; ?>" />
Run Code Online (Sandbox Code Playgroud)
我想我必须使用 jquery post/get 但如何只更新我需要检查数据库的 var?
就像 Thamizhan 在评论中所说,你需要使用 AJAX。
这是一个简单的例子:
HTML(索引.html)
<select name="name_select" id="my_select">
<option value="1">first</option>
<option value="2">first</option>
<option value="3">first</option>
</select>
Run Code Online (Sandbox Code Playgroud)
JS
$('#my_select').on('change', function () {
var selectData = $(this).val();
$.ajax({
type: "POST",
url: "custom.php",
data: {'selectData': selectData },
success: function (json) {
// do things
}
});
}
Run Code Online (Sandbox Code Playgroud)
PHP(自定义.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 ;)
}
Run Code Online (Sandbox Code Playgroud)