简体   繁体   中英

Call a PHP File with AJAX

I have been looking at other questions and based my code out of the corresponding answers, but I can't seem to make this work. I would appreciate if someone more experienced than me can help me see clearer on this one.

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
}

My HTML buttons do have the class 'going-decision'. jQuery library is included.

Result: The 'clicked' alert is displaying, along with the 'success' one. However, the database is not updated.

Anyone has a hint on what's going on here? Thanks.

EDIT, here's my PHP code. I know it's non-secure code, i'm currently learning 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;
}

?>

EDIT#2: FORM

<!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>

EDIT#3: Solution

The issue is, indeed, from my PHP script. In the functions going(), and notGoing(), the statement

if (!empty($result))

will not work as expected... If I interchange the if and the else statements, everything works fine...

Why would an empty result from mySQL would not be empty for that statement?

I am hoping that I am wrong, but you SQL query has a mistake.

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

No closing bracket for condition, which should break the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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