Ajax JQuery post code with Php to query MySql database not working

2016-05-20T17:14:34

I have an HTML form that runs a Javascript function on page load. In the Javascript function, a town name is sent via Ajax post call to Php function to get details from MySql database.

Ajax call in JavaScript function

var town = "Town name";          //depends on the previous execution in JS function
if (town != "undefined") {
    jQuery.ajax({
        type: "POST",
        url: '../model/data_access/GetCity.php',
        dataType: 'json',
        data: {town: town},
        success: function (obj) {
            document.getElementById("selectedLocation").value = obj[0]["name"];
            document.getElementById("placeDescriptionBox").value = obj[0]["description"];
        }
    });
}

GetCity.php

$town = $_POST['town'];
get_city($town);

function get_city($town)
{
    $place = array();
    $db_conn = DBConnection::get_database_connection(); 
    $stmt = $db_conn->prepare("SELECT * FROM place WHERE name=?");
    $stmt->bind_param("s", $town);
    $stmt->execute();

    if (!($result = $stmt->get_result())) {
        echo "Error" . $stmt->error;
    }

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $place[0] = new Place();
        $place[0]->set_name($row["name"]);
        $place[0]->set_description($row["description"]);
        echo json_encode($place);
    }
}

The data from database are not displayed in the HTML form. How can I solve this? I'm new to Ajax JQuery so any help is much appreciated.

Copyright License:
Author:「Fleur」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/37342687/ajax-jquery-post-code-with-php-to-query-mysql-database-not-working

About “Ajax JQuery post code with Php to query MySql database not working” questions

I have an HTML form that runs a Javascript function on page load. In the Javascript function, a town name is sent via Ajax post call to Php function to get details from MySql database. Ajax call in
With thanks to Elf Sternberg (I couldn't get your solution working, sorry), I think I am heading in the right direction, but I am having trouble writing the php to the mysql database. The origina...
I usually work with .NET but I have a php page that calls an ajax post through another php script with parameters, then calling a stored procedured. But it does not work when trying to write to the
I am trying to use Ajax post to send data from my local sqlite database in my phonegap app to my remote database. I have checked the php file and it is working fine and is inserting into the my rem...
I see a lot of questions like this, however, I am having trouble even getting into the .ajax() function. I had a previous issue resolved here: Accessing clicked cell instead of all cells using JQue...
I am trying to use JQuery AJAX to allow the user to input information into a form in a pop-over box, and then the information gets added to a MySQL database. However, for some reason, when I use the
I am trying to learn Ajax. I am inserting some data to mysql database from a Html Form by php. It works nicely. But my ajax part does not work. I get the success message but data dont go to my php ...
I have a form with a "select" box. When the box is selected, I want the appropriate records from the database to be shown on the same page. There are two files involved: an HTML page that contains...
I am trying to update mysql database table with button click. But database is not getting updated... HTML Code : <tr > <td>Advt Heading :</td> <td> <input type=&q
I have a page that shows inventory. When inventory becomes defective (or any other status) I would like to select that option from a dropdown, and update the database accordingly without a refresh ...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.