Zange-chan Zange-chan - 4 months ago 4x
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']); ?> ;

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

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

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


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