3

I am using dojo and ajax to send a time stamp to PHP, which does a database check, then returns the time stamp for debugging purposes. When I send this time stamp, it's a number, when it is returned, it is a string. Is there a specific reason for this? What should I do to avoid this (cast to int in PHP, fix via JSON, or cast to int in javascript)

Here is the Dojo code

dojo.xhrGet({
 url: 'database/validateEmail.php',
 handleAs: "json",
 content: {
 email : '[email protected]',
 time: 0
 },
 load: function(args) {/*SEE BELOW*/}
});

Here is the PHP script

<?php

/**
 ** connect to the MySQL database and store the return value in $con
 *
 */
$con = mysql_pconnect("localhost:port", "username", "password");

/**
 ** handle exceptions if we could not connect to the database
 *
 */
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

/**
 ** Create table query
 *
 */
mysql_select_db("portal", $con);

/**
 ** Get user entered e-mail
 *
 */
$emailQuerry = mysql_num_rows(mysql_query("SELECT EMAIL FROM user WHERE EMAIL='" . $_GET["email"] . "'")) == 1;

/**
 ** Whether successful or not, we will be returning the time stampe (this is used to determine whether there were any changes between the time a request
 ** was sent, and when this response was returned.
 *
 */
 $result['time'] = $_GET["time"];

/**
 ** Currently only checks to see if the two values were provided. Later, will have to check against passwords
 *
 */
if ($emailQuerry) {
    $result['valid'] = true;
}
else {
    $result['valid'] = false;
}

echo json_encode($result);
?>

And finally the load function left blank above

load: function(args) {
 console.log(localArgs.time + ' v ' + args.time);
 console.log(localArgs.time === args.time);
 console.log(localArgs.time == args.time);
}

The output of which is

0 v 0
false
true
17
  • @Blender -- all the password info is still in the history ^_^ Commented Nov 1, 2011 at 14:23
  • 1
    You can replace this whole block: if ($emailQuerry) {... } with just $result['valid'] = $emailQuerry; Commented Nov 1, 2011 at 14:25
  • @Neal, better in the history than out in the open. Thanks for telling erryone ;) Commented Nov 1, 2011 at 14:26
  • @Blender lol i flagged it for a mod. Commented Nov 1, 2011 at 14:28
  • 1
    That wasn't actually my password...it was my attempt at humor. Commented Nov 1, 2011 at 14:34

3 Answers 3

3

json_encode encodes all variables as a string.

So the javascript will see it as a string.

So in the javascript you could use parseInt(...)

Sign up to request clarification or add additional context in comments.

5 Comments

Is json the best/preferred/accepted way carry out what I am doing right now? I would prefer to do things correctly from the beginning. Thanks
@puk it is the perferred way for me (if that is enough for you ^_^)
json_encode does not encode all values as a string. Numbers, true, false, and null are all encoded correctly using json_encode. The actual cause is that all scalar $_GET parameters are string, so it should be converted to number there: $result['time'] = (float)$_GET["time"];
@Thai, I'll just convert it client side then.
@Neal I like your answer but I am coming under considerable heat for choosing it since some of your statements are inaccurate. Could you please mention JSON_NUMERIC_CHECK and change 'have to use' to 'could use'
2

You can use json_encode() to output your numbers from PHP (post-encode), with the JSON_NUMERIC_CHECK option.

1 Comment

This is good to know, however, GET returns a string always, so this approach does not apply here.
1

To send an integer out as an integer is simple - provide json_encode with one! Put '(int)' around anything that you want converting.

Here's an example:

echo json_encode(array(1, 2, 3));

Output:

[1,2,3]


And another:

$a = '123';
echo json_encode(array($a, (int) $a));

Output:

["123",123]

2 Comments

Your answer is also correct, but I'm going to go with Neal's b/c I think it's easier to do these calculations client side. If anyone believes that this must be done server side, I will consider changing the correct answer back over to muu.
@puk: Neil's method is valid however he is wrong that it's the only way.

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.