Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Javascript within PHP

Status
Not open for further replies.

fswa

Programmer
Sep 2, 2005
6
US
Hello,
I am having an issue placing some javascript into a .php header file. I have tried to incorportate it into the scripting, but all I get is php errors.

I want to place a pop-up window script on a log-out link so that members who are leaving the site are asked if thats what they want to do. Here part of the .php header file and then below it, the javascript I need added.

<?php
#
# a_header.php
#
include('php/functions.php');

if (!isset($_SESSION['logged_in'])) $_SESSION['logged_in'] = False;

if ($_SESSION['logged_in']) {
$status = $_SESSION['first_name'].' '.$_SESSION['last_name'].' - Logged In';
} else $status = 'You Are Not Logged In';

$a_space = '&nbsp;&nbsp;&nbsp;';
print '<html>';
print '<head>';
print '<title>Are you Ready? - '.$status;
print '</title>';
print '<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">';
print '<LINK href="_style.css" type=text/css rel=stylesheet>';
print '</head>';
print '<body class=bodyclass background="images/header.jpg" leftMargin=0 topMargin=0 MARGINHEIGHT="0" MARGINWIDTH="0">';
print '<table class=header width="800" height="93" border=0 align=center cellPadding=0 cellSpacing=1>';
print '<tr>';
print '<td height="72">';
print '<img src="images/header4.gif" height="72"></td>';
print '</tr>';
#print '<tr><td height="1"><div bgcolor="white"></div></td></tr>';
print '<tr><td height="20">';
print '<table width="800" border="0" cellpadding="1" class=header>';
print ' <tr>';
print ' <td height="20" align="left">'.$status.'</td>';
print ' <td align="right"><div>';

if (isset($_SESSION['admin'])) {
if ($_SESSION['admin']) {
print '<a href="php/admin.php">ADMIN</a>'.$a_space;
}
}
if ($_SESSION['logged_in']) {
print '<a href="index.php">Home</a>'.$a_space;
print '<a href="member.php">Members Area</a>'.$a_space;
print '<a href="contact.php">My Contact Info</a>'.$a_space;
print '<a href="logout.php">Log Off</a>'.$a_space;
}
else {
print '<a href="index.php">Homepage</a>'.$a_space;
print '<a href="signup1.php">Sign-Up</a>'.$a_space;
print '<a href="login.php">Log-In</a>'.$a_space;
print '<a href="support.php">Support</a>'.$a_space;
}
print'</div></td>';
print'</tr>';
print'</table>';
print'</td></tr></table>'.chr(13);
print '<!-- end of header block -->'.chr(13);


Here is the javascript I need to have added to the code above.

<script Language="JavaScript">
<!--
function prelogout() {
window.name="mainwindow";
agree=window.open('prelogout.html','agreement','height=400,width=500,screenX=400,screenY=250,top=250,left=400');
return false;
}
//-->
</script>

onClick='agreement2();return false'"

Any help with this would be appreciated. I have tried adding it but when I do I get php errors.

Thanks
 
what error do u get???

Known is handfull, Unknown is worldfull
 
States I am getting a parse error on line 28
Line 28 reads
print ' agree=window.open('prelogout.html','prelogout','height=400,width=500,screenX=400,screenY=250,top=250,left=400');';
 
try:
print " agree=window.open('prelogout.html','prelogout','height=400,width=500,screenX=400,screenY=250,top=250,left=400');";

u were trying to include a ' within a '...

Known is handfull, Unknown is worldfull
 
now what does it say?

Known is handfull, Unknown is worldfull
 
I did it two different ways. One I changed the outside ' to be " and then did visa versa. One time it gave the same error and the next time it froze and the whole site wouldn't come up.
 
which time? id the site is freezing up it could mean anything(from php to javascript). where are u calling the JS function anyway...

Known is handfull, Unknown is worldfull
 
This is part of the file where I added the code.

include('php/functions.php');

if (!isset($_SESSION['logged_in'])) $_SESSION['logged_in'] = False;

if ($_SESSION['logged_in']) {
$status = $_SESSION['first_name'].' '.$_SESSION['last_name'].' - Logged In';
} else $status = 'You Are Not Logged In';

$a_space = '&nbsp;&nbsp;&nbsp;';
print '<html>';
print '<head>';
print '<title>Are You Ready - '.$status;
print '</title>';
print '<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">';
print '<LINK href="_style.css" type=text/css rel=stylesheet>';
print '<script Language="JavaScript">';
print '<!--';
print 'function prelogout() ';
print 'window.name="mainwindow";';
print 'agree=window.open('prelogout.html','prelogout','height=400,width=500,screenX=400,screenY=250,top=250,left=400');';
print 'return false;';
print '}';
print '//-->';
print '</script>';
print '</head>';

Then I call it here...

print '<a href="logout.php" onClick="prelogout();return false">Log Off</a>'.$a_space;
 
Once again, you cannot begin to delineate string with single quotes and then use single quotes inside that string without escaping them (like this \'). You can however, switch up quotes that delineate the string:
Code:
print '<html>';
print '<head>';
print '<title>Are You Ready - '.$status;
print '</title>';
print '<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">';
print '<LINK href="_style.css" type=text/css rel=stylesheet>';
print '<script Language="JavaScript">';
print '<!--';
print 'function prelogout() ';
print 'window.name="mainwindow";';
print "agree=window.open('prelogout.html','prelogout','height=400,width=500,screenX=400,screenY=250,top=250,left=400');";
print 'return false;';
print '}';
print '//-->';
print '</script>';
print '</head>';
This should work as far as php is concerned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top