使用ajax将数据传递给php

时间:2022-12-01 21:33:38

this is not my first time to use ajax on my website but right now im really confused whats wrong with it. i even removed the important codes. what im doing right now. is to check if ajax is passing the data to the php or not.

这不是我第一次在我的网站上使用ajax,但现在我真的很困惑,它有什么不对。我甚至删除了重要的代码。我现在正在做什么。是检查ajax是否将数据传递给php。

 <script type='text/javascript'>
        $(document).ready(function () {
            $('.btndeclineme').click(function () {
                    var docreason = document.getElementById("docreason");
                    if (docreason !== null && docreason.value === "")
                    {
                      $('#reasonalert').show();
                      return;
                    }else{
                        var signatoryidglobal = JSON.parse('<?php echo json_encode($_SESSION['signatoryid']); ?>');
                        alert(signatoryidglobal);

                        $('#reasonalert').hide();

                                $.ajax({
                                        type: "POST",
                                        url: "sample.php",
                                        data: ({dtsignatory: signatoryidglobal})
                                    })
                                    .done(function (msg) {
                                    alert( "Data:" + msg);

                                    })
                            .fail(function() {
                                alert( "Posting failed." );
                            }); 
                    };
            });
        });
  </script>

and this is the sample.php

这是sample.php

<?php 

    $temp1 = $_POST['dtsignatory'];

echo $temp1;

?>

my problem is its not showing errors. isn't it supposed to promp the data i passed to the php because of alert( "Data:" + msg); but it keeps showing nothing.

我的问题是它没有显示错误。是不是应该提示我传递给php的数据因为警报(“数据:”+ msg);但它一直没有显示出来。

i even add a alert prompt to check if the variable has a value. and it has a value. sorry for my bad english. i hope my question is clear

我甚至添加一个警报提示来检查变量是否有值。它有一个价值。对不起,我的英语不好。我希望我的问题很清楚

1 个解决方案

#1


0  

 $.ajax({
     type: "POST",
     url: "sample.php",
     data: {
         dtsignatory: signatoryidglobal
     },
     headers: {
         "Accept": "application/json; charset=utf-8"
     },
     contentType: "application/json; charset=utf-8",
 })

#1


0  

 $.ajax({
     type: "POST",
     url: "sample.php",
     data: {
         dtsignatory: signatoryidglobal
     },
     headers: {
         "Accept": "application/json; charset=utf-8"
     },
     contentType: "application/json; charset=utf-8",
 })