Reputation: 828
I want to "connect" a select with a input text without refresh the page
This is my example
<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) ?>
Then in var $name there is value from db
<input type="text" value="<?php echo $name; ?>" />
I think I must use jquery post/get but how to update only var that I need to check db?
Upvotes: 0
Views: 15317
Reputation: 11
Please clear one thing why you need sql query here.
<option value="1">first</option>
first is name and 1 is option there. then we can put first or 1 into input box without using sql and ajax.
use only jquery for this
$(document).ready(function(){
$("#my_select").change(function(){
var thisval = $(this).val();
$("input").val(thisval);
});
});
Upvotes: 0
Reputation: 913
$("#my_select").on("change", function () {
$.ajax({
url: "phpfile.php",
method: 'POST',
data: {val: $(this).val()},
success: function (data) {
$("[type='text']").val(data);
}
});
});
In phpfile.php
include("db.php");
// ... your code ...
// execute sql(SELECT name FROM table WHERE id = value) and return value.
Upvotes: 0
Reputation: 8461
Like Thamizhan say in comment, you need to use AJAX.
Here is a simple example :
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 ;)
}
Upvotes: 5