Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have got an issue with passing a json object to ajax at prototype.
here is the situation:
PHP:

<?php
    $a = array(k1=>v1, k2=>v2);
    $a = json_encode($a);
?>//JS 
<script>
    var a_js = <?= $a ?>
</script>
<?
    echo '<input type:"button" onclick='save(a_js) value='save js''>';
?>

JS

function save(a_json) {
    AJAX_ACTIONS_URL = 'ajax_action.php';
    params = {
        act: 'save_filters_status',
        a_json: a_json
    };
    new Ajax.Request(AJAX_ACTIONS_URL, {
        method: 'post',
        parameters: params,
        onSuccess: function (transport) {
            alert(transport.responseText);
        }
    });
}

Ajax_actions.php

if(var_post('act')=='save_filters_status') {
    $x = explode(',',var_post('a_js'));
    print_r($x);
}

Now, I don't see any post for a_js at fire fox, there is no response from the ajax, and I am a bit lost, thanks for your help.. Notice I work at JavaScript prototype frame work..

share|improve this question
'<input type:"button" onclick="save(a_js)" value="save js">' – Tal Dec 19 '12 at 13:38
hey i think my problem was that i havent use JSON.stringify at the js parts after get the var from the php.. so this is still an object. because this the output default between php ->js.. thanks for your help. sorry for posting syntax errors.. – Amir Maman Dec 26 '12 at 12:50

3 Answers

Your echo'd button has some faulty quote nesting:

 echo '<input type:"button" onclick='save(a_js) value='save js''>';

Should be:

 echo '<input type="button" onclick="save(a_js)" value="save js">';
share|improve this answer
I'd add that type:"button" isn't valid either. – Ranty Dec 19 '12 at 14:05
@Ranty: Derp, how could I have missed that! Thanks! – Cerbrus Dec 19 '12 at 14:15
i change it, but still he cant the json, when using alert at the js part i get (object object) – Amir Maman Dec 19 '12 at 18:37
@AmirMaman: Apparently transport.responseText is a object, then. Can you console.log() it, to see it's actual contents? – Cerbrus Dec 20 '12 at 7:34
 <?php
  $a=array(k1=>v1, k2=>v2)
  $a = json_encode($a)
  ?>//JS 
  <script>
    var a_js='<?=$a?>';
  </script>
  <?
  echo '<input type:"button" onclick='save(a_js) value='save js''>';
 ?>
share|improve this answer

hey i think my problem was that i havent use JSON.stringify at the js parts after get the var from the php.. so this is still an object. because this the output default between php ->js.. thanks for your help. sorry for posting syntax errors..

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.