我想:
使用ajax将数据从index.php文件发送到php
然后根据它在数据库中找到合适的值
并将返回的值发送到index.php并将其放入“经度”输入中。
我真的不知道该怎么做。
例如:我在输入“bar”中写“lala”,然后点击“提交”按钮,然后在Nombre = "lala"
的数据库中找到“经度”,最后它出现在“经度”输入中。
的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(发送值),然后取数据(经度)并放到经度:
$(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 ));
答案 0 :(得分:0)
将返回的值发送到index.php并将其放入“经度”输入中。
如果一切顺利,请尝试此
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'http://localhost/inne/phonegap_test/agregar.php',
data: $('form').serialize(),
//dont forget set return back
success: function ( e) {
//parse the data from json
var data = JSON.parse(e);
//set into longitude input
$('#longitude').val(data.longitude);
alert('form was submitted');
}
});
});
});
和你的php代码
if ($result2->num_rows > 0) {
$row2 = $result2->fetch_assoc();
$cal2 = $row2["longitude"];
}
else{
$cal2 = "Nie znalazło miasta";
}
return json_encode(array( "longitude" => $cal2 ));