I would like to:
send data from the index.php file using ajax to php
then based on it find the appropriate value in the database
and send the returned value to index.php and put it in "longitude" input.
I really have no idea how to do it.
For example: I write "lala" in input "bar", then click "submit" button and then find "longitude" in database where Nombre = "lala"
and finally it appears in "longitude" input.
index.php:
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<input id="longitude" name="longitude" type="text" value="" />
index.js (send value) and next take data (longitude) and put to longitude:
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'http://localhost/inne/phonegap_test/agregar.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
agregar.php:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inzynierka_test2";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$val1 = $_POST['bar'];
$sql2 = "SELECT `longitude` FROM `lugar` WHERE `Nombre`= $val1 ORDER BY `ID` DESC LIMIT 1";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
$row2 = $result2->fetch_assoc();
$cal2 = $row2["longitude"];
}
else{
echo "Nie znalazło miasta";
}
echo json_encode(array( "longitude" => $cal2 ));