I’m passing an object through ajax to a PHP file for processing like this:
How to keep variable types inside object posted through ajax?
var Obj = {id:1, name:"John", value:12.1}; $.ajax({ url : "myfile.php", type : 'POST', data : Obj, success : function(data) { console.log(data); });
My issue is when I receive the parameters on my $_POST variable everything is a string like id => “1” or value => “12.1”. I would like these to be kept like an Int and a Float for example without additional conversions on the PHP side.
Is there an easy way to maintain the variable types?
Gurpreet Singh Padam
You need to convert the values on PHP because PHP receives values as a string :
$data = $_POST[‘data’] ;
$int = (int)$data[‘id’]; // or intval($data[‘id’])
$float = (float)$data[‘value’]; // or floatval($data[‘value’])