0

I've got this javascript code:

$(document)
   .load(function(){
      $.post(
          'result_source.php?term='+<?php echo $_REQUEST['term']; ?>
        );alert('abc123');
});

and it doesn't alert('abc123');. If I remove the

+<?php echo $_REQUEST['term']; ?>

it does alert('abc123').

Thanks

1
  • 3
    You can't call PHP code from a Javascript file. sigh You can call HTML, JavaScript inside of a PHP file however. Learn the languages Commented Sep 7, 2013 at 0:21

2 Answers 2

1

You need to take the PHP part out of the concatenation. The PHP is effectively pasted in to the javascript page before it is processed, so unless your $_REQUEST['term'] is the name of a javascript variable you are using, it will cause errors.

Change it to: $(document).load(function(){$.post('result_source.php?term=<?php echo $_REQUEST['term']; ?>');alert('abc123');});

Bear in mind this won't work inside external javascript files, unless you create an .htaccess or something to configure the server so it parses .js files as PHP before outputting to the browser

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

Comments

0

PHP will not run in an external JavaScript file unless you create a .htaccess file or configure the server so it parses .js files as PHP before outputting to the browser.

If you put that in a file(with a .php extension), in <script> tags, it will work, though.

Comments

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.