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!';
}
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
Try sending only the value of the select element and the whole object:
var txt = (document.getElementById(‘Refva’));