-2

I am storing a string value in one php variable.I want to send this value to a function on image click. Here is code

userfn.php

$actvitycode = "AG001";
<img src="phpimages/addicon.gif" onclick="showData('.$actvitycode.')"></img>



getdata.js

function showData(actcode)
{
  alert(actcode);
  document.getElementById("tasklist").value = actcode;
}

Problem is, on image click the function is not calling. FYI: If I store integer value (ex:2342) in $actvitycode the function is successfully called and alert is showing the result. The problem with the string value only. I tried with several syntaxes like

onclick="showData("'.$actvitycode.'")"
onclick="showData($actvitycode)"

but no one syntax is giving result. Please provide me correct syntax.

1
  • To elaborate both answers below, you are using two different script languages and mix them together. Javascript only operates on the client (browser) and PHP only on the server. Commented Sep 14, 2012 at 18:44

4 Answers 4

1

You have to use the echo command:

<?php $actvitycode = "AG001"; ?>
<img src="phpimages/addicon.gif" onclick="showData('<?php echo $actvitycode; ?>')"></img>
1
<?php 
$actvitycode = "AG001";
// close php tag ?>
<img src="phpimages/addicon.gif" onclick="showData('<?php echo $actvitycode ?>')"></img>
1

you are trying to use a php variable in javascript. that is not possible, as javascript is client side ( in other words executes in clients browser ) and php is server side ( = executes on the server ).

<img src="phpimages/addicon.gif" onclick="showData('<?php echo $actvitycode ?>')"></img>

should work in this case.

keep in mind, that code in <?php ?> tags executes on the server ( for example apache ) before being sent to the client, while the rest of the markup and code along with the results of the server side code is sent "as is" to the client to display ( in case of html or css ) or execute ( in case of scripts - for example javascript ).

the code actually produces a javascript code - if $activity = 'foo', code produces a constant to parameters in showData function call :

<img src="phpimages/addicon.gif" onclick="showData('foo')"></img>
3
  • great, couldn't you post like 5 seconds earlier, you could have spared my comment :P Commented Sep 14, 2012 at 18:44
  • i spend way too much time being articulate :) Commented Sep 14, 2012 at 18:47
  • 1
    Yea :| .. for me, writing all that stuff isn't taking much time, reading it over and over again just drains the time away, it becomes pretty intense if you measure the time .. sometimes I just post a link and be done with it :P Commented Sep 14, 2012 at 18:50
0

Just do it in this way:

PHP file:

<? $actvitycode = "AG001";?>
<img src="http://www.kafkabrigade.org.uk/wp-content/uploads/2011/07/button-pic.jpg" onclick="showData('<?=$actvitycode?>')"></img>

<? $actvitycode = "AG002";?>
<img src="http://www.kafkabrigade.org.uk/wp-content/uploads/2011/07/button-pic.jpg" onclick="showData('<?=$actvitycode?>')"></img>

<input type="input" id="tasklist" value="">

JS file:

<script type="text/javascript" charset="utf-8">
    function showData(actcode) {
        alert(actcode);
        document.getElementById("tasklist").value = actcode;
    }
</script>

So in ')"> $actvitycode must be inserted or as or as . Actually it's the same.

Write if you have some question...

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.