【问题标题】:If (isset $_Post[]) doesn't work when i send a var from ajax to php当我将 var 从 ajax 发送到 php 时,如果 (isset $_Post[]) 不起作用
【发布时间】:2021-12-19 13:46:43
【问题描述】:

我在尝试将 AJAX 帖子捕获到我的 PHP 代码时遇到问题

让我解释一下:

所以我想要做的是,当select2 上的值发生变化时,我将捕获结果以将其发送到我的 PHP,然后我想在我的数据库请求中使用它。

为了查看它是否有效,我编写了一个脚本来更改我的输入文本的值,当我从帖子中得到结果时,这是我的代码: js脚本:

$(function(){
    $('#Refva').select2();
})

function changeValue(){
    var txt =  (document.getElementById('Refva'));

    $.ajax({
        url : "Vente.php",
        type : "POST",
        data : ({'txt':txt}),
        success: function(data){
            console.log(txt);
        },
        error : function(data){
            console.log("Une erreur s'est produite");
        }
    });
}

PHP:

if (isset($_POST['txt']))
{
    $P = 'It work!';
}

填充输入的php脚本:

<script>
$(document).ready(function(){
    var P = <?php echo json_encode($P); ?>;

    $('#Article_vendua').on('click',function(){
        $('#Article_vendua').val(P);
    });
});
</script>

当我使用时:

if (!isset($_POST['txt']))
{
  $P = 'It work!';
}

我有结果了!这是合乎逻辑的,但是当我想设置 isset 时它不起作用

注意在我的控制台中我有 Post 但在我的 PHP 中没有

【问题讨论】:

标签: php jquery ajax bootstrap-4


【解决方案1】:

var txt = (document.getElementById('Refva'));

在上面: txt 保存元素而不是值 你可以使用 $('#Refva').val();

$.ajax({
    url : "Vente.php",
    type : "POST",
    data : ({'txt':txt}),  // brackets ( ) are not required here
    success: function(data){
        console.log(txt); // you should check the data - i dont know why to check txt
    },
    error : function(data){
        console.log("Une erreur s'est produite");
    }
});

【讨论】:

  • 嗯,值不是问题,问题是当我尝试用 if (isset($_Post[txt])) 捕获值时
【解决方案2】:

尝试只发送选择元素的值和整个对象:

document.getElementById ('Refva'). value ()

【讨论】:

  • 仍然无法正常工作:/ 我不知道问题在哪里
猜你喜欢
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多