为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过 Ajax 发送 $_GET

如何通过 Ajax 发送 $_GET

PHP
鸿蒙传说 2022-01-23 10:54:28
我正在尝试发送一个 $_GET['CategoryID'] trought ajax 来调用目标文件 getdata.php,但我无法使其工作,我在这里找不到完美的信息。我知道我真的很菜鸟,但我正在努力学习。我一直在尝试很多不同的代码,但它仍然无法正常工作。<button type="button" name="btn_more" data-vid="<?php echo $stockID; ?>" id="btn_more" class="btn btn-success form-control">Ver Mais</button><input class="form-control" id="PresentCategoryID" name="PresentCategoryID" data-cat="<?php echo $_GET['categoryID']; ?>" value="<?php echo $_GET['categoryID']; <script>   $(document).ready(function(){        $(document).on('click', '#btn_more', function(){             var last_video_id = $(this).data("vid");               var PresentCategoryID= ('PresentCategoryID');           $('#btn_more').html("<div class=loader></div>");             $.ajax({                  url:"getdata.php",                  method:"POST",                    data:{                         last_video_id:last_video_id,                         PresentCategoryID:PresentCategoryID},                  dataType:"text",                  success:function(data)                  {                       if(data != '')                       {                            $('#remove_row').remove();                            $('#result').append(data);                       }                       else                       {           $('#btn_more').html("No Data");             }           }        });     }); });  </script>我的目标是在getdata.php中调用categoryID,像这样,<?php  $output = '';  $stockID = '';$PresentCategoryID = '';sleep(1);  include 'includes/dbh.inc.php';include 'includes/rating.inc.php';$sql = "SELECT stock.stockID, stock.name, stock.marca, stock.origem, stock.categoryID, stock.thumbnail, category.name AS catname FROM stock JOIN category ON stock.categoryID=category.categoryID WHERE stock.categoryID='$PresentCategoryID' AND stockID > ".$_POST['last_video_id']." LIMIT 4";?>
查看完整描述

3 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

var PresentCategoryID= ('PresentCategoryID')

应该

var PresentCategoryID= $('#PresentCategoryID').val();

您需要使用$选择元素,添加#前缀以将其用作 ID,并.val()获取输入的值。


查看完整回答
反对 回复 2022-01-23
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

检查这个答案,这将有助于你的问题


$(document).ready(function() {

    $(document).on('click', '#btn_more', function() {

        var last_video_id = $(this).data("vid");

        var PresentCategoryID = ('#PresentCategoryID').val();

        $('#btn_more').html("<div class=loader></div>");

        var data = {

            last_video_id,

            PresentCategoryID

        };


        $.get('getdata.php', JSON.stringify(data)).done(response => {

            console.log(response);

            if (response != '') {

                $('#remove_row').remove();

                $('#result').append(response);

            } else {

                $('#btn_more').html("No Data");

            }


        }).fail(() => {

            console.log("Something went wrong");

        });

    });


});

PHP 脚本


<? php

include 'includes/dbh.inc.php';

include 'includes/rating.inc.php';

if ($_SERVER['REQUEST_METHOD'] == 'GET') {

//if you using get request

//recommended way to get the data use the mysqlconn->real_escape_string($_GET['last_video_id']);

    $last_video_id = $_GET['last_video_id'];

    $PresentCategoryID = $_GET['PresentCategoryID'];

    sleep(1);

    $sql = "SELECT stock.stockID, stock.name, stock.marca, stock.origem, stock.categoryID, stock.thumbnail, category.name AS catname FROM stock JOIN category ON stock.categoryID=category.categoryID WHERE stock.categoryID='$PresentCategoryID' AND stockID='$$last_video_id'";

    " LIMIT 4";

} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    //if you using post request then in jquery remove $.get to just $.post 

    $data = json_decode(file_get_contents('php://input'),true);

    $last_video_id = $data['last_video_id'];

    $PresentCategoryID = $data['PresentCategoryID'];

}


查看完整回答
反对 回复 2022-01-23
?
精慕HU

TA贡献1845条经验 获得超8个赞

您想通过“GET”发送,但您使用“POST”方法。

最好的问候 MrKampf


查看完整回答
反对 回复 2022-01-23
  • 3 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号