Home > Mobile >  If (isset $_Post[]) doesn't work when i send a var from ajax to php
If (isset $_Post[]) doesn't work when i send a var from ajax to php

Time:11-06

i have a problem when am trying to catch the ajax post into my php code let me explain:

So what i want to do is that when the value change on select2 i'll catch the result to send it at my php and then i want to use it in my db request .

to see if it work i'v wrote a script to change the value of my input text when i got a result from the post here is my codes: Js script:

$(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 to fill the input :

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

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

When i use :

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

enter image description here

I have result! It's logical, but when I want to set isset it doesnt work

N.B. in my console I have the Post but not in my php

CodePudding user response:

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

on the above : the txt holds the element not value you can use $('#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");
    }
});

CodePudding user response:

Try sending only the value of the select element and the whole object:

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