I am unable to send a string from php page to java script page. I am storing a string in a php variable and I want to send this value to java script page on image icon click. Here is the source code:

timetrack.php

$activityid = $getResult->fields('ACTIVITY_ID');    // Now consider the value in $activityid is "AG001"
<img src="phpimages/tt_comment.gif" title="Add remarks" alt="add remarks icon" style="border:none;cursor:pointer;margin:0 0 0 5px" onclick="showRemarks('.$activityid.');" id="remarksadd"></img>

userfn.js

function showRemarks(actid)
{
alert(actid);
document.getElementById("tt_comment_section").value = actid;
}

Here the function showRemarks(actid) itself is not calling.
FYI: If I store integer value(ex: 256487) in $activityid variable, the function is calling successfully and alert is showing the result. Problem coming with the string alone. I have tried with different syntaxes like

onclick="showRemarks("'.$activityid.'");"
onclick="showRemarks('".$activityid."');"
onclick="showRemarks(".$activityid.");"
onclick="showRemarks($activityid);"

but no one giving result. Please give me the solution.
Thanks.

Dani AI

Generated

Good catch by and — and well done for finding the immediate cause. The underlying point is simple: JavaScript needs a string literal (quotes) but PHP must emit that literal correctly and safely. Integers worked because they can be emitted as raw numeric literals; alphanumeric IDs must be quoted and escaped when inserted into an inline handler.

A robust, safe way to emit a PHP string into an inline onclick is to let PHP produce a properly escaped JavaScript literal with json_encode. Using single quotes for the HTML attribute avoids nested-quote issues:

<img src="phpimages/tt_comment.gif"
  onclick='showRemarks(<?php echo json_encode($activityid); ?>);' />

json_encode returns a valid JS string literal (with quotes and escapes), so you avoid broken attributes and syntax errors when the value contains quotes or backslashes.

A cleaner, more maintainable pattern is to keep data in HTML attributes and attach event handlers from JavaScript (no inline JS). Escape the attribute for HTML, then read it from dataset:

<img class="remark-icon"
  data-activity-id="<?php echo htmlspecialchars($activityid, ENT_QUOTES, 'UTF-8'); ?>" />

<script>
document.querySelectorAll('.remark-icon').forEach(function(img){
  img.addEventListener('click', function(){
    showRemarks(this.dataset.activityId);
  });
});
</script>

Quick troubleshooting checklist: make sure PHP tags are opened/closed and the variable is in scope; prefer json_encode or htmlspecialchars rather than manual quoting; check the browser console for ReferenceError if the injected token was treated as an identifier (e.g., AG001 without quotes); and protect against XSS by never injecting untrusted input into HTML/JS without escaping.

Recommended Answers

All 3 Replies

Have you tried:

onclick="showRemarks('<?php echo $activityid ?>');"
commented: you got me for a few seconds :-) +6

If you want to use a php variable in html enclose it within the <?php ?> tags and echo it:

onclick="showRemarks(' <?php echo $activityid; ?>');" id="remarksadd"></img>
commented: Same solution as I offered :) +1

Thanks for your reply JayJ and broj1. Your solution is correct. The mistake I have done is, forget to close php tag before html img tag. Here is the answer.

<?php
$activityid = $getResult->fields('ACTIVITY_ID');    // Now consider the value in $activityid is "AG001"
?>
<img src="phpimages/tt_comment.gif" title="Add remarks" alt="add remarks icon" style="border:none;cursor:pointer;margin:0 0 0 5px" onclick="showRemarks('<?php $activityid ?>');" id="remarksadd"></img>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.