0

I have a php file which shows the username & password for a userID (via a mysql query). The userID is sent via jquery. I used json_encode to combine the username & password as an array. But when I attempt to extract the strings (username & password) from that combined (array) It seems that "whole array" was returned. How can I extract the strings (username & password) from a json_encoded array? These are my code snippets.

This is the file display.php

<?php
if(isset($_POST['id']))
{
    $ID=$_POST['id'];
    $userQuery = mysql_query("SELECT * FROM users WHERE userid ='".$ID."'");
    $UserArray = mysql_fetch_array($userQuery);
    $username=$UserArray[1];
    $passWord=$UserArray[2];
    $combinedArray = array($username,$passWord);
    echo json_encode(combinedArray);
}
?>

This is the JQuery which gets the strings from the combined array...

<script type="text/javascript">
    $.ajax({ 
        type: "POST",
        url: "display.php",
        data:{id:userID},
        success: function(returnedData)
        {
            $("#userNameDiv").html(returnedData);
            $("#passWordDiv").html(returnedData);
        }
    }); 
</script>

And this is the combined array as it displays...

["anandan2345","appleMac2015"]
["anandan2345","appleMac2015"]

But I need these strings in two separate variables...

Could anyone suggest a way to do this?

6
  • what do you mean by strings in two separate variables... ? Commented Aug 13, 2015 at 12:45
  • 1
    I think you want to display the username in the username div's only and password in the password div's? Commented Aug 13, 2015 at 13:03
  • @aldrin27: You got it..! Commented Aug 13, 2015 at 13:04
  • Why would you show a password? Passwords should always be hashed using a one-way algorithm like bcrype; it should be impossible to read a database and get a valid password from it. If your passwords are readable, then you are breaking pretty much every security best-practice there is. And you should certainly not be giving details from your users table without verifying that the request is legitimate (ie from the account owner); just querying the userid like that without any other checks means that anyone could read anyone else's account details just by guessing the userid. Ouch. Commented Aug 13, 2015 at 13:19
  • 1
    The other thing you need to be made aware of is that the mysql_xxx() functions in PHP are deprecated (and will be removed entirely in the next release of the language). You should stop using them, and switch to using either the mysqli or PDO libraries instead. Commented Aug 13, 2015 at 13:21

4 Answers 4

2

Use

 json_decode() 

to decode the json value

For example, If $combinedArray has the resultant value as below:

$combinedArray = array(["anandan2345","appleMac2015"]);
$temp = json_encode($combinedArray);
$tempres= json_decode($temp);
for($i=0;$i<count($tempres);$i++)
{
    for($j=0;$j<count($tempres[$i]);$j++)
    {
        echo $tempres[$i][$j].'<br/>';
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you show an example to eliminate the unwanted characters form that combined array, using json_decode() ?
Please give me the resulting array that you are getting without decode. I will show you how to do
["anandan2345","appleMac2015"]
2

This should work. returnedData is an array of two items. You just must need to insert each item on it's own div.

<script type="text/javascript">
    $.ajax({ 
        type: "POST",
        url: "display.php",
        data:{id:userID},
        dataType: 'json',
        success: function(returnedData)
        {
            $("#userNameDiv").html(returnedData[0]);
            $("#passWordDiv").html(returnedData[1]);
        }
    }); 
</script>

2 Comments

Yeah I tried this too... but the array-index gives a 'single character' only... It seems that 'returnedData' is a string as I saw.. ["anandan2345","appleMac2015"]
I've updated my answer. I added the parameter: 'dataType': json because jquery it's currently parsing returnedData as txt.
0

You can just change the blow code :

success: function (returnedData) {
    $("#userNameDiv").html(returnedData);
    $("#passWordDiv").html(returnedData);
}

such as the jquery function $.each() to fit your need.You can click here to learn the $.each.

1 Comment

Sorry... Couldn't understand. You mean $.each(returnedData) ..?
0

Oh my dear friends... I achieved..!

I avoid the "json_encoded" & used "implode" function in php to concatenate those username & password as following.

$combinedArray = array($username,$passWord);
echo implode("2015",$combinedArray );

Then in the Jquery, I use the 'split' method as following...

var mystr = returnedData;  
/*returndata is the concatenated string echoed by php*/
var myarr = mystr.split("2015");
var un = myarr[0];
var pw = myarr[1];

Now I'm having those two strings as 'un' & 'pw'

1000s of Thanks for your valued contribution...!

I have to thank especially @Simba... for his/her greatest advice...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.