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

LVL 1
sfmyAsked:
Who is Participating?
 
sfmyConnect With a Mentor Author Commented:
Hello,

I figure out the way already.
The main point is to insert the value from a php variable into a hidden input

Using the idea from geowrian, actually a simple javascript worked it out on all browser

Thanks all!

<script type="text/javascript">
document.getElementById('custom_JID').value ='<? echo $JID; ?>';
</script>


<input type="hidden" name="custom_JID" id="custom_JID" value="">
 
<script type="text/javascript">
document.getElementById('custom_JID').value ='<? echo $JID; ?>';
</script>

Open in new window

0
 
gamebitsCommented:
is the page saved with a .php extension?
0
 
sfmyAuthor Commented:
Sure
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
geowrianCommented:
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;?>
0
 
basic612Commented:
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

0
 
basic612Commented:
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

0
 
sfmyAuthor Commented:
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

0
 
geowrianConnect With a Mentor Commented:
I understand the situation why you can't just update the javascript file. Here's an option. Anywhere that javascript code is called in your PHP file, add this code below and let me know how that works out.
<script type="text/javascript" lanaguage="Javascript">
  document.getElementById('popin2').forms[0].custom_JID.value='<?php echo $JID;?>';
</script>

Open in new window

0
 
sfmyAuthor Commented:
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?
0
 
geowrianCommented:
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.
0
 
sfmyAuthor Commented:
I thought  i already accept multiple solution and 300 points for you. Hurm did i do something wrong? Any admin could help me?
0
 
geowrianCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.