0

Not sure if that title makes any sense but I'll try and explain it.

I am generating some content with PHP.

What I am attemtping to generate is a string that will spit out an URL with some JavaScript in the onlick event if the user has JavaScript enabled, and then a regular link if the JavaScript is not enabled.

$url = "<script type=\"text/javascript\">
    document.write(\"<a href='#' onclick='$('#UserID').val(" . 
            mysql_result($recordset, $count, "userid") . "); return false;'>
        Click here
    </a>\");
</script>
<noscript>
    <a href=\"/scripts/updateuser.php?id=" . 
            mysql_result($recordset, $count, "userid") . "\">Click here</a>
</noscript>"; 

The problem I am running into is the jQuery selector for #UserID. Since it's single quoted it bombs out there when clicked and processes the hash mark and goes to the top of the page ignoring the return false.

I can't put double quotes there because that's at the start and end of my document.write statement.

Any ideas on how to structure the string around the jQuery selector here using escape characters or whatever?

2
  • 3
    1. Don't use document.write, cmon, you have jQuery! 2. Don't use inline JavaScript 3. Don't use quotes into quotes, use apostrophe inside quotes 4. There is no id="UserID" element inside your script Commented Apr 1, 2011 at 19:14
  • Best practice would to not use document.write or 'onclick. Bind an event handler to the "click" JavaScript event using .click()`. Commented Apr 1, 2011 at 19:19

2 Answers 2

2

You can use $(\\'#UserID\\'). The double backslash is to make PHP put one extra backslash, making them usable in the onclick attribute. (Besides that, the first comment on the question should be followed rather than using this answer.)

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

2 Comments

What is the alternative to using document.write for this purpose? I want the browser to display the URL with the JavaScript if the user has JS enabled and the URL w/o JS if they don't have it enabled.
You should put the whole <a> tag (without JavaScript) there by default and give it an ID. Then in your JavaScript (which should be in an external file) use: $("#aTagId").click(function() {$('#UserID').val(queryResult); return false;}); after the page has been loaded. This way, the link will always be there, but it's clicking action will be replaced if JavaScript is present. (Don't forget to set the var queryResult in the head when you can still print stuff with PHP.
1

Good god that's ugly. Try this on for size:

$userid = mysql_result($recordset, $count, "userid");

$url = <<<EOL
<script type="text/javascript">
    document.write('<a href="#" onclick="$(\'#UserID\').val({$userid})); return false;">Click here</a>');
/script>
<noscript><a href="scripts/updateuser.php?id={$userID}">Click here</a></noscript>
EOL;

I won't get into how 1990's document.write is, but how about this:

<?php
    $userid = ...
?>
<a href="scripts/updateuser.php?id=<?php echo $userid ?>" onclick="$('#UserID').val({$userid}); return false;">Click here</a>

1 Comment

Ha ha, I know, real ugly. Your second solution works perfectly. Don't know why I brain cramped so badly on this, such a simple solution.

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.