jQ / PHP
Is any way pass from jQ/Ajax Post function an array or object to PHP for processing…???
eg in a car dealer site add to favorites Car added to localStorage array, and then shown in favorites page by passing this array to PHP…
well…?
jQ / PHP
Is any way pass from jQ/Ajax Post function an array or object to PHP for processing…???
eg in a car dealer site add to favorites Car added to localStorage array, and then shown in favorites page by passing this array to PHP…
well…?
I have to stringify and use php json decode()…?
In other words how from $INCLUDED_MAKES PHP can get an array of [“184745”, “184401”] in PHP so pass to MySQL Query…? I want process these IDs below die();
$fav18obj = json_decode($fav18); // $fav18 from ajax
$INCLUDED_MAKES = json_encode($fav18obj);
echo $INCLUDED_MAKES; // [{"id":"184745"},{"id":"184401"}] // in js given this
die();
{“id”:“184745”} // this got from (jQ/ajax/success) below …how got only number for use in database??
$INCLUDED_MAKES = json_encode($fav18obj[0]);
echo $INCLUDED_MAKES;
die();
You can edit your posts y’know. ;-)
Anyway, on the basis of your snippets I’m not really sure what the problem is… here’s how you’d perform a POST request with jQuery:
// Note the 'data' property which can be accessed
// by PHP just like a form input field (say)
var data = {data: [
{id: 184745},
{id: 184401}
]};
$.ajax({
url: 'foo.php',
method: 'POST',
data: data
}).done(function(response) {
// Logs
// 184745
// 184401
console.log(response);
});
and process it with PHP like e.g.
<?php
// The $_POST index is the property of the object you sent
if (isset($_POST['data'])) {
$data = $_POST['data'];
foreach ($data as $value) {
// Same here...
echo $value['id'] . PHP_EOL;
}
}
In php needed json decode function at startup?
As you say how in php create array to pass to mysql query?
How in mysql get only cars with cardids given…? I need parentheses in query or array???
Select * cars where carid in (carid1, carid2, carid3);
No, you’d only need to do that if you’d receive a JSON string, but you’re submitting an object which will be treated by PHP as a multidimensional array. The code above should work as is.
Yes, with parentheses like that. You might concatenate the SQL string in a foreach
loop over the ID array, for instance.
tried
SELECT c.* FROM cars c WHERE c.id=(‘184745’,‘184401’)
tried and this
SELECT c.* FROM cars c WHERE c.id=(184745,184401)
but getting one array object with one object === all null… i must get 2 objects in array… is QUERY CORRECT???
You had it correct above using the IN
operator.
SELECT c.* FROM cars c WHERE c.id IN (184745,184401)
Worked this
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.