简体   繁体   English

从Javascript调用PHP文件(AJAX?)

[英]Call a PHP file from Javascript (AJAX?)

The Problem: 问题:

Hello, I have researched AJAX online and i don't really understand it. 您好,我在网上研究了AJAX,但我不太了解。 I am trying to call a php file from within a javascript function and unfortunately it is not working. 我试图从javascript函数中调用php文件,不幸的是它无法正常工作。 I am trying everything but i still do not get it. 我正在尝试一切,但我仍然不明白。 Hopefully people here can explain it to me with my code as an example. 希望这里的人可以以我的代码为例向我解释。


Javascript: Javascript:

Here is the function I want to call the file from: 这是我要从中调用文件的函数:

function countdownEnded() {
    //make serverscreen dissapear
        document.getElementById('serverScreenWrapper').style.display = 'none';
        document.getElementById('serverScreenWrapper').style.opacity = '0';
        document.getElementById("cashOutNumTwo").style.right = '150%';
        document.getElementById("cashOutNumOne").style.right = '150%';
//start Timer
        setInterval(gameTimer.update, 1000);
//make player move again
        socket.emit('4');
        socket.emit('6');
//make game appear
        document.getElementById('gameAreaWrapper').style.opacity = 1;
//play sound
        document.getElementById('spawn_cell').play();
//cut 5 cents from account - php function
        //CALL PHP FUNCTION HERE WITH AJAX
}

So the php file is basically | 因此,php文件基本上是| ../../../../subtract5.php | ../../../../subtract5.php | in relation to the other file. 关于另一个文件。


PHP: PHP:

Here is the file: 这是文件:

<?php
session_start();

$servername = "localhost";
$username = "myUser";
$password = "myPass";
$dbname = "myDBname";
$cash_amount = $_SESSION['cash_amount'];

// Create connection

$userid = $_SESSION['id'];

// You must enter the user's id here. /\

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch the existing value of the cash_amount against that particular user here. You can use the SELECT cash_amount from users where userid = $userid
$_SESSION['cash_amount'] -= 0.05;
$newAmount = $cash_amount - 0.05;

$sql = "UPDATE users SET cash_amount = $newAmount WHERE id = $userid";
$result = $conn->query($sql);

if($result)
{
   echo "5 cents have been subtracted!";
}
else
{
   echo mysqli_error($conn);
   session_start();
   session_unset();
   session_destroy();
}

$conn->close();
?>

Conclusion: 结论:

How can i call this file from the function, and as a bonus: if i wanted to send a javascript variable over to the file to be used to update my database, how can i do that? 如何从函数中调用此文件,以及作为奖励:如果我想将javascript变量发送到该文件以用于更新数据库,该如何做?

Thanks so much for your help! 非常感谢你的帮助!

Write this part of code your javascript function 将这段代码写成您的javascript函数

function countdownEnded() {
       /* Your Code */
     $.ajax({
        type: "POST",
        url: '/subtract5.php',
        data: { },
        success: function (data) {
            alert(data);
        }
    });
}

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

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