0

I want my ajax call to only return the values from my array in test.php

At the moment the ajax call is returning all the code present in the php file.

How can I return only the json_encoded array?

jQuery Code:

var params = {'action': 'save' , 'id':5};
$.ajax({
    type: "POST",
    url: "test.php",
    data: params,
    success: function( data ) {
        $.each(data, function (index, value) {
            $('#menu_container a').eq( index).text( value);
        });
    }
});

test.php:

<?php
    $array = array();
    $i = 0;
    while ($i < $num) {
        $f1 = mysql_result($result, $i, "Page");
        $array[] = $f1; ?>

        <?php echo $f1; ?>

        <?php $i++;
    }
?>

</br>
</br>

<?php
    echo json_encode($array);
?>

</body>
</html>
1
  • when you say you're getting the code in the PHP file, you mean that the AJAX call is seeing the text inside the test.php file? if so, sounds like your web server isn't configured to run PHP. Commented Feb 24, 2012 at 11:38

1 Answer 1

1

Simply remove all other portions of your PHP code that generated output:

<?php
$array = array();
$i = 0;
while ($i < $num) {
    $f1 = mysql_result($result,$i,"Page");
    $array[] = $f1;
    $i++;
}
echo json_encode($array);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, now the only output from the php file is: ["Home","","test","test1","w","home","a"] However, how can I insert each value inside the quotations into the menu_container <a> tags? At the moment it inserts each individual character into the <a> tags e.g. [ " H o m e ",
I have done the same to my code and it works fine, however when I get the array code from my php file it doesn't output in the same way. It doesn't seem to see the array values as individual pieces of data like in your example.
Try: $('#menu_container a').eq( index).text(data[index]);

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.