1

I have a js file. Is it possible to do inside the js file a php coding? On my header I include my js file like this.

<script type="text/javascript" src="js/playthis.js"></script>

now inside my jsfile:

function getURL(e) {
    alert(e+' javascript varibale');
    <?php $url; ?> = e;
    alert('<?php echo $url; ?>'+' php variable');
}

in my php file

<?php $url = "www.google.com"; ?>
<a href="#" onclick="getURL('<?php print $url; ?>');" class="title">

It's not working. Is there something wrong?

D. Schreier
  • 1,700
  • 1
  • 22
  • 34
Vincent
  • 852
  • 6
  • 29
  • 67
  • 3
    you can use ajax to call the url – Scary Wombat Dec 05 '13 at 03:00
  • You can have a look at [$.ajax()](http://api.jquery.com/jQuery.ajax/) – Arun P Johny Dec 05 '13 at 03:00
  • Are you trying to assign the value of a JavaScript variable to a PHP variable? No, you cannot do that. The only way JavaScript can push values to PHP is over AJAX. – user229044 Dec 05 '13 at 03:02
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Felix Kling Dec 05 '13 at 03:03
  • Note sure what you are trying to do, but you can write the data with php to the anchor data attribute and use a selector in jquery to get it. – Ray Dec 05 '13 at 03:07
  • @user2310289 i've updated my post. You'll see that i have a value on my $url on the body page. $url = "Google.com"; – Vincent Dec 05 '13 at 03:11

5 Answers5

1

No it's not possible to have PHP code inside JS file.

You can make $.ajax call to a PHP file and that file can handle the setting of the variable required.

Hope this helps.

Harsain
  • 121
  • 2
  • "No it's not possible to have PHP code inside JS file." what if i re-code my javascript and put it inside my php code. would that be possible now? – Vincent Dec 05 '13 at 03:10
1

There are a few ways to handle this.

1 - Have .js files parsed by php. You will need to change your webserver configuration to do this.
2 - Use AJAX to interact with php.
3 - Have php render an additional JS snippet in the body onload event that sets this JS parameter (but still have the library as a seperate file).

Mark
  • 330
  • 2
  • 9
1

this is not necessary,
when you print the url with print $url in onclick attribute inside the php page as an argument for getURL function, it means the value of $url will be sent to the function with name getURL in your js file...
so, you have to write:

 alert(e);

and if you are just asking, as the other friends told, you should use Ajax for this...
when you assign a variable to a function as an argument, just like getURL(e), the time that you call the function,getURL('print $url") you have to set the value of that variable, so, the program set the value you give to the function to the default variable...e = $url

Kiyarash
  • 2,437
  • 7
  • 32
  • 61
  • i want to pass the selected link in javascript to php because in php i will process it. – Vincent Dec 05 '13 at 03:38
  • is the url for current page or this is for another one? – Kiyarash Dec 05 '13 at 03:43
  • same page maybe? sorry I dont use ajax. but if youll show me some basic on how, then I would gladly to check it how. :) thanks maybe it will have another php if i'm gonna have a process on it and return it to the current page. like alert it maybe to try if it's working. – Vincent Dec 05 '13 at 03:46
1

you can change .js file to .php file and work with javascript and php together

change this

<script type="text/javascript" src="js/playthis.js"></script>

to this

<script type="text/javascript" src="js/playthis.php"></script>
1

you have to make a new object for Ajax transaction named XMLHTTP REQUEST in some browsers and in I.E this is ActiveXObject basically;
and this object belong to window object;
so the first step is:

if ( window.XMLHttpRequest ) {
    XHR = new XMLHttpRequest();
}
else if ( window.ActiveXObject ) {
    XHR = new ActiveXObject("Microsoft.XMLHTTP");   
}
else {
    alert("You Cant see because Your Browser Don't Support Ajax!!!\n Change Your Browser To A Modern One");
}

now, you have the right object
in next step, there is some methods for XHR object for send and receive you should know:
1/ onreadystatechange
2/readyState
3/status
4/open
5/send
6/setRequestHeader

first open a connection with open:

XHR.open("POST", url, true);

as you know, post is method of sending, and now you have to set the url you want to information send to, for example if you want to send the variable to the test.php, then the url is test.php...
true means the request will sent asynchronously.. next is set your request header, because your are using post method, in get method you don't need this:

        XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

next is sending the request with send method with this format send(name, value):

XHR.send('value=' + value);

first string 'value=' is the index of $_POST that you will get in php, so in your php code, you'll give this with $_POST['value'], if you set the name to 'name=', in php you have $_POST['name'], be careful that in Ajax send param you have to use = after the name of data, but in php the = is not used... after you sent the request; it's time to mange response:

XHR.onreadystatechange = function() {
  if ( XHR.readyState == 4 && XHR.status == 200 ) {
     document.getElementById('your target element for show result').innerHTML == XHR.responseText;
  }
}

XHR.readyState == 4 and XHR.status == 200 means every thing is O.K.

this is the basics of Ajax as you wished for; but there is so many information for Ajax, and either you can use jQuery Ajax method that is so simple and flexible; But as you want I described the basics for you...

Kiyarash
  • 2,437
  • 7
  • 32
  • 61
  • in post method, if you have more than one data, seprate them with `&` ampersand... 'name=' + value1 + '&' + 'name2=' + value2 – Kiyarash Dec 05 '13 at 04:15
  • with this. i think i know now what to do. :) i'll take this as an answer. – Vincent Dec 05 '13 at 04:46