0

I am trying to call a JavaScript function with parameter as php script using the following code

<a href="javascript: submitform(<?php echo $job; ?>)">submit</a>

any suggestions will be appreciated

  • 1
    you can't Javascript is client side. and php is server side. they won't understand each other on their own. – Yaje Jul 03 '14 at 09:55
  • @ejay_francisco I wrote the javascript inside the php file but the problem is with the parameter, how its should be written – user3801082 Jul 03 '14 at 09:58

4 Answers4

1

Use something like this

<a href="#" onclick="submitform(<?php echo $job; ?>);"> submit</a>

OR

var job  ='';
job  = <?php echo $job; ?>;
<a href="#" onclick="submitform(job);"> submit</a>
user3244721
  • 714
  • 2
  • 11
  • 29
0

If $job is anything but a number, your code will fail.

To drop a PHP variable into a JavaScript context, you must use json_encode. Since you're in an HTML attribute, you must also use htmlspecialchars.

So:

<a href="javascript:submitform(<?php echo htmlspecialchars(json_encode($job));?>);">
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You can also use any of this two

<a href="javascript:void(0);" onclick="submitform(<?php echo $job; ?>);">submit</a>
<a href="javascript:;" onclick="submitform(<?php echo $job; ?>);">submit</a>
Mrinmoy Majee
  • 366
  • 1
  • 13
0

You can write in php directly

<?php
    echo '<a href="#" onclick="javascript: submitform(\"'+$job+'\")">submit</a>';
?>
durgas09
  • 30
  • 6