获取从AJAX传递的值时,php中未定义的索引

时间:2022-10-17 09:10:24

I'm trying to get the value that I passed in AJAX to PHP. It shows an alert that says, "Success!" however it when I try to display the value in PHP, it says undefined index. Also I am passing it in the same page.

我正在尝试将我在AJAX中传递的值传递给PHP。它显示一条警告,上面写着“成功!”然而,当我尝试在PHP中显示值时,它表示未定义的索引。我也在同一页面传递它。

Whenever I click a button, it opens a modal and I also passing values from that button to the modal. This is evident in my JS code.

每当我点击一个按钮时,它会打开一个模态,我也会将该按钮的值传递给模态。这在我的JS代码中很明显。

<script>
        $(document).ready(function(){
          $('#editModal').on('show.bs.modal', function (e) {

            var id = $(e.relatedTarget).data('id'); //im trying to pass this value to php, e.g. 5
            var time = $(e.relatedTarget).data('time');
            var name = $(e.relatedTarget).data('name');
            var stat = $(e.relatedTarget).data('stat');
            var desc = $(e.relatedTarget).data('desc');
            alert(id);
            $("#task_id_edit").val(id);
            $("#new-taskID").val(id);

            $("#allotted_time_edit").val(time);
            $("#task_name_edit").val(name);
            $("#task_status_edit").val(stat);
            $("#task_desc_edit").val(desc);
            $("#task_id_ref2").val(id);


           //AJAX CODE HERE
           $(function() {
                $.ajax({
                    type: "POST",
                    url: 'tasks.php?id='<?php $id = isset($_GET['id']) ? $_GET['id'] : ""; echo $id; ?>,
                    data: { "userID" : id },
                    success: function(data)
                    {
                        alert("success!"); //this display
                    }
                });
           });



           }); // line 1131
        });
</script>

PHP CODE:

<?php

   $uid = $_POST['userID'];
   echo $uid." is the value"; 

?>

It keeps getting an error that says, undefined index: userID. I am confused. Please help me how to fix this. Your help will be much appreciated! Thank you!

它不断收到错误,说明未定义的索引:userID。我很迷惑。请帮我解决这个问题。非常感谢您的帮助!谢谢!

2 个解决方案

#1


2  

Echo the number in the javascript string.

回显javascript字符串中的数字。

Currently you will get:

目前您将获得:

url:'tasks.php?id='1,

The 1 should be concatenated or inside the quote. Try:

1应该连接或在引号内。尝试:

url: 'tasks.php?id=<?php $id = isset($_GET['id']) ? $_GET['id'] : ""; echo $id; ?>',

or take that parameter out since it doesn't appear to be being used.

或者取出该参数,因为它似乎没有被使用。

#2


-2  

Just put the $_POST['userID'] in a if statement

只需将$ _POST ['userID']放在if语句中即可

Undefined index: UserID is not a error it is a warning

未定义的索引:UserID不是错误,它是一个警告

<?php
   if(isset($_POST['userID'])
   {
   $uid = $_POST['userID'];
   echo $uid." is the value"; 
   }
   else
   {
     echo "Sorry Value cant be printed";
   }

?>

#1


2  

Echo the number in the javascript string.

回显javascript字符串中的数字。

Currently you will get:

目前您将获得:

url:'tasks.php?id='1,

The 1 should be concatenated or inside the quote. Try:

1应该连接或在引号内。尝试:

url: 'tasks.php?id=<?php $id = isset($_GET['id']) ? $_GET['id'] : ""; echo $id; ?>',

or take that parameter out since it doesn't appear to be being used.

或者取出该参数,因为它似乎没有被使用。

#2


-2  

Just put the $_POST['userID'] in a if statement

只需将$ _POST ['userID']放在if语句中即可

Undefined index: UserID is not a error it is a warning

未定义的索引:UserID不是错误,它是一个警告

<?php
   if(isset($_POST['userID'])
   {
   $uid = $_POST['userID'];
   echo $uid." is the value"; 
   }
   else
   {
     echo "Sorry Value cant be printed";
   }

?>