$(".save_form_email").on('click', function() {
var pocet = $('#save__cena').val();
console.table(pocet);
$.ajax({
url: '/modules/mod_custom_extended/tmpl/default.php',
type: 'POST',
data: {
pocet: pocet
},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(response) {
console.table(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('AJAX request failed: ' + textStatus + ', ' + errorThrown);
}
});
});
This is the PHP
<?php
if (isset($_POST['pocet'])) {
$temp_pocet = $_POST['pocet'];
}
?>
This is my attempt to send value of html input field with id "save__cena" to the php so i can later use this price to send it to stripe.
Currently i have nothing in php variable temp_pocet
2
Answers
When you send JSON data using an ajax request to php, the data is not accessible in the
$_POST
superglobal. You need to read fromphp://input
instead like so:$_POST only works with data that has the
Content-Type
set toapplication/x-www-form-urlencoded
ormultipart/form-data
, notapplication/json
.Additional info: this stack overflow answer
EDIT
If you also plan on returning a json response from the php script, then you should move the code that processes the ajax request to a separate php file that is not mixed with any html code. This is to prevent html characters from being sent along with the json response and causing it to become corrupted.
Oh and you should also change your content-type header to
application/json
Try this