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 8077495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:29:25+00:00 2026-06-05T15:29:25+00:00

Im submitting Data to a php file via AJAX using POST. It worked fine

  • 0

Im submitting Data to a php file via AJAX using POST.
It worked fine with just submitting strings, but now I wanted to submit my JS Object with JSON and decode it on PHP side.

In the console I can see, that my data is submitted correctly but on PHP side json_decode returns NULL.

I’ve tried the following:

this.getAbsence = function()
{
    alert(JSON.stringify(this));
    jQuery.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "ajax/selectSingle.php?m=getAbsence",
        data: JSON.stringify(this),
        success : function(data){
            alert(data);
        }
    });
}

PHP:

echo $_POST['data'];
echo json_decode($_POST['data']);
echo var_dump(json_decode($_POST['data']));

And:

this.getAbsence = function()
{
    alert(JSON.stringify(this));
    jQuery.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "ajax/selectSingle.php?m=getAbsence",
        data: {'Absence' : JSON.stringify(this)},
        success : function(data){
            alert(data);
        }
    });
}

PHP:

echo $_POST['Absence'];
echo json_decode($_POST['Absence']);
echo var_dump(json_decode($_POST['Absence']));

The alert was just to check everything is alright…

And yea usual string were echoed correctly 🙂

  • 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-06-05T15:29:27+00:00Added an answer on June 5, 2026 at 3:29 pm

    Where you went wrong in your code in the first code is that you must have used this:

    var_dump(json_decode(file_get_contents("php://input"))); //and not $_POST['data']
    

    Quoting from PHP Manual

    php://input is a read-only stream that allows you to read raw data from the request body.

    Since in your case, you are submitting a JSON in the body, you have to read it from this stream. Usual method of $_POST['field_name'] wont work, because the post body is not in an URLencoded format.

    In the second part, you must have used this:

    contentType: "application/json; charset=utf-8",
    url: "ajax/selectSingle.php?m=getAbsence",
    data: JSON.stringify({'Absence' : JSON.stringify(this)}),
    

    UPDATE:

    When request has a content type application/json, PHP wont parse the request and give you the JSON object in $_POST, you must parse it yourself from the raw HTTP body. The JSON string is retrieved using file_get_contents("php://input");.

    If you must get that using $_POSTyou would make it:

    data: {"data":JSON.stringify({'Absence' : JSON.stringify(this)})},
    

    And then in PHP do:

    $json = json_decode($_POST['data']);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm submitting a form using an ajax request (POST method), and checking the HTTP
I'm submitting data using the jQuery plugin 'Jeditable' and that part is working fine.
I have followed three tutorials, one on submitting form data to a php file,
I'm having a problem with my ajax call. I'm submitting some info via php
My PHP file doing 2 operations: 1. Submits data from form into db table,
How do I start with creating a file upload mechanism that works via Ajax?
I have this form: <form name=commentform id=commentform action=comment.php method=post enctype=multipart/form-data> Your Name: <textarea maxlength=60
I've been studying PHP for a while now, and Ajax for about a month
I have a form which uses ajax for data submitting. Here is the html:
i have an form which will send some data to an php file ,

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.