weixin_33717117 2014-03-13 13:22 采纳率: 0%
浏览 31

$ .ajax POST未定义索引

Apologies in advance for any mistakes. This is my first post on StackOverflow.

So basically I have a php function. Inside it is a jQuery click function with a variable, into which I'm saving the clicked element's text. I'm trying to send that variable's value to my Uredi.php file, via $.ajax function, to save it into a php variable. However, I'm getting an "Undefined index" error. Help would be greatly appreciated. Thank you.

Here's the code:

PHP function:

public function uredi()
{
?>
<script>
$('.edit').click(function(){

$('.naslov_edit').val($(this).text());
$('#on_top').hide();
var refId = $(this).text();
    $.ajax({
        url: 'uredi.php', 
        type: "POST",
        data: {id: refId},
        success: function(data){
            alert(data);
        }
    }); 

});
</script>
<?php
}

Uredi.php:

<?php
include 'Funkcije.php';

$uredi = new urejanje();
$uredi->uredi();

$id = $_POST['id'];
echo $id;
?>

Funkcije.php is the file with the uredi() function.

EDIT: Thank you everyone for helping. I figured out the problem. Turns out I just had to clean up my code a bit.

  • 写回答

3条回答 默认 最新

  • weixin_33699914 2014-03-13 13:26
    关注

    Why are you assigning $(this).text() to var id and then not using it in the $.ajax()? Remember scope and what doe $(this) actually reference within the $ajax()? I would also shy away from naming variables "id" Try:

    var refId = $(this).text();
    $.ajax({
        url: 'uredi.php', 
        type: "POST",
        //dataType: 'json',
        data: {id: refId},
        success: function(data){
            alert(data);
        }
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备