0

I am creating one website in php. and i am using jquery ajax to pass data from one page to another. and that is general process for all pages.

my javascript function is :

function add_data()
{       
  var searchinOn = mainObjectAddForm['available_add_fields'].split(","); 
  var whereOn="{";
  var where = "";
  for (i=0;i<searchinOn.length;i++)
  {
    var keyValue= searchinOn[i];
    where+=getAddData(mainObjectAddForm[keyValue]['name'],mainObjectAddForm[keyValue]['text'],mainObjectAddForm[keyValue]['datatype'],mainObjectAddForm[keyValue]['data'],keyValue)  

  }  

  whereOn+= where.substring(0,where.length-1)+"}";

  $.ajax({
    type:'POST',
    data:whereOn,
    url:siteurl + "add/",
     dataType:'json',
    success:function(data)
    {
    }
  });
 }



 function getAddData(name,text,datatype,data,keyValue)
 {  

 var control="";
 var value1="";
 var value2="";
switch(datatype)
{
    case "text":                         
        control +=  "'"+name+"':'"+trim($('#add_form #'+name).val())+"'";
        break;
    case "password":
        control +=  "'"+name+"':'"+trim($('#add_form #'+name).val())+"'";           
        break;      
    case "label":
        control +=  "'"+name+"':'"+trim($('#add_form #'+name).val())+"'";           
        break;          
    case "textarea":
        control +=  "'"+name+"':'"+trim($('#add_form #'+name).val())+"'";           
        break;
}
if (trim(control)=="") return "";
else return control + ",";
}

but on php page i am not getting all values. Its coming as one string in post variable.

like

 [{'field1':'abc','field2':'pqr','field3':'xyz'}] => 

but i want like this :

Array
{
[field1] => abc
[field2] => pqr
[field3] => xyz
}

and i know the problem that the problem is the ajax data is passing as one string because the '{' and '}' are in whereOn string. but if i add that in ajax call than also its same problem.

can anyone help me to solve it ?

4

2 回答 2

2

几个建议:

  1. 永远不要手动构建 JSON。你有一个很好的JSON.stringify(..)功能,所有现代浏览器都支持。
  2. 您在 POST 正文中发送原始 JSON。为什么不设置标题Content-Type: application/json
  3. POST 正文不是百分位数编码的。$_POST所以使用数组没有意义。您需要做的是通读整个 POST 正文file_get_contents('php://input')以获取 JSON-String。然后您可以使用json_decode(..)将其转换为可以以通常方式使用的 PHP 对象。

    $postBody = file_get_contents('php://input');
    var_dump($postBody);
    $jsonObj = json_decode($postBody);
    var_dump(jsonObj);
    
于 2013-04-15T04:11:04.723 回答
0

如果您尝试手动构建 json,请查看http://json.org。我建议不要这样做,并为此使用 json 。
而是使用 application/x-www-form-urlencoded (键/值对)

function add_data()
{       
  var searchinOn = mainObjectAddForm['available_add_fields'].split(","); 
  var whereOn={};
  var where = "";
  for (i=0;i<searchinOn.length;i++)
  {
    var keyValue= searchinOn[i];
    getAddData(mainObjectAddForm[keyValue]['name'],mainObjectAddForm[keyValue]['text'],mainObjectAddForm[keyValue]['datatype'],mainObjectAddForm[keyValue]['data'],keyValue,whereOn)  

  }  

  $.ajax({
    type:'POST',
    data:whereOn,
    url:siteurl + "add/",
     dataType:'json',
    success:function(data)
    {
    }
  });
 }



function getAddData(name,text,datatype,data,keyValue,whereOn)
{  
 switch(datatype)
 {
    case "text":                         
        whereOn[name] = trim($('#add_form #'+name).val());
        break;
    case "password":
        whereOn[name] = trim($('#add_form #'+name).val());           
        break;      
    case "label":
        whereOn[name] = trim($('#add_form #'+name).val());           
        break;          
    case "textarea":
        whereOn[name] = trim($('#add_form #'+name).val());           
        break;
 }

}
于 2013-04-15T04:10:56.817 回答