-6

I am trying to use some PHP code within Javascript statement. Now the syntax is a bit confusing bcoz of the quotes and I am unable to test it locally (cannot directly test it on the server)

So could you please confirm if the following line of code is correct;

"<a href='<?php somePHPFunction(" + $(this).text() + '.doc",10000,1,0); ?>'>" +  $(this).text() + "</a>"

Basically I need the following after evaluation;

<a href="http://myServer.com/DocNum123.doc">DocNum123</a>

The PHP function will return the server side path and file name without the extension (http://myServer.com/someFile)

Also $(this).text() would return DocNum123

5
  • what does SomePHPFunction do? Commented Oct 30, 2011 at 17:41
  • 3
    You can not execute PHP code from within javascript. I wonder why this is asked over and over again here on SO. Please use the search. - Possible duplicate: stackoverflow.com/questions/6116931/… Commented Oct 30, 2011 at 17:43
  • it just returns me the path w/o extension...myServer.com/someFile Commented Oct 30, 2011 at 17:44
  • Ok..so if i cannot use it within JS...how do I write the codeif I write inside my HTML code.. Commented Oct 30, 2011 at 17:45
  • $(this).text() inside the php function SomePHPFunction will not return any value. that makes no sense. Commented Oct 30, 2011 at 17:49

1 Answer 1

2

Hint: you can't.

PHP is server side. The code runs before the page is even sent to your browser. Javascript is client side. It is executed after the server sends you the page.

To get your code working would require to: - Run PHP code on the server, send you the page. - Run Javascript, "edit" the page - Request the server for another version of your page, now with the code.

You can, however, directly add it into your HTML.

Just use The file is at the location <?php SomeMethod(); ?>.

1
  • Actually when I say HTML, actually the extension is .php with HTML code...So hope that should work... Commented Oct 30, 2011 at 17:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.