【问题标题】:How to make a subscribe button using php, ajax and jQuery?如何使用 php、ajax 和 jQuery 制作订阅按钮?
【发布时间】:2018-05-16 20:11:55
【问题描述】:

实际上,我对 Ajax 和 jQuery 完全陌生,但我正在尝试使用两者为我的网站制作一个订阅按钮。

现在我已经阅读了 Ajax 和 jQuery 的大部分文档,并想出了一些代码来做到这一点。

问题是它不工作,我一辈子都无法理解为什么。

我点击订阅按钮,没有任何反应:
subscribe.php 脚本没有运行,数据库没有更新,也没有显示警报 - 就像我说的......什么都没有发生

希望有人能提供帮助。

到目前为止的代码
jQuery.js(加载在 index.php 中,在 body-tag 的开头包含所有其他页面)

<body>
// Load official jQuery-library
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
// Load my own jQuery-script file
<script src="jQuery/jQuery.js"></script>
.
.
.
// Load all pages (videos.php, channel.php,...) as needed
</body>

// Code in my own jQuery.js
$(document).ready(function(){
    "use strict";
    $("#subscribe").click(function(subscribeData){
        $.ajax({
            type: "POST",
            url:'../scripts/subscribe.php',
            data:{randomStringUser:subscribeData},
            dataType:'json',
            success:function(data){
                alert("You subscribed");// Do what you do after subscribe.php has done it's thing
                // just to test I just put an alert here
            },
            error:function(){
                alert("Didn't work");// Do what you do if mission failed
                // just to test I just put an alert here
            }
        });
    });
});

订阅按钮所在页面顶部的PHP(即channel.php)和订阅按钮代码

<?php

    .
    .
    .
    $randomStringUser = $_GET['channel'];
    $subscribeData = json_encode($randomStringUser);

?>
// Page html
.
.
.

// Subscribe-button
<button type="button" class="btn btn-success" id="subscribe">
    Subscribe
</button>

来自 ajax 调用的 subscribe.php

<?php

    session_start();

    $randomStringUser = $_POST['randomStringUser'];

    // $_SESSION['id'] is set with users-id when user log in
    // So if user logged in use that id as $subscriberID
    if ( isset ( $_SESSION['id'] ) ) {
        $subscriberID = $_SESSION['id'];
    }

    "SELECT id FROM userinfo WHERE randomString = ? LIMIT 1" // using $randomStringUser as ?

    // Use the id found in SELECT as $subscriptionID

    "INSERT INTO usersubscription ( subscriberID , subscriptionID ) VALUES ( ? , ? )" // Using $subscriberID and $subscriptionID from above as ?

【问题讨论】:

  • 您是否可以检查 Google Chrome 开发者工具中的“网络”选项卡以查看是否正在向 ../scripts/subscribe.php 发出请求? (F12 -> 网络)
  • 刚刚做了,没有任何请求...很奇怪嗯...
  • 好的,我必须检查网络中的错误...所以没有发送请求,但它会抛出“未捕获的 TypeError:$.ajax 不是函数”
  • 我发现了您的问题:stackoverflow.com/questions/44212202 --- 您使用的是不包含 $.ajax 的精简版 jQuery
  • https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js替换你的jQuery链接

标签: php jquery ajax


【解决方案1】:

$.ajax 未找到,因为您引用的是 slim 版本的 jQuery,而不是整个库。

我建议您使用 jQuery 的完整缩小版,以便充分发挥它的潜力。

尝试将您的&lt;script&gt; 标记替换为以下内容:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="crossorigin="anonymous"></script>

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 2014-02-26
    • 1970-01-01
    • 2018-09-28
    • 2012-06-30
    • 2017-07-29
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多