簡體   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