skip to Main Content

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 works I’ve wrote a script to change the value of my input text when I’ve 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 doesn’t work

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

2

Answers


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

    document.getElementById ('Refva'). value ()
    
    Login or Signup to reply.
  2. 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");
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search