PHP的AJAX不工作结合我的PHP脚本和MySQL我的、脚本、工作、PHP

2023-09-10 17:21:24 作者:告诉我我该怎样做ゝ

所以在这里我张贴我的第一个PHP函数,我感到骄傲,但我刚刚了解了AJAX,想测试一下。不幸的是,我不能让它的工作。

我的经验: PHP(3周)。 CSS3,HTML,基本的JavaScript。

我的问题:获取AJAX工作。我想阿贾克斯从中获得的选票从我的测试服务器(Xampp时)数据库中的PHP文件得到我的数据。因此,每个用户点击好坏AJAX的时间应显示新的结果而无需刷新页面。问题是却是:A)我的if语句工作,通过检查使用isset($ _ POST),它不会工作了,如果我叫阿贾克斯。 B)preventing刷新。 C)制作后,每点击AJAX更新。我知道即时几乎没有,我只是失去了一些东西,我不知道它到底是什么是诚实的。

我的尝试:经过我的数据库连接。如果我的PHP code没有Ajax的工作检查,它完全没有问题(我只显示一半的功能,在这里,一个精简版的版本,为简单起见)。试图改变提交按钮。清除缓存。 jQuery是在我的文档的头部和路径是正确的。看了教程和读取文档,但我只是不往任何地方,可能是由于缺乏经验。

编辑:会话和PHP的一切工作正常。我我的会话开始和数据库连接都包括在最顶端。

摘要:如何解决这个AJAX,使其始终更新我的号码

Linux环境使用Shell脚本安装Mysql5.7

让我知道如果你要我解释我的PHP code部位。进出口愿意发表意见的部分,如果neccesary。

JQUERY / AJAX code

 函数票(){
        VAR请求= $阿贾克斯({
            网址:PHP /核心/ voting_system.php
            键入:POST,
            数据类型:HTML
        });

        request.done(函数(vote_sum){
            $(#内容)HTML(vote_sum)。
        });
}
 

HTML code:

 < D​​IV ID ='票'>< / DIV>

<形式ID =好行动=的方法=邮报>
    <输入类型=提交名称=好的onclick =投票()值=+>
< /形式GT;

<形式ID =坏行动=的方法=邮报>
    <输入类型=提交名称=坏的onclick =投票()值= - >
< /形式GT;
 

解决方案

在HTML你不需要<形式GT; ,你正在做它用 AJAX ,对吧?

 < D​​IV ID ='票'>< / DIV>
<按钮的onclick =投票(好);&GT + LT; /按钮>
<按钮的onclick =投票(坏);>  - < /按钮>
 

在JavaScript中,它更容易使用,而不是 AJAX 功能

 函数票(GB){
  $。员额(PHP /核心/ voting_system.php,{投票:GB},功能(vote_sum){
    $(#内容)HTML(vote_sum)。
  });
}
 

在PHP中,提取出票,并把它作为需要(添加验证/卫生):

  $票= $ _ POST ['投票']; //无论是好或坏
//做你需要它
 

So here I am posting my first PHP function that I am proud of but I just recently learned about AJAX and wanted to test it out. Unfortunately I can't get it to work.

My experience: PHP (3 weeks). CSS3, HTML, Basic Javascript.

My Problem: Getting AJAX to work. I want ajax to get my data from the php file which gets the votes from my test server (Xampp) database. So each time the user clicks on good or bad AJAX should display the new results without refreshing the page. The issue is however that: A) My if statements work by checking isset($_POST) which wont work anymore if I call by AJAX. B) Preventing refresh. C) Making AJAX update after every click. I know im nearly there, im just missing something and I dont know exactly what it is to be honest.

What I tried: Checked my database connection. Checked if my php code worked without ajax and it does perfectly fine (I am just displaying half of the functionality here, a lite version, for the sake of simplicity). Tried to change submit to button. Cache clearing. Jquery is in the head of my document and the path is correct. Watched tutorials and read the documentation but I am just not heading anywhere, probably due to lack of experience.

Edit: Sessions and everything php works fine. I my session start and database connection are included on the very top.

Summary: How do I fix this ajax so that it always updates my numbers?

Let me know if you want me to explain parts of my php code. Im willing to comment the parts if neccesary.

JQUERY / AJAX CODE

function vote() {
        var request = $.ajax({
            url: "php/core/voting_system.php",
            type: "POST",           
            dataType: 'html'
        });

        request.done(function(vote_sum) {
            $("#votes").html(vote_sum);         
        });
}

HTML CODE:

<div id='votes'></div>

<form id="good" action="" method="post">
    <input type="submit" name="good" onclick="vote()" value="+">
</form>

<form id="bad" action="" method="post">
    <input type="submit" name="bad" onclick="vote()" value="-">
</form>

解决方案

In HTML you don't need <form>, you are doing it with AJAX, right?

<div id='votes'></div>
<button onclick="vote('good');">+</button>
<button onclick="vote('bad');">-</button>

In JavaScript, it is easier to use post rather than ajax function

function vote(gb) {
  $.post("php/core/voting_system.php", { vote: gb }, function(vote_sum) {
    $("#votes").html(vote_sum);         
  });
}

In PHP, extract the vote and use it as needed (add validation/sanitation):

$vote = $_POST['vote']; // either 'good', or 'bad'
// do what you need with it