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

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

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

If $job = "); alert('lol hax'" then you're in trouble.
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));?>);">

Comments

0

You can write in php directly

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

Comments

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>

1 Comment

What if $job contains an apostrophe? Or a double-quote? XSS alert!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.