通过POST从ajax调用到PHP页面获取JSON数据

时间:2022-10-18 00:27:53

I want to get some data from a form making AJAX call. I am getting the data as a string in my PHP page. The string looks like

我想从一个表单中获取AJAX调用的一些数据。我在PHP页面中将数据作为字符串获取。字符串看起来像

'fname':'abc','lname':'xyz','email':'','pass':'','phone':'','gender':'','dob':''

'FNAME': 'ABC', 'L-NAME': '某某', '邮件': '', '通': '', '电话': '', '性别': '', '出生日期': ''

Now I want to convert this entire string into array which would look like ["fname"] => "abc", ["lname"] => "xyz"... and so on

现在我想将整个字符串转换为数组,看起来像[“fname”] =>“abc”,[“lname”] =>“xyz”......依此类推

The ajax call is like below:

ajax调用如下:

fname = $("#form-fname").val();
lname = $("#form-lname").val();
email = $("#form-username").val();
pwd = $("#form-password").val();
phone = $("#form-phone").val();
gender = $("#form-gender").val();
dob = $("#form-dob").val();
var user = {"fname":fname,"lname":lname,"email":email,"pass":pwd,"phone":phone,"gender":gender,"dob":dob};
$.ajax({
      type: "POST",
      url: "doRegistration.php",
      data: user
 })
 .done(function( msg ) {                        
       window.location.href = "../profile.php";
 })
 .fail(function(msg) {
       sessionStorage.setItem("success","0");
       window.location.reload();
 }); 

And here is my PHP code:

这是我的PHP代码:

$content = file_get_contents("php://input");
file_put_contents("log1.txt",$content); //This produces the string I get above

Now I try to convert the string into array as above

现在我尝试将字符串转换为数组,如上所示

$dec = explode(",",$content);
$dec1 = array();
for($i=0;$i<count($dec);$i++)
{
    $dec1[i] = substr($dec[i],strpos($dec[i],":"));
}
//After this $dec1 is empty and count($dec1) gives 0.

But this does not give me the required array. I have checked several answers here, but they do not solve my issue. I tried to google but did not find any resolution. Is there something wrong in the code? Kindly help. Thanks in advance.

但这并没有给我所需的数组。我在这里检查了几个答案,但它们没有解决我的问题。我试图谷歌但没有找到任何解决方案。代码中有什么问题吗?请帮助。提前致谢。

2 个解决方案

#1


4  

Change quotes and add braces. Then you can decode resulting json

更改报价并添加大括号。然后你可以解码生成的json

$string = "'fname':'abc','lname':'xyz','email':'','pass':'','phone':'','gender':'','dob':''";

print_r(json_decode('{' . str_replace("'", '"', $string) . '}', true));

result

结果

Array
(
    [fname] => abc
    [lname] => xyz
    [email] => 
    [pass] => 
    [phone] => 
    [gender] => 
    [dob] => 
)

#2


0  

In your code above, you are using the index of the for loop for the key of the array. If you are trying to archive (I believe it is called dictionary in Java). You can try the following code:

在上面的代码中,您正在使用for循环的索引作为数组的键。如果您正在尝试存档(我相信它在Java中称为字典)。您可以尝试以下代码:

<?php
$string = "'fname':'abc','lname':'xyz','email':'','pass':'','phone':'','gender':'','dob':''";
$pieces=explode(',',$string);
foreach($pieces as $array_format){
list($key,$value) = explode(':',$array_format);
$array[$key]=$value;
}

#1


4  

Change quotes and add braces. Then you can decode resulting json

更改报价并添加大括号。然后你可以解码生成的json

$string = "'fname':'abc','lname':'xyz','email':'','pass':'','phone':'','gender':'','dob':''";

print_r(json_decode('{' . str_replace("'", '"', $string) . '}', true));

result

结果

Array
(
    [fname] => abc
    [lname] => xyz
    [email] => 
    [pass] => 
    [phone] => 
    [gender] => 
    [dob] => 
)

#2


0  

In your code above, you are using the index of the for loop for the key of the array. If you are trying to archive (I believe it is called dictionary in Java). You can try the following code:

在上面的代码中,您正在使用for循环的索引作为数组的键。如果您正在尝试存档(我相信它在Java中称为字典)。您可以尝试以下代码:

<?php
$string = "'fname':'abc','lname':'xyz','email':'','pass':'','phone':'','gender':'','dob':''";
$pieces=explode(',',$string);
foreach($pieces as $array_format){
list($key,$value) = explode(':',$array_format);
$array[$key]=$value;
}