猿问

如何通过 Ajax 发送 $_GET

我正在尝试发送一个 $_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";


?>


鸿蒙传说
浏览 147回答 3
3回答

冉冉说

var&nbsp;PresentCategoryID=&nbsp;('PresentCategoryID')应该var&nbsp;PresentCategoryID=&nbsp;$('#PresentCategoryID').val();您需要使用$选择元素,添加#前缀以将其用作 ID,并.val()获取输入的值。

牛魔王的故事

检查这个答案,这将有助于你的问题$(document).ready(function() {&nbsp; &nbsp; $(document).on('click', '#btn_more', function() {&nbsp; &nbsp; &nbsp; &nbsp; var last_video_id = $(this).data("vid");&nbsp; &nbsp; &nbsp; &nbsp; var PresentCategoryID = ('#PresentCategoryID').val();&nbsp; &nbsp; &nbsp; &nbsp; $('#btn_more').html("<div class=loader></div>");&nbsp; &nbsp; &nbsp; &nbsp; var data = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_video_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PresentCategoryID&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; $.get('getdata.php', JSON.stringify(data)).done(response => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response != '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#remove_row').remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#result').append(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#btn_more').html("No Data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }).fail(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Something went wrong");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });});PHP 脚本<? phpinclude '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']);&nbsp; &nbsp; $last_video_id = $_GET['last_video_id'];&nbsp; &nbsp; $PresentCategoryID = $_GET['PresentCategoryID'];&nbsp; &nbsp; sleep(1);&nbsp; &nbsp; $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'";&nbsp; &nbsp; " LIMIT 4";} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {&nbsp; &nbsp; //if you using post request then in jquery remove $.get to just $.post&nbsp;&nbsp; &nbsp; $data = json_decode(file_get_contents('php://input'),true);&nbsp; &nbsp; $last_video_id = $data['last_video_id'];&nbsp; &nbsp; $PresentCategoryID = $data['PresentCategoryID'];}

精慕HU

您想通过“GET”发送,但您使用“POST”方法。最好的问候 MrKampf
随时随地看视频慕课网APP
我要回答