最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - JavaScript retrieve data from DB - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

Generally 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

发布评论

评论列表(0)

  1. 暂无评论