【发布时间】:2020-10-15 11:44:15
【问题描述】:
我的 PHP 文件中有一个按钮,当我单击该按钮时,我希望运行另一个 PHP 文件并将一些数据保存在 MySQL 表中。为此,我正在使用此链接 (How to call a PHP function on the click of a button) 中建议的 AJAX 调用,这是 StackOverflow 本身的答案。
这是我的 show_schedule 文件,我试图从中执行另一个 PHP 文件的代码:
$('.edit').click(function() {
var place_type = $(this).attr("id");
console.log(place_type);
$.ajax({
type: "POST",
url: "foursquare_api_call.php",
data: { place_type: place_type }
}).done(function( data ) {
alert("foursquare api called");
$('#userModal_2').modal('show');
});
});
这里的“编辑”是按钮的类,并且该按钮的 id 正在控制台中正确打印。
这是我的foursquare_api_call.php 文件(应该在单击按钮时运行):
<?php
session_start();
include('connection.php');
if(isset($_POST['place_type'])){
$city = $_SESSION['city'];
$s_id = $_SESSION['sid'];
$query = $_POST['place_type'];
echo "<script>console.log('inside if, before url')</script>";
$url = "https://api.foursquare.com/v2/venues/search?client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET&v=20180323&limit=10&near=$city&query=$query";
$json = file_get_contents($url);
echo "<script>console.log('inside if, after url')</script>";
$obj = json_decode($json,true);
for($i=0;$i<sizeof($obj['response']['venues']);$i++){
$name = $obj['response']['venues'][$i]['name'];
$latitude = $obj['response']['venues'][$i]['location']['lat'];
$longitude = $obj['response']['venues'][$i]['location']['lng'];
$address = $obj['response']['venues'][$i]['location']['address'];
if(isset($address)){
$statement = $connection->prepare("INSERT INTO temp (name, latitude, longitude, address) VALUES ($name, $latitude, $longitude, $address)");
$result = $statement->execute();
}
else{
$statement = $connection->prepare("INSERT INTO temp (name, latitude, longitude) VALUES ($name, $latitude, $longitude)");
$result = $statement->execute();
}
}
}
?>
console.log 没有记录在控制台中,“temp”表也没有更新。谁能告诉我我在哪里犯错?或者甚至可以像这样执行PHP文件的代码?
【问题讨论】:
-
你检查了网络标签吗?
-
“foursquare_api_call.php”文件有一个条目。
-
那么当你点击它时会有什么反应
-
先显示警告信息,再显示模型。
-
但“temp”表中没有条目。
标签: javascript php jquery mysql ajax