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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.