PHP,AJAX,HTML,MySQL,点击按钮从数据库获取信息

时间:2022-11-27 18:27:25

I am trying to create a website that will present a person and his score but behind a password protected wall I can increase or decrease the score. I can get the button to respond to ajax but I cant get it to pass off to the php and have the php respond with the sql entry.

我正在尝试创建一个网站,提供一个人和他的分数,但在密码保护墙后面我可以增加或减少分数。我可以得到按钮来响应ajax但我不能让它传递到PHP并让php响应sql条目。

Ajax

<script>   
 $(document).ready(function () {
$('#next').click(function (e) {
    e.preventDefault();
    $.ajax({
        type: "GET",
        url: "a1.php",
        dataType: "html",
        success: function (msg) {
            if (msg.success) {
                $("#responsecontainer").html(msg);
            } else {
                alert("error");
            }
        }
    });
});
});
</script>

In all honesty I have spent so much time on this flipping ajax problem I dont even think I can understand what I was trying to do at this point...Sorry lads.

老实说,我花了很多时间来解决这个问题,我甚至认为我无法理解我此刻想要做什么......对不起小伙子们。

PHP

<?php
$servername = "localhost";
$username = "test_db_usr";
$password = "123";
$dbname = "test_db";

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


$sql = "SELECT id, name, points FROM points";
//how do I send this back to my html file?


$conn->close();
?>

So my goal is when I click the "next button" the system will move onto the next ID number in the MySQL database. I have the database set up and I can pull info through PHP but I cant get a button click to do anything!

所以我的目标是当我点击“下一步按钮”时,系统将移动到MySQL数据库中的下一个ID号。我有数据库设置,我可以通过PHP提取信息,但我无法点击按钮做任何事情!

I also plan to have two buttons one that adds a point and one that removes one and they would pass this info off to the database...but one step at a time for me.

我还计划有两个按钮,一个添加一个点,一个删除一个,他们会将这些信息传递给数据库...但是我一步一步。

Sorry if this isnt enough info Ill do my best to fill in any gaps I left out.

对不起,如果这还不够,我会尽力填补我遗漏的任何空白。

Regards.

3 个解决方案

#1


0  

As a first step, I suggest you see the answer to the question that I am linking to below.

作为第一步,我建议您看到我正在链接到下面的问题的答案。

Fetch rows using mysqli

使用mysqli获取行

More importantly, you need to read the PHP documentation: http://php.net/manual/en/mysqli-stmt.get-result.php

更重要的是,您需要阅读PHP文档:http://php.net/manual/en/mysqli-stmt.get-result.php

#2


0  

U have to mention hidden field score "id" and action field which will be "add" or "remove" which be set upon button click and score field,in click event set action field like $("#action").val("remove"); and add data:$("#form").serialize(); option in your Ajax call, receive posted fields in the server side and execute SQL I.e. Update ..., echo them and update response.

你必须提到隐藏的字段分数“id”和动作字段,它将是按钮点击和分数字段设置的“添加”或“删除”,在点击事件集操作字段中,如$(“#action”)。val( “去掉”);并添加数据:$(“#form”)。serialize(); Ajax调用中的选项,接收服务器端的已发布字段并执行SQL I.e.更新...,回显它们并更新响应。

#3


0  

to answer your

回答你的问题

$sql = "SELECT id, name, points FROM points";
//how do I send this back to my html file?

i think you need to add another AJAX GET call thats something like this

我认为你需要添加另一个类似这样的AJAX GET调用

 $.ajax({
        type: "GET",
        url: "a1.php",
        dataType: "html",
        success: function (data) {
           console.log(data);
        }
    });

and in your php code you need to return a JSON response thats something like

并在您的PHP代码中,您需要返回类似的JSON响应

return json_encode(array('success', data));

and use this response to append updated data on your html to get updated values

并使用此响应在html上附加更新的数据以获取更新的值

#1


0  

As a first step, I suggest you see the answer to the question that I am linking to below.

作为第一步,我建议您看到我正在链接到下面的问题的答案。

Fetch rows using mysqli

使用mysqli获取行

More importantly, you need to read the PHP documentation: http://php.net/manual/en/mysqli-stmt.get-result.php

更重要的是,您需要阅读PHP文档:http://php.net/manual/en/mysqli-stmt.get-result.php

#2


0  

U have to mention hidden field score "id" and action field which will be "add" or "remove" which be set upon button click and score field,in click event set action field like $("#action").val("remove"); and add data:$("#form").serialize(); option in your Ajax call, receive posted fields in the server side and execute SQL I.e. Update ..., echo them and update response.

你必须提到隐藏的字段分数“id”和动作字段,它将是按钮点击和分数字段设置的“添加”或“删除”,在点击事件集操作字段中,如$(“#action”)。val( “去掉”);并添加数据:$(“#form”)。serialize(); Ajax调用中的选项,接收服务器端的已发布字段并执行SQL I.e.更新...,回显它们并更新响应。

#3


0  

to answer your

回答你的问题

$sql = "SELECT id, name, points FROM points";
//how do I send this back to my html file?

i think you need to add another AJAX GET call thats something like this

我认为你需要添加另一个类似这样的AJAX GET调用

 $.ajax({
        type: "GET",
        url: "a1.php",
        dataType: "html",
        success: function (data) {
           console.log(data);
        }
    });

and in your php code you need to return a JSON response thats something like

并在您的PHP代码中,您需要返回类似的JSON响应

return json_encode(array('success', data));

and use this response to append updated data on your html to get updated values

并使用此响应在html上附加更新的数据以获取更新的值