Jump to content

Using PHP with InnerHTML in External JavaScript


phpBeginner06

Recommended Posts

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?

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?

<?

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.

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.

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

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.