使用AJAX调用PHP文件
我一直在研究其他问题,并根据相应的答案编写代码,但我似乎无法做到这一点。如果有比我更有经验的人能帮我看得更清楚,我将不胜感激 jQuery:使用AJAX调用PHP文件,php,jquery,ajax,Php,Jquery,Ajax,我一直在研究其他问题,并根据相应的答案编写代码,但我似乎无法做到这一点。如果有比我更有经验的人能帮我看得更清楚,我将不胜感激 jQuery: $(function () { $(".going-decision").click(function () { var clickBtnValue = $(this).val(); alert('clicked'); $.ajax({ type: "POST", url: "http://local
$(function () {
$(".going-decision").click(function () {
var clickBtnValue = $(this).val();
alert('clicked');
$.ajax({
type: "POST",
url: "http://localhost/townbuddies/public_html/event_going_ajax.php",
data: {'btn-clicked': clickBtnValue},
success: function () {
alert('success');
}
});
});
});
if (isset($_POST['btn-clicked'])) {
$decision = $_POST['btn-clicked'];
//Logic that writes to database
}
event\u going\u ajax.php:
$(function () {
$(".going-decision").click(function () {
var clickBtnValue = $(this).val();
alert('clicked');
$.ajax({
type: "POST",
url: "http://localhost/townbuddies/public_html/event_going_ajax.php",
data: {'btn-clicked': clickBtnValue},
success: function () {
alert('success');
}
});
});
});
if (isset($_POST['btn-clicked'])) {
$decision = $_POST['btn-clicked'];
//Logic that writes to database
}
我的HTML按钮确实有“going decision”类。包括jQuery库
结果:将显示“单击”警报以及“成功”警报。但是,数据库没有更新
有人知道这里发生了什么吗?谢谢
编辑,这是我的PHP代码。我知道这是不安全的代码,我目前正在学习PHP。
<?php
session_start();
$user_id = $_SESSION['user'];
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'townbuddies');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (isset($_POST['btn-clicked'])) {
//Value will be in this format: 'decision.event_id'
$value = $_POST['btn-clicked'];
$length = strlen($value);
$findme = '.';
$pos = strpos($value, $findme);
//Separating the decision (going/not going) and the event_id
$decision = substr($value, 0, $pos);
$event_id = substr($value, $pos + 1);
//Verify if user is already 'going' to the event
$result = verifyDatabase($con, $user_id, $event_id);
//Depending on user decision
switch ($decision) {
case 'going':
going($con, $result, $user_id, $event_id);
break;
case 'not_going':
notGoing($con, $result, $user_id, $event_id);
break;
}
}
function verifyDatabase($con, $user_id, $event_id) {
//Verify if already in database
$sql = "SELECT * "
. "FROM user_events "
. "WHERE event_id = "
. "'$event_id'"
. "AND user_id = "
. "'$user_id'";
$result = mysqli_query($con, $sql);
return $result;
}
function going($con, $result, $user_id, $event_id) {
//If already in database as 'going', then do not duplicate. Do nothing.
if (!empty($result)) {
header("Location: http://localhost/townbuddies/public_html/event.php?event=" . $event_id);
exit;
}
//Not in database as 'going', so add it.
else {
$sql = "INSERT INTO user_events (user_id,event_id) "
. "VALUES ("
. "'$user_id'"
. ","
. "'$event_id'"
. ")";
if (!mysqli_query($con, $sql)) {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
//Update amount of people going.
$sql =
mysqli_close($con);
}
exit;
}
function notGoing($con, $result, $user_id, $event_id) {
//If already in database as 'going', then delete
if (!empty($result)) {
$sql = "DELETE FROM user_events "
. "WHERE user_id = "
. "'$user_id'"
. "AND event_id = "
. "'$event_id'";
if (!mysqli_query($con, $sql)) {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
}
exit;
}
?>
将无法按预期工作。。。如果我交换If和else语句,一切正常
为什么mySQL的空结果对于该语句来说不是空的?我希望我是错的,但是您的SQL查询有一个错误
$sql = "INSERT INTO user_events (user_id,event_id) "
. "VALUES ("
. "'$user_id'"
. ","
. "'$event_id'"; // should be ."'$event_id')"
条件没有结束括号,这将中断查询。If
alert('success')正在调用code>,那么您的问题是php文件事件_going_ajax.php
,而不是您的js代码。您能在控制台上看到请求吗?(F12)??我可以看看你的事件吗\u go\u ajax.php
代码?你能检查一下你的错误日志并告诉我们你得到了什么吗。如果你得到一个错误?如果你不明白我在说什么,你有一个更大的问题@LeaTano我可以在“网络”选项卡下看到请求。我将在几秒钟内编辑我的原始帖子,以显示所有php代码。非常感谢。编辑我的代码以包含PHP。正如mysqli_query
文档所说,“失败时返回FALSE。对于成功的选择、显示、描述或解释查询,mysqli_query()将返回mysqli_结果对象。对于其他成功的查询,mysqli_query()将返回TRUE。”因此,这就是它总是返回TRUE或FALSE的原因。谢谢。这并没有解决我遇到的问题,但肯定是我代码中的一个问题。