简体   繁体   中英

how to send JSON array to server with no AJAX

i am trying to send an array of JSON objects to

PHP server using an hidden field

but all i am getting in the server is just a string

在此处输入图片说明

this is my javascript

function CreateArrayOfJSON()
{
      var all_childrens = $('#form_div').find('*');//get all root element childrens
      var form_elements = {
            elements: []
        };
            for(var i=0;i<all_childrens.length;i++)
            {
               var id='#'+$(all_childrens[i]).attr('id');         //get id
               var style_attr=$(all_childrens[i]).attr('style'); //get style inline
               var classes=$(all_childrens[i]).attr("class");

               form_elements.elements.push
               ({ 
                    "id"         : id,
                    "style_attr" :style_attr,
                    "classes"    :classes
               });
            }
            document.getElementById('form_elements_array').value=form_elements;//fill hidden field
}

this is my PHP:

this will return Object object (as in the picture above)

$form_elements=$_POST['form_elements_array']; 

this will return null

$form_elements=json_decode($_POST['form_elements_array']);

any ideas ?

thank you

document.getElementById('form_elements_array').value=JSON.stringify(form_elements);

can you paste the output string ?

or alternatively

You can use Curl for the same

    $url = "URL Where you want to send data";
    $ch = curl_init();
    $attachment = array(
    'name' => 'Your first field',
    'link' => 'Second Field',
    'description' => 'description here',
);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    $result = curl_exec($ch);
    curl_close($ch);

now php code on the receiver page

can you paste the output string ?

or alternatively

You can use Curl for the same

    $url = "URL Where you want to send data";
    $ch = curl_init();
    $attachment = array(
    'name' => 'Your first field',
    'link' => 'Second Field',
    'description' => 'description here',
);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    $result = curl_exec($ch);
    curl_close($ch);

now php code on the receiver page

        echo $_POST['name']; // get your data with POST method

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