Jump to content

Archived

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

tlsterling

JavaScript Parsing PHP

Recommended Posts

Hi there,

I'm trying to institute remote scripting using an iframe and I'm really stumped on this error.  This is my client.html file:

----------------------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html><head><title>Remote Scripting with an IFrame - complexer client page</title>
<meta http-equiv="pragma" content="no-cache">

<script type="text/javascript">

function buildQueryString(theFormName) {
theForm = document.forms[theFormName];
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='') {
qs+=(qs=='')?'?':'&'
qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
}
}
return qs
}

function messageReciept(name, msg) {
var theFormDiv = document.getElementById('theFormDiv');
theFormDiv.style.display = 'none';
var responseMessage = document.getElementById('responseMessage');
var message = '<p>Your message has been sent! Thank you.<\/p>'
message += ('<p>Your Name:  <\/p>' + name)
message += ('<p>Your Message:  <\/p>' + msg)
message += '<p><a href="#" onclick="showForm(); return false;">Send another message<\/a><\/p>'
responseMessage.innerHTML = message;
responseMessage.style.display = 'block';
}

var IFrameObj; // our IFrame object
function callToServer(theFormName) {
if (!document.createElement) {return true};
var IFrameDoc;
var URL = 'server.php' + buildQueryString(theFormName);
if (!IFrameObj && document.createElement) {
// create the IFrame and assign a reference to the
// object to our global variable IFrameObj.
// this will only happen the first time
// callToServer() is called
var tempIFrame=document.createElement('iframe');
tempIFrame.setAttribute('id','RSIFrame');
tempIFrame.style.border='0px';
tempIFrame.style.width='0px';
tempIFrame.style.height='0px';
IFrameObj = document.body.appendChild(tempIFrame);

if (document.frames) {
// this is for IE5 Mac, because it will only
// allow access to the document object
// of the IFrame if we access it through
// the document.frames array
IFrameObj = document.frames['RSIFrame'];
}
}

if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
// we have to give NS6 a fraction of a second
// to recognize the new IFrame
setTimeout('callToServer("'+theFormName+'")',10);
return false;
}

if (IFrameObj.contentDocument) {
// For NS6
IFrameDoc = IFrameObj.contentDocument;
} else if (IFrameObj.contentWindow) {
// For IE5.5 and IE6
IFrameDoc = IFrameObj.contentWindow.document;
} else if (IFrameObj.document) {
// For IE5
IFrameDoc = IFrameObj.document;
} else {
return true;
}

IFrameDoc.location.replace(URL);
return false;
}



function showForm() {
var theFormDiv = document.getElementById('theFormDiv');
theFormDiv.style.display = 'block';
document.forms['emailForm'].reset();
var responseMessage = document.getElementById('responseMessage');
responseMessage.style.display = 'none';
}

function processState(st) {
alert(st)
}
</script>

</head>
<body>

<div id="responseMessage" style="display:none"></div>

<div id="theFormDiv">
<form name="emailForm" id="emailForm"
action="server.php"
onsubmit="return callToServer(this.name)">
Your name:<br>
<input type="text" name="realname"><br>

Your message:<br>
<textarea name="message" cols="50" rows="10"></textarea><br><br>
<input type="submit" value="submit">
</form>
</div>

</body>
</html>
----------------------------------------------------------------------------------------

And this is what I've got for server.php:

---------------------------------------------------------------------------------------

<?php

    $username = "testName";
?>
<script language="javascript" type="text/javascript">
    parent.window.messageReciept('<?php =$username;?>' + 'ggg', '<?php echo "hhh";?>')
</script>

------------------------------------------------------------------------------

After entering some arbitrary values into the 'Your name' and 'Your message' box and submitting, this is the output I get:

-------------------------------------------------------------------------------

Your message has been sent! Thank you.

Your Name:
ggg

Your Message:

Send another message

-----------------------------------------------------------------------

I currently have the xampp (LAMP) installation set up on my machine and it seems to parse php files/commands with no trouble.  Any idea why I cannot get JavaScript to recognize/parse any php??

I'm really stumped, any help you can offer would be greatly appreciated, thanks!!!


Share this post


Link to post
Share on other sites
<?php
//server.php
?>
<script language="javascript" type="text/javascript">
    parent.window.messageReciept('<?php echo $realname;?>', '<?php echo $message;?>')
</script>

Share this post


Link to post
Share on other sites
Your php echoes javascript.

Javascript CAN NOT parse php EVER.

Please note the difference, it is an important one.

Share this post


Link to post
Share on other sites
    My apologies for the ambiguity, I should clarify.  It's not that I'm having trouble getting JavaScript to parse my PHP, but rather any PHP code in server.php located within the <script> tag does not seem to parse.  Any PHP located outside said <script> tags will parse just fine. 

    So, ideally what I'd like to happen and what I believe is supposed to happen is for the PHP to get parsed server side before it ever arrives back at the client page via the messageReciept call.  So all the client.html page sees are string values as those parameters.  That's not what's happening, the client page is getting nothing back when I try to incorporate a PHP command inside the <script> tag, but it will see literal text if that's all I include. 

I don't know why this is happening unless something's gone awry with my configuration since I've seen this exact format work just fine in the past.  Any ideas?? 

Share this post


Link to post
Share on other sites

×

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.