Solved

Inserting PHP variable in document.write inside JS file

Posted on 2009-05-10
13
1,685 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:sfmy
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24348032
is the page saved with a .php extension?
0
 
LVL 1

Author Comment

by:sfmy
ID: 24348056
Sure
0
 
LVL 12

Expert Comment

by:geowrian
ID: 24348144
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
 
LVL 6

Expert Comment

by:basic612
ID: 24348334
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
 
LVL 6

Expert Comment

by:basic612
ID: 24348347
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
 
LVL 1

Author Comment

by:sfmy
ID: 24348387
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Assisted Solution

by:geowrian
geowrian earned 300 total points
ID: 24349848
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
 
LVL 1

Author Comment

by:sfmy
ID: 24350005
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
 
LVL 1

Accepted Solution

by:
sfmy earned 0 total points
ID: 24352603
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
 
LVL 12

Expert Comment

by:geowrian
ID: 24355886
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
 
LVL 1

Author Comment

by:sfmy
ID: 24357436
I thought  i already accept multiple solution and 300 points for you. Hurm did i do something wrong? Any admin could help me?
0
 
LVL 12

Expert Comment

by:geowrian
ID: 24358319
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now