Jump to content

Onclick event with PHP 2 lines, one word


Dzherzinsky

Recommended Posts

Hello everybody

 

There is one word here that I dont get to include the right way, I think because of the issue of single and double quotes. When I replace the variable by its value, it works me alright, but since I have to include this PHP variable ($user) within the onclick JS function, it is problematic.

 

<?php

echo '
<form>

    
<input type="checkbox" name="asset" value="" onclick="getLoggedInUser($user) " /> I am potentially interested in this product  service<br />
<div id="txtHint"><b>Note:</b></div>


</form>

 

';

By the way, I cant believe the verification code to submit the text. It took me 2 minutes to figure out what they were talking about John, George, Paul, and  I had no idea what the hell that was, when I casually thought of the Beatles. so Ringo. I hate that rubbish music. Next time I get verification code like that I go elsewhere.

Link to comment
Share on other sites

Variables between single-quotes are not parsed. Concatenate the value:

 

[...] onclick="getLoggedInUser(' . $user . ') " [...]

 

Although unless $user is numeric, you're going to get a parse error in your JavaScript as you don't have quotes around the parameter value. If that's the case try:

 

[...] onclick="getLoggedInUser(\'' . $user . '\') " [...]

Ain't pretty, but neither's inline JS ;)

Link to comment
Share on other sites

@Adam

 

 

Thank you very much! it worked and thanks for the tip about being numeric, I have no problem in turning $user to $user_id so that is numeric.

 

That code snippet is part of an AJAX code. I was forced to do a weird thing like this because in my System the only way to get the user_id was through the built in PHP function get_userID() of my system (a social network)  and then I would be inserting that data into the JS. Naturally I would have written 'this.value' if I were getting the IDs, say from a Select box but it had to locate the user_id, thanks God (and to you!) it worked, I was beginning to dread that my idea risking client server with client side languages would hate to talk to each other, but it did the trick

 

Best regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.