Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 202827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:19:27+00:00 2026-05-11T17:19:27+00:00

I am starting to use the jquery $.ajax() but I can’t get back what

  • 0

I am starting to use the jquery $.ajax() but I can’t get back what I want to…I send this:

$(function() {
  $.ajax({
    url: "graph_data.php",
    type: "POST",
    data: "casi=56&nada=48&nuevo=98&perfecto=100&vales=50&apenas=70&yeah=60",
    dataType: "json",
    error: function(xhr, desc, exceptionobj) {
      document.writeln("El error de XMLHTTPRequest dice: " + xhr.responseText);
    },
    success: function(json) {
      if (json.error) {
        alert(json.error);
        return;
      }
      var output = "";
      for (p in json) {
        output += p + " : " + json[p] + "\n";
      }
      document.writeln("Results: \n\n" + output);
    }
  });
});

and my php is:

<?php

$data = $_POST['data'];

function array2json($data){ 
    $json = $data;

    return json_encode($json);
}
?>

and when I execute this I come out with:

Results:

just like that I used to have in the php a echo array2json statement but it just gave back gibberish…I really don’t know what am I doing wrong and I’ve googled for about 3 hours just getting basically the same stuff. Also I don’t know how to pass parameters to the “data:” in the $.ajax function in another way like getting info from the web page, can anyone please help me?


Edit

I did what you suggested and it prints the data now thank you very much =) however, I was wondering, how can I send the data to the “data:” part in jQuery so it takes it from let’s say user input, also I was checking the php documentation and it says I’m allowed to write something like:

json_encode($a,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP)

however, if I do that I get an error saying that json_encode accepts 1 parameter and I’m giving 2…any idea why? I’m using php 5.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:19:27+00:00Added an answer on May 11, 2026 at 5:19 pm

    First: the data you are passing to the PHP script will be in $_POST, not in $_POST['data'].

    Next: you need to actually call the array2json function with some data… Is that the full code sample?

    Also, I’m not sure what array2json is supposed to do… why not call json_encode directly?

    For your example you can simply make your PHP file look like this:

    print json_encode($_POST);
    

    Finally, you should get a tool like Firebug to easily debug your AJAX calls.

    EDIT:

    In order to send data from the user, all you have to do is have a form anywhere in your page, then catch the submit event of it and serialize the data, or simply get the value of an individual input field. Then you can just construct your data query with it, as a string or an object. There is also the very popular Form plugin for jQuery that makes it all easier.

    The reason you are getting an error is because the 2nd argument of json_encode was not added until PHP 5.3.0. So if you have anything earlier than that it’s not available to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the JSON data from the URL below and use it
I am starting to use some basic ajax calls in jQuery. The call works
I am starting a new project using WCF so that I can use jQuery
We are starting a new ASP.NET MVC application and want to use an jQuery
I'm trying to use AJAX (not jQuery).... I basically have this going on. Content
I'm just starting out with jQuery and want to use $(document).ready() to dynamically generate
I'm starting to use GameKit for a p2p project, and I want to call
I'm starting to use Role Management in my website, and I'm currently following this
I learned from this article that to avoid confliction between javascript libraries, use jQuery.noConflict();
I'm still learning JQuery (and as a result a little JavaScript) but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.