2

I need to pass a javascript object to PHP then decode it as an associative array. I have read many similar questions on this site and googled for a few hrs now. My problem is that I cannot find a SIMPLE example that covers both sides (javascript and PHP). Here is a simplified version of what im trying to do. Posting data as a key/value pair is the only way I can get anything to pass. There is a DIV in the HTML that is used to display the answer. All I want to do is confirm that the data gets past in this code.

All I get is "POST array is loaded with", which to me suggests that the POST array contains data, but im either decoding it wrong or accessing the associative array incorrectly. I have also read the AJAX and POST pages on the JQUERY site but the examples don't get me where I need to be. If I assign strob a simple string value it gets passed perfectly.

        $(document).ready(function(){
            var obj={};
            obj.value1="foo";
            obj.value2="bar";
            var strob=JSON.stringify(obj);
            $.post("byterecord.php",{sendToValue: strob},
            function(data){
                $('#display').html(data.returnFromValue);
            }, 
            "json");
        });

byterecord.php

<?php
if (isset($_POST['sendToValue'])){

$aarray=json_decode($_POST['sendToValue'],true);
$strval=$aarray[value1];

}else{
$strval = "nothing.";
}

echo json_encode(array("returnFromValue"=>"POST array is loaded with " .$strval));  

?>
Bio_Xcoder
  • 41
  • 4

2 Answers2

0

Just pass the object directly:

$.post("byterecord.php", obj, function() {});

Then in php, you could get it by:

$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Nope, there must be something fundamentally wrong with my brain or site. Now I get nothing passed. Here are the URLs: http://learningbyte.comze.com/indexexample.html and byterecord.php – Bio_Xcoder Oct 12 '12 at 01:52
  • You have a syntax error in your code. There's an extra ',' at the end, line 29, after }). put a ; instead – Maktouch Oct 12 '12 at 02:24
  • Good catch but no function still. I'm REALLY thankful for the help its driving me crazy. I don't mind starting from scratch with fresh code that passes any object from JQuery to PHP then sends back some notification that the data made it. – Bio_Xcoder Oct 12 '12 at 02:33
0

Your code works fine:

However, your request, when examined from the console, is generating PHP notice :

  Notice: Use of undefined constant value1 - assumed 'value1' 

Either change the param from

 $aarray[value1];

to

 $aarray['value1'];

Or, try with turning off the error_reporting :

 error_reporting(0);
janenz00
  • 3,315
  • 5
  • 28
  • 37
  • I am always impressed that smart people are willing to help newbies like me. In that regard, I have to admit that i'm still not getting this to work. I redid the two files so they look like the code above, except that I now reference $aarray['value1'] as suggested above. When I use firebug in chrome all seems to be correct (except I dont understand: sendToValue=%7B%22value1%22%3A%22foo%22%2C%22value2%22%3A%22bar%22%7D) To me this means that there is some value in the $_POST array corresponding to sendToValue but that the code to I use to access the value of 'value1' is not correct. – Bio_Xcoder Oct 12 '12 at 23:05
  • 1
    Im not sure why, but a stripslashes suggested here http://stackoverflow.com/a/2197308/1024783 solved the problem. Thanks Pablo! – Bio_Xcoder Oct 13 '12 at 01:01