Zange-chan Zange-chan - 5 months ago 5
Ajax Question

Passing data from AJAX to PHP with POST method without using <form> element

I'm having a "Undefined index" while passing values from ajax to php without using a form.

Here are my codes:



function sendDatasToPhp() {
var id = <? php echo json_encode($id); ?> ;
var otherId = <? php echo json_encode($_SESSION['id']); ?> ;

$.ajax({
method: "POST",
url: "send_data_to_php.php",
data: "id=" + id + "&otherId=" + otherId,
cache: false,
success: function() {
alert("Datas are being transfered");
}
})
}

<div>
<label class="send_datas" onclick="sendDatasToPhp();">Send datas to php</label>
</div>





Assuming that
$id
is already initialized and
$_SESSION['id']
has already a value. The problem is that the two indices are undefined once transferred to the php file. How can I fix these?

Answer

I used the GET method instead of POST and I made it work.