繁体   English   中英

使用AJAX调用PHP文件

[英]Call a PHP File with AJAX

我一直在寻找其他问题,并从相应的答案中得出了自己的代码,但我似乎无法实现这一目标。 如果有人比我经验丰富,可以帮助我更清楚地了解这一点,我将不胜感激。

jQuery的:

$(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');
        }
    });
});
});

event_going_ajax.php:

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;
}

?>

编辑#2:表格

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta name="generator" content="Bootply" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Event</title>
    <link href = "css/main.css" rel = "stylesheet" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">     </script>
    <script src="js/event.js"></script>
</head>
<body>
    <button type = "submit" id = "go-btn" class = "going-decision" value="going.' . $event_id . '">Going</button>
    <button type = "submit" id = "no-go-btn" class = "going-decision" value="not_going.' . $event_id . '">Not Going</button>
</body>

编辑#3:解决方案

实际上,问题出在我的PHP脚本中。 在函数going()和notGoing()中,该语句

if (!empty($result))

将无法按预期方式工作...如果我互换if和else语句,则一切正常...

为什么mySQL的空结果对于该语句不会为空?

我希望我错了,但是您的SQL查询有一个错误。

$sql = "INSERT INTO user_events (user_id,event_id) "
            . "VALUES ("
            . "'$user_id'"
            . ","
            . "'$event_id'"; // should be ."'$event_id')"

没有条件的右括号,这会中断查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM