1

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

enter image description here

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

1
  • pass array instead of object... and serialize() it Commented Oct 18, 2013 at 6:19

3 Answers 3

1
document.getElementById('form_elements_array').value=JSON.stringify(form_elements);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.