Can someone tell me what is wrong with the following syntax: if ($_SESSION['genericNewConfig']) { echo '<INPUT TYPE=submit VALUE="Save" NAME="generic_savebutton" ONCLICK="verifySave()" />'; echo '<INPUT TYPE=reset VALUE="Cancel" NAME="generic_cancelbutton" ONCLICK="verifyCancel()" />'; } PHP: I keep getting Error: syntax error
Nothing wrong with it... I even copy/pasted the code into a new PHP file to make sure and it didn't give any errors.
Okay it must be something else i'm doing, here's the long story.... I have this method that gets executed first thing called init(), which works fine at first: <?php function init() { if ($_SESSION['genericNewConfig']) { echo '<INPUT TYPE=submit VALUE="Save" NAME="generic_savebutton" ONCLICK="verifySave()" />'; echo '<INPUT TYPE=reset VALUE="Cancel" NAME="generic_cancelbutton" ONCLICK="verifyCancel()" />'; } else { echo '<INPUT TYPE=button VALUE="Save" NAME="generic_savebutton" DISABLED="disabled" />'; echo '<INPUT TYPE=button VALUE="Cancel" NAME="generic_cancelbutton" DISABLED="disabled" />'; } } ?> PHP: later on in my code i have a javascript method called createNewConfig() which gets called when i hit "New Config" button, which at the end of it calls the init method to check the status of the SESSION variable. However, once it hits the first line in the init() function it gives me a syntax error. <HTML> <HEAD> <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript"> function createNewConfig() { //window.location='systemconfig-generic.php'; document.getElementById('systemselect').style.display='none'; document.getElementById('systemselect').value=""; document.getElementById('systemtext').style.display=''; document.getElementById('systemtext').focus(); <?php $_SESSION['genericNewConfig'] = true; ?> <?php init()?> } </SCRIPT> </HEAD> </HTML> HTML:
This maybe a silly comment as I have never tried what you are doing but can one mix javascript and PHP like that?
Just to let you know, I'm using Mozilla Firefox's JavaScript Console and it keeps outputting this: Error: syntax error Source File: http://localhost/systemconfig-generic.php Line: 132, Column: 10 Source Code: <INPUT TYPE=submit VALUE="Save" NAME="generic_savebutton" ONCLICK="verifySave()" /><INPUT TYPE=reset VALUE="Cancel" NAME="generic_cancelbutton" ONCLICK="verifyCancel()" /> }
Thats a javascript (client-side) error, not a php (server-side) error. You need to look at line 132 of the html output, not the php code. Do a "view source" and look at line 132. Mat
I just looked at your code again, and while you can "mix PHP and Javascript like that", you can't put HTML in JavaScript. Put your PHP call to init() in the body of your document, and outside any script tags.
Thanks. I understand what you mean, however, i'm not sure i know where exactly to place the init() call. Would i be able to perhaps have 2 method calls for the onclick event, so that they get called one after the other?? <INPUT TYPE=button VALUE="Create New Configuration" NAME="newconfigbutton" ONCLICK="createNewConfig()" ONCLICK="init()">
I'm not sure why you need to split it up? Move the php code to be like: </HEAD> <body> <?php $_SESSION['genericNewConfig'] = true; ?> <?php init(); ?> </body> </HTML> Code (markup):
The code you posted earlier doesn't look like a complete page. Is there more to the page that you're not showing? What errors are you getting now. My first suggestion, not knowing the error, would be to put <form> tags around the <input> tags generated by your PHP.
<FORM ID="genericform" ACTION="systemconfig-generic.php" METHOD="post"> <?php init(); ?> <CENTER><INPUT TYPE=button VALUE="Create New Configuration" NAME="newconfigbutton" button.ONCLICK="createNewConfig()"> <?php function init() { if ($_SESSION['genericNewConfig']) { echo '<INPUT TYPE=submit VALUE="Save" NAME="generic_savebutton" ONCLICK="verifySave()" />'; echo '<INPUT TYPE=reset VALUE="Cancel" NAME="generic_cancelbutton" ONCLICK="verifyCancel()" />'; } else { echo '<INPUT TYPE=button VALUE="Save" NAME="generic_savebutton" DISABLED="disabled" />'; echo '<INPUT TYPE=button VALUE="Cancel" NAME="generic_cancelbutton" DISABLED="disabled" />'; } } ?> </FORM> </BODY> <HTML> <HEAD> <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript"> function createNewConfig() { //window.location='systemconfig-generic.php'; document.getElementById('systemselect').style.display='none'; document.getElementById('systemselect').value=""; document.getElementById('systemtext').style.display=''; document.getElementById('systemtext').focus(); <HEAD> <BODY> <?php $_SESSION['genericNewConfig'] = true; ?> <?php init(); ?> </BODY> </HTML> } function verifySave() { msg = "Are you sure you want to save this data?"; //all we have to do is return the return value of the confirm() method return confirm(msg); } function verifyCancel() { msg = "Are you sure you want to cancel?"; //all we have to do is return the return value of the confirm() method return confirm(msg); } </SCRIPT> </HEAD> </HTML> Code (markup):
I'm getting syntax errors that point right at the <HEAD> tag in this chunk of code: <HEAD> <BODY> <?php $_SESSION['genericNewConfig'] = true; ?> <?php init(); ?> </BODY> </HTML>