phpBeginner06 Posted February 13, 2007 Share Posted February 13, 2007 Does any one know how to add a php echo or print to a form field that is in an external javascript? I have an external JS with a function that populates a DIV via innerHTML. It places a form in the DIV, through innerHTML. I want to be able to put this in a form field in my external javascript: <? echo "$Name"; ?> and when the function fires; the innerHTML would insert the form in the DIV and the php echo/print would display in the form field. But when I use the above code in my form field of my external javascript; it just inserts the value as text, not actual php code. Any one know how to do this? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 if you're using apache you could add AddType application/x-httpd-php .js to a .htaccess file of the js dir Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 13, 2007 Author Share Posted February 13, 2007 genericnumber1, That will not work for me; I do not have apache. Any other ideas? - Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 You have to make the file a php file and use the header to specify that it is javascript <? header('Content-type: application/x-javascript'); print "alert('test');"; ?> That should work. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted February 13, 2007 Share Posted February 13, 2007 what you're looking to do is process the page as php before the javascript inserts into the page - am i correct? if so, you need to look into using ajax Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 13, 2007 Author Share Posted February 13, 2007 If I change the external JS to PHP and include the header; how do I declare a javascript function and how do I connect it to my other php file? would I use an php include? If I use ajax; how would I go about doing what I want to do - not read enough about coding with ajax yet? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 <? header('Content-type: application/x-javascript'); print "alert('test');"; ?> function myJSFunc(){ } Do your regular js outside the php, just like HTML. Then link to the php file just like you would the JS file. Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 13, 2007 Author Share Posted February 13, 2007 thanks jesirose - i will give it a try i may be back to post more on this thread later but if i am not - thanks to everyone for their help Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 14, 2007 Author Share Posted February 14, 2007 I tried the code below; the function fired, but the echo did not occur when I typed in a query string of "http://www.somedomain.com/page1.php?Name=Bob" into the address bar. The form field still remained empty. <?php header('Content-type: application/x-javascript'); print "function contactme(){document.getElementById('txtsec').innerHTML='All fields with red star above them are required.<br><br><form name=\"contactfswd\" action=\"contactform.php\" method=\"Post\">Name <span style=\"color:red\">*</span><br><input type=\"text\" name=\"Name\" class=\"field\" value=\"$Name\"><br><br>Email <span style=\"color:red\">*</span><br><input type=\"text\" name=\"Email\" class=\"field\"><br><br>Phone:<br><input type=\"text\" name=\"Phone\" class=\"field\"><br><br><textarea name=\"Comment\" class=\"field\"></textarea><br><br><input type=\"submit\" value=\"Submit\" class=\"button\"> <input type=\"reset\" value=\"Reset\" class=\"button\"></form>';}"; ?> I also tried this: <?php header('Content-type: application/x-javascript'); ?> function contactme() { document.getElementById('txtsec').innerHTML='All fields with red star above them are required.<br><br><form name="contactfswd" action="contactform.php" method="Post">Name <span style="color:red">*</span><br><input type="text" name="Name" class="field" value="<?php echo "$Name"; ?>"><br><br>Email <span style="color:red">*</span><br><input type="text" name="Email" class="field"><br><br>Phone:<br><input type="text" name="Phone" class="field"><br><br><textarea name="Comment" class="field"></textarea><br><br><input type="submit" value="Submit" class="button"> <input type="reset" value="Reset" class="button"></form>'; } But I still did not get any value in the textbox when I sent a query string through address bar. I linked the above script to my main php page (the one that innerHTML displays in) in this fashion: <script type="text/javascript" src="somepageXternalJSCode.php"></script> Why am I not getting this to work and how can I fix this, so that it will work? Oh, I used a DOM onload event from the <body> tag too fire contactme() function - I do not think that would matter though. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2007 Share Posted February 14, 2007 Where do you call contactme()? Is that file in the same folder? Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 14, 2007 Author Share Posted February 14, 2007 I call it through an onload event in body tag. External JS in same directory as main php page; that links to js. Example: + www.somedomain.com (root directory) + page1.php + somepageXternalJSCode.php Both of the above are in same directory Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2007 Share Posted February 14, 2007 Can you link to the actual page, so I can use my debugging tools for ya Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 14, 2007 Author Share Posted February 14, 2007 External PHP/JS Code - aka "02-14-2007-2.php" <?php header('Content-type: application/x-javascript'); print "function contactme(){document.getElementById('txtsec').innerHTML='All fields with red star above them are required.<br><br><form name=\"contactfswd\" action=\"contactform.php\" method=\"Post\">Name <span style=\"color:red\">*</span><br><input type=\"text\" name=\"Name\" class=\"field\" value=\"$Name\"><br><br>Email <span style=\"color:red\">*</span><br><input type=\"text\" name=\"Email\" class=\"field\"><br><br>Phone:<br><input type=\"text\" name=\"Phone\" class=\"field\"><br><br><textarea name=\"Comment\" class=\"field\"></textarea><br><br><input type=\"submit\" value=\"Submit\" class=\"button\"> <input type=\"reset\" value=\"Reset\" class=\"button\"></form>';}"; ?> Actual Main PHP Page - aka "02-14-2007.php" <html> <head> <title>Untitled</title> <script type="text/javascript" src="02-14-2007-2.php"></script> </head> <body onload="contactme()"> <div id="txtsec"> </div> </body> </html> Thank You jesirose - Sorry I did not have link; I already erased page from directory - So I sent actual code that I had saved to my desktop. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2007 Share Posted February 14, 2007 It works for me on my server. Can you post an actual link to it, and what it does wrong? Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted February 14, 2007 Author Share Posted February 14, 2007 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.