简体   繁体   中英

Passing a PHP $_POST array to javascript/jQuery to send back to PHP via ajax

I need to pass the $_POST array currently available on the page into my javascript. jQuery needs to $.post this array back to php and get it processed.

I've tried the below and my jquery and post is fine but my echo $items; is drawing a blank on receipt on the php side.

any ideas please?

php :

$items = json_encode($_POST);

jquery :

var data = {
'action': 'someaction',
'items': <?php echo $items;?>
 } ;

$.post(ajaxurl, data, function(response) {
                    console.log( response ) ;
});

I'm expecting the $_POST array to be passed via $.post in jquery back to my php function.

yes i've found an answer.

The line i'm missing is:

<?php echo "var items = " . $items . ";" ;?>

so this works for me:

**PHP**
$items = json_encode( $_POST ) ;

**JS**
var items;
<?php echo "var items = " . $items . ";" ;?>

Yes a form was already submitted....so ihave to capture what was submitted. Timeouts occur on server side so i compare what was posted with what has been brought back... i use ajax to process the two. Messy. But if i find a better solution i'll use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM