douge3492 2015-07-08 01:06
浏览 70


I've tried over and over and it never works. I don't know how to receive the JSON on the server side.


function newReport_compilation(){
 return JSON.stringify(formData);
var formData = newReport_compilation(); //takes all values from my form
ajaxRequestTwo("databaseButler.php?reqType="+2, "text", formData, function(returnedData){
console.log(returnedData); //To test

function ajaxRequestTwo(reqScript, returnDataType, reqData, callback){
        type: "POST",
        dataType: returnDataType,
        url: reqScript,
        data: {fData:reqData},
        contentType: 'application/json; charset=UTF-8',
        success: function(data) {
            console.log("AJAX request success.");
        fail: function(){
            console.log("AJAX request failed.");
        error: function(){

PHP (DatabaseButler.php)

function reportInsert(){
    //Establish a new connection
    $repInsCon = new Db();
    //Run the insertReport function from the DB class (Send it the report ID & the JSON data of the submitted form)
    $result = $repInsCon->insertReport($_POST['fData']);
    echo $result;
    }else if($_GET['reqType']=="2"){

Deeper PHP (inside Db() class)

public function insertReport($jsonForm){
$newRepData = json_decode($jsonForm,true);
echo $jsonForm;
... //I have the SQL INSERT query part commented out here but I can't get here regardless

It gives me an error saying Notice: Undefined index: fData in C:\wamp\www\HCSProjects\Pigeon\Main\databaseButler.php

I don't understand how you receive the JSON data on the PHP side? What is the JSON called once it reaches PHP? How do I receive it properly on the server side? :/

I've been looking into this for hours on end. There's no proper answer anywhere.

The AJAX request works fine because I get a 4 and a 200 on the response. The following line in the butler.php file is the problem: (I removed the reqType by the way.... i just made the url databaseButler.php with no additional parameters).


'req' in the above line is one my JSON values. I console.logged the JSON before I sent it to PHP. (My JSON is reqData. I console.logged formData which is the same thing)

{"id":"1615","na":"Y","ph":"905-525-9140","em":"","dep":"Residence Admissions","req":"PC Issues","cus":"","summ":"diwjdiwjdi","det":"","pri":"Low","dat":"07/08/2015","ti‌​m":"anytime"}

This is my newReport_compilation function by the way:

function newReport_compilation(){
    var formData = {id:hash(1),na:$("#newReport_name").val(),ph:$("#newReport_phone").val(),em:$("#newReport_email").val(),dep:$("#newReport_department").val(),req:$("#newReport_requestCategory").val(),cus:$("#newReport_otherRequest").val(),summ:$("#newReport_summary").val(),det:$("#newReport_details").val(),pri:$("input[type='radio'][name='priority']:checked").val(),dat:$("#newReport_date").val(),tim:$("#newReport_time").val()};
    return JSON.stringify(formData);

How do I get my JSON values on the PHP side? There's no help anywhere guys :/


For those interested:
The problem was the JSON.stringify(formData) in newReport compilation. Apparently you don't have to stringify formData if it was stored in JSON like form. It was like I tried to convert an already JSON string to JSON, which broke it.

Also, when you send data in both the URL and in JSON form, access everything using $_REQUEST and not $_POST or $_GET. However any JSON data you send over goes by the format you send the data. In my case I used POST. So to get ID or name or the server side (DatabaseButler PHP) you would use $_POST['id'] or $_POST['summ'].

  • 写回答

3条回答 默认 最新

  • du6jws6975 2015-07-08 01:20

    HTTP request can be POST or GET but not both and your code is trying to access variables through $_POST and $_GET, and that is not OK. I would suggest to use $_REQUEST instead and it will take care of it (if POST or GET). Despite of using type: "POST", jQuery will take it as a "GET" request because you are pointing to an URL with a querystring.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dtdb99743 2015-07-08 01:09

    pass it as JSON instead of text:

    ajaxRequestTwo("databaseButler.php?reqType=2", "json", formData, function(returnedData)

    This will give you more debugging error:

    error: function(xhr, status, error) {
      var err = eval("(" + xhr.responseText + ")");
  • dpspn60064 2015-07-08 01:18

    You First Change

    ajaxRequestTwo("databaseButler.php?reqType="+2, "**text**", formData, ....


    ajaxRequestTwo("databaseButler.php?reqType="+2, "json", formData, ....

    You should set datatype to json no text! $.ajax({ dataType: "json",... });

    in php you must Before Use $_GET or $_POST data, check if(isset($_POST['...'])) or if($_POST['...'])




  • ¥15 ECharts 增加Zoom,整行包括右边的Text一起滑动
  • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
  • ¥15 开地址法双散列函数处理碰撞
  • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
  • ¥15 联通光猫掉注册了怎么重新注册上去
  • ¥15 关于unity开发steamvr程序遇到的问题
  • ¥60 求tc downloader的下载方式
  • ¥15 华为 快捷方式 手电筒 接口
  • ¥15 Qt6.5支不支持Android13开发啊
  • ¥20 网络只能跑一半,应该如何设置