1

There are tons of questions on passing strings to a javascript function - that's not what I'm asking here. My problem is that I have to pass an integer (a customer ID) as a string to a javascript function from PHP. The reason I have to do this is sometimes a customer ID can have leading zeros, and I do not know how many leading zeros will be needed, so I can't pad or format with leading zeros in javascript.

Here's the PHP that calls my function. The $row is a resulting array from an odbc query.

parse_str("customerId=" . $row["customerId"], $output);
// This is called via AJAX, so returning HTML as response
...
echo "<td><input type='checkbox' id='" . $output["customerId"] . "' onclick='disableAccount(" . $row["userAccountId"] . ", " . $output["customerId"] . ");' checked /></td>

The above works fine, and correctly passes a string with leading zeros to the funtion. Here's part of that function.

function disableAccount(userAccountId, customerId) {
// Do stuff here
}

As an example, I have a customerId of "026608". When the disableAccount function is called, the customerId parameter is parsed as an integer, which strips the leading zero, and I now have a parameter of '26608'.

As I mentioned above, I can't use padding to add a leading zero to the customerId, as there isn't always a leading zero on the customerId. The customerId can also have more than one leading zero.

How can I get my function to parse a string, i.e. "026608" as a parameter with the leading zeros? Thanks in advance!

4
  • 2
    If it has leading zeros it's not an integer, it's a string. If you need it to process with leading zeroes, send it as a string the entire way. Commented Jun 21, 2017 at 20:15
  • Your php is not wrapping that value in quotes as it writes out the params to the onclick handler, so it will be a number. Commented Jun 21, 2017 at 20:16
  • "The above works fine, and correctly passes a string with leading zeros to the funtion." No it doesn't. Commented Jun 21, 2017 at 20:22
  • What database are you using Riccaforte? Commented Jun 21, 2017 at 20:28

3 Answers 3

3

try to pass/escape the quotes so it gets parsed as string by the js

(\"" . $row["userAccountId"] . \""
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, thanks. I thought it was being passed correctly, because I'm also using this value to set the element's ID in a while loop. Since the ID was coming in correctly there, I thought it was being passed correctly to the function.
you´re welcome, glad it helped ;) this way your js-function is called disableAccount("026608"... instead of disableAccount(026608...
1

Great spot by john Smith, also another useful function for leading zeroes (though in this instance, not more efficient than john's) is the sprintf function.

$id = 321;

function disableAccount($userAccountId, customerId)
    {
    //Result is 000321
    $userAccIdWithZeros = sprintf('%06d', $userAccountId);

    //Rest of your code...
    }

Comments

1

Use the heredoc syntax whenever you produce HTML in PHP, it's more readable:

echo <<< _
<td>
    <input type="checkbox" 
           id="{$output['customerId']}" 
           onclick="disableAccount('{$row['userAccountId']}', this.id);" 
           checked />
</td>
_;

... because you don't have to escape your quotes, and you might have spotted your issue at once.

1 Comment

Thanks, I did not know about that!

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.