[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Inserting PHP variable in document.write inside JS file

Posted on 2009-05-10
13
Medium Priority
?
1,716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 12

Assisted Solution

by:geowrian
geowrian earned 1200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

649 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