1

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.

2
  • Did you get anything when you console log obj? Commented May 20, 2016 at 9:16
  • change your ajax call type to type: "GET" Commented May 20, 2016 at 9:23

4 Answers 4

1

You are making a POST request:

type: "POST",

… but are trying to read the data from the query string:

$town = $_GET['town'];

jQuery will post data in the request body when you make a POST request, so it will appear in $_POST not $_GET.

Change type: "POST", to type: "GET", (or omit it entirely as GET is the default).

Sign up to request clarification or add additional context in comments.

Comments

0

if you are checking whether the variable exist are not :

if (town != "undefined") {} should be if (typeof town !== "undefined") {}

Comments

0

Change $town = $_GET['town']; to $town = $_POST['town']; as you used type: "POST", in your ajax request.

Comments

-1

Please pass the town name with url encoded with javascript function encodeURI and please debug the code whether your php code returns the town list or not

    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) {
console.log(obj);

        document.getElementById("selectedLocation").value = obj[0]["name"];
        document.getElementById("placeDescriptionBox").value = obj[0]["description"];
    }
});

}

2 Comments

jQuery will encode data apropriate when you pass an object to data, you only need to encode it manually if you are passing data a string (and then you should be using encodeURIComponent, not encodeURI.
Oh! sorry i forgot that appreciate for your feedback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.