Zange-chan Zange-chan - 1 year ago 34
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?

Answer Source

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