admin管理员组文章数量:1391805
In my database I have table Person (id, name, last_name, phone_number)
. I do something like this:
$queryPerson = mysqli_query($con, 'SELECT * FROM Person');
while ($person = mysqli_fetch_array($queryPerson)) {
echo '<option value="'.$person["id"].'">'.$person["name"].' '.$person["last_name"].'</option>';
}
I want to use javascipt function to copy selected value from the select to textboxes:
function copyPerson(data) {
document.getElementById["name"].value = data.value;
}
...but instead of their id I want their names and last names and phone numbers to be displayed in textboxes. Now I came up with this idea to read the value from an option, send query to DB SELECT * FROM Person WHERE id = ID_FROM_SELECT
and then recieve the data I want to recieve. I'm not sure if it's a good idea though.
In before: yes, I need to have $person["id"]
as a value of an option.
My question is: sending the query to DB is a good idea - if yes, how can I send a value from javascript to MySQL, if no - what is a better solution?
EDIT: Apart from @Thanos Tourikas answer, I found this link to be helpful: .asp
In my database I have table Person (id, name, last_name, phone_number)
. I do something like this:
$queryPerson = mysqli_query($con, 'SELECT * FROM Person');
while ($person = mysqli_fetch_array($queryPerson)) {
echo '<option value="'.$person["id"].'">'.$person["name"].' '.$person["last_name"].'</option>';
}
I want to use javascipt function to copy selected value from the select to textboxes:
function copyPerson(data) {
document.getElementById["name"].value = data.value;
}
...but instead of their id I want their names and last names and phone numbers to be displayed in textboxes. Now I came up with this idea to read the value from an option, send query to DB SELECT * FROM Person WHERE id = ID_FROM_SELECT
and then recieve the data I want to recieve. I'm not sure if it's a good idea though.
In before: yes, I need to have $person["id"]
as a value of an option.
My question is: sending the query to DB is a good idea - if yes, how can I send a value from javascript to MySQL, if no - what is a better solution?
EDIT: Apart from @Thanos Tourikas answer, I found this link to be helpful: http://www.w3schools./php/php_ajax_database.asp Share Improve this question edited Sep 1, 2014 at 13:41 whiteestee asked Aug 30, 2014 at 10:55 whiteesteewhiteestee 3011 gold badge4 silver badges12 bronze badges 3
- your question is not clear and The mysql_query($sql,$con) – Mohammad Kermani Commented Aug 30, 2014 at 10:58
-
questions - fixed, my
mysqli_query
syntax is correct – whiteestee Commented Aug 30, 2014 at 10:59 - Isn't mysqli deprecated now? Should probably be using PDO, Yes you can send the query to the database you'll need to use AJAX to call a php script that will run your query and return a result array which you can loop through – Halfpint Commented Aug 30, 2014 at 15:47
1 Answer
Reset to default 4Generally it is not a good idea to send the query.
The solution would be to send the id with ajax and have a php page that handles that request.
In that php page, you just need to get the id, which is sent as a parameter to the request, and construct there the db query. Then just echo the result as a json object and handle that response with JavaScript.
First, use jQuery to make a request to the server sending the person id:
$.ajax({
url: 'url_that_handles_request.php',
data: { id: person_id },
dataType: 'json',
success: function(response){
// here you handle the response from server.
// you can access the data returned doing something like:
var id = response.id;
var name = response.name;
}
});
And then, you need to provide a php page to handle the ajax call:
$person_id = $_POST["person_id"];
// here you make the query to the database using the person id
// and then just echo the db response as a json object
echo json_encode($db_response);
Here are some useful links
A quick tutorial for jQuery and how to install it: http://www.w3schools./jquery/default.asp
A quick tutorial for jQuery ajax: http://www.w3schools./jquery/jquery_ajax_intro.asp
Some references in the ajax methods that jQuery provides: http://www.w3schools./jquery/jquery_ref_ajax.asp
A tutorial about json: http://www.w3schools./json/
And finally a documentation about json_encode php function: http://php/manual/en/function.json-encode.php
本文标签: phpJavaScript retrieve data from DBStack Overflow
版权声明:本文标题:php - JavaScript retrieve data from DB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744761872a2623801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论