5
votes

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

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

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

The ajax call is like below:

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:

$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
Split your $dec values by ':' then from that result the [0] element is your associated array key, and the [1] is your assoicated array value. Then json_encode that associated array at the end. - Taplar
@Taplar if I split it using ':', then I get something like ["fname"]->"abc,lname",["xyz,email"]->"email" and so on... - CoolJavaProgrammer
You don't split $content by ':'. You split each $dec string by ":" The entire string consists of name value pairs separated by comma, and each name value pair separated by colon. Thus you have to unwind that, the comma being the outer most separator, and then the colon being the inner seperator for each of those tokens. - Taplar

2 Answers

4
votes

Change quotes and add braces. Then you can decode resulting 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] => 
)
0
votes

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:

<?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;
}