Jump to content

Is it ok to embed php withing javascript (see below pls) ?


jd2007

Recommended Posts

function loadshopping()
  {
   <?php
   $theIP=$_SERVER['REMOTE_ADDR'];
   $_SESSION["ip"]=$theIP;?>
   document.write("Welcome, "+ <?php echo $_SESSION["ip"]; ?>);
   document.write("Quantity: "+ <?php echo "2"; ?>);
  }  

 

the above displays nothing but when i modify it to :

 

function loadshopping()
  {
   <?php
   $theIP=$_SERVER['REMOTE_ADDR'];
   $_SESSION["ip"]=$theIP;?>
   document.write("Welcome, "+ <?php echo "2"; ?>);
   document.write("Quantity: "+ <?php echo "2"; ?>);
  }  

 

it doesn't work if i echo a session variable...why...what can i do ?...pls help

Link to comment
Share on other sites

If you are running Javascript client side, you cannot embed php code in it.

PHP runs server side only, if your Javascript writes PHP code to the window, the PHP module never sees it.

PHP run "behind the scenes", the only way to get code to the module is at the server.

Or you can use Javascript to write form and input elements, that then submit to a PHP script, which is stored on the server. Although this is only for transferring values from Javascript to PHP.

PHP can send Javascript code, because the browser accepts it the same way it accepts HTML.

Remember, the Javascript function your working with is called 'document', it is exactly that, the document in the browser, and the browser has no clue what to do with PHP code.

 

I hope all that rambling made some sense to you.

Link to comment
Share on other sites

you have a syntax error, just remmeber the php on your javascript will be a string,

 

for example in this line

document.write("Welcome, "+ <?php echo $_SESSION["ip"]; ?>);

 

You need to include the echo inside the quote marks

document.write("Welcome,  <?php echo $_SESSION["ip"]; ?>");

 

So the HTML output will be something like this

document.write("Welcome, 123.345.667.23");

 

Let me know if this make sense.

Link to comment
Share on other sites

I realized something. While you cannot technically embed PHP in Javascript, you can mix them. I was thinking of a case where Javascript creates dynamic php code.

My post applies to the Javascript runtime, not mixing php code and Javascript code in the same file on the server, as it appears is your case.

Appologies, I missunderstood you situation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.