Link to home
Start Free TrialLog in
Avatar of sfmy
sfmy

asked on

Inserting PHP variable in document.write inside JS file

Hi all.

I'm using XsitePro software for my web development. There is a popup feature that can be used and this popup used JS file.

What ever code i writte will be output by document.write in the JS file. I need to insert a php variable inside the doc but it won't processed. Instead it will output exactly as the php code is written.

Just to give a clear view, i posted the whole code inside the doc.

I intend to insert a value to a hidden field from a php variable like so somewhere in the code,
<input type="hidden" name="custom_JID" value="<? echo $JID; ?>">

Unfortunately the value isn't processed n outputted as <? echo $JID; ?> :(

Please help, thanks.
document.write('<div id="popin2" pos="mc" style="display:none;top:5000;left:0;width:238;height:320;padding:10px;z-index:1000;"><table width="100%" height="100%" cellspacing="0" cellpadding="2" style="font:;border:1px solid Maroon;"><tr style="background-color:Maroon;color:White;font: normal normal none 10pt Arial, Sans-Serif;"><th style="background-color:Maroon;color:White;font-weight: normal ; text-decoration: none; font-style: normal ; font-size: 10pt ; font-family: Arial, Sans-Serif;" width="100%" height="10" >Info KopiBerasMerah.com</th><th style="padding-left:1ex;padding-right:1ex;"><a href="javascript:popin2_close()" style="color:White;font:normal 10pt Verdana;text-decoration:none;">&#215;</a></th></tr><tr><td height="100%" colspan="2" style="background-color:White;color:black;"><div style="width:100%;height:100%;overflow:auto; FONT-FAMILY:Verdana; FONT-SIZE: x-small; COLOR: Black"><div class="wFormContainer">     <div class="wForm wFormdefaultWidth"><form method="post" action="http://www.getresponse.com/cgi-bin/add.cgi" accept-charset="UTF-8" class="labelsAbove hintsTooltip"> <div class="htmlsection" id="html-tmp-3138164830948"> <p align="center"><b>Dapatkan Informasi Terkini!</b></p> <ul> <li>Promosi Terkini &amp; Istimewa!</li> <li>Dapatkan Kopi PERCUMA!</li>  <li>Peluang Menjadi "Affiliate"</li> </ul> <div align="center"><b>Isikan Nama &amp; Email Anda!</b><br> </div> </div> <br> <div class="oneField"> Nama <input type="text" id="tfa_Nama" name="category2" value="" size="30" class="required"> <br><span class="errMsg"> </span> </div> <div class="oneField"> Email <input type="text" id="tfa_Email" name="category3" value="" size="30" class="validate-email required"> <br><span class="errMsg"> </span> </div> <br> <div class="actions" align="center"> <input type="submit" value="HANTAR"> <input type="hidden" name="custom_JID" value="<? echo $JID; ?>">  <input type="hidden" name="category1" value="kopiberasmerah">  <input type="hidden" name="confirmation" value="http://kopiberasmerah.com/Terima-Kasih-Kerana-Melanggan-Info-KopiBerasMerah.php">  <input type="hidden" name="ref" value="910"> <input type="hidden" name="getpostdata" value="get"> </div> </form></div> </div></div></td></tr></table></div>');

Open in new window

Avatar of gamebits
gamebits
Flag of Canada image

is the page saved with a .php extension?
Avatar of sfmy
sfmy

ASKER

Sure
I think the issue here is that javascript is client-side and php is server-side. PHP isn't parsing the code because it's not seeing it...the visitor's browser is (and doesn't know what to do with it). If possible, I would recommend making a .php file with a javscript block with the document.write(..) stuff in it. Since the server process the .php file, it will insert the value of the php variable into the document.write() statement before sending it to the visitor to be parsed as javascript code.

If that's not doable, then you may be able to look into setting the value via
document.getElementById('custom_JID').value=<?php echo $JID;?>
Do you have short tags enabled in php.ini? If not your <? ?> tag will not be parsed

This is a safer bet:

<input type="hidden" name="custom_JID" value="<?php echo $JID; ?>">

Open in new window

or, the full code from your question with full <?php ?> tags:

document.write('<div id="popin2" pos="mc" style="display:none;top:5000;left:0;width:238;height:320;padding:10px;z-index:1000;"><table width="100%" height="100%" cellspacing="0" cellpadding="2" style="font:;border:1px solid Maroon;"><tr style="background-color:Maroon;color:White;font: normal normal none 10pt Arial, Sans-Serif;"><th style="background-color:Maroon;color:White;font-weight: normal ; text-decoration: none; font-style: normal ; font-size: 10pt ; font-family: Arial, Sans-Serif;" width="100%" height="10" >Info KopiBerasMerah.com</th><th style="padding-left:1ex;padding-right:1ex;"><a href="javascript:popin2_close()" style="color:White;font:normal 10pt Verdana;text-decoration:none;">&#215;</a></th></tr><tr><td height="100%" colspan="2" style="background-color:White;color:black;"><div style="width:100%;height:100%;overflow:auto; FONT-FAMILY:Verdana; FONT-SIZE: x-small; COLOR: Black"><div class="wFormContainer">     <div class="wForm wFormdefaultWidth"><form method="post" action="http://www.getresponse.com/cgi-bin/add.cgi" accept-charset="UTF-8" class="labelsAbove hintsTooltip"> <div class="htmlsection" id="html-tmp-3138164830948"> <p align="center"><b>Dapatkan Informasi Terkini!</b></p> <ul> <li>Promosi Terkini &amp; Istimewa!</li> <li>Dapatkan Kopi PERCUMA!</li>  <li>Peluang Menjadi "Affiliate"</li> </ul> <div align="center"><b>Isikan Nama &amp; Email Anda!</b><br> </div> </div> <br> <div class="oneField"> Nama <input type="text" id="tfa_Nama" name="category2" value="" size="30" class="required"> <br><span class="errMsg"> </span> </div> <div class="oneField"> Email <input type="text" id="tfa_Email" name="category3" value="" size="30" class="validate-email required"> <br><span class="errMsg"> </span> </div> <br> <div class="actions" align="center"> <input type="submit" value="HANTAR"> <input type="hidden" name="custom_JID" value="<?php echo $JID; ?>">  <input type="hidden" name="category1" value="kopiberasmerah">  <input type="hidden" name="confirmation" value="http://kopiberasmerah.com/Terima-Kasih-Kerana-Melanggan-Info-KopiBerasMerah.php">  <input type="hidden" name="ref" value="910"> <input type="hidden" name="getpostdata" value="get"> </div> </form></div> </div></div></td></tr></table></div>');

Open in new window

Avatar of sfmy

ASKER

Basic61, the short tag is enable.

Geowrian, since i'm using a software, i basically don't want to mess with the default code written to output the popup which is using

document.write('MYCODECANONLYBEHERE');

If i were to edit the file, manually, it will be overwrite the next time i edit n publish from the software.

How do u suggest i write that code?

This one doesn't work




<input type="hidden" document.getElementById('custom_JID').value=<?php echo $JID;?>>

Open in new window

SOLUTION
Avatar of geowrian
geowrian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sfmy

ASKER

Tried that, the php value was parsed (looking at the source code) but it is not inserted in the hidden input.

Do i need to change any parameter?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm glad you got this figured out. However, you used my idea and other's assistance to get that, so I request that our ansers are accepted.
Avatar of sfmy

ASKER

I thought  i already accept multiple solution and 300 points for you. Hurm did i do something wrong? Any admin could help me?
Got it. My bad. I'm new to the EE format and didn't read that part of it fully. I'll be more careful in the future.