Paul-D Posted February 24 Share Posted February 24 Hi. Can someone tell me why I cant use the else in this if statment please? <?php if($_SESSION["DE_Retain"] == 1) ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>"; value="Des"> Max 32 Characters<br><br> <?php else ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>"; value="Dave"> Max 32 Characters<br><br> Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/ Share on other sites More sharing options...
jodunno Posted February 24 Share Posted February 24 (edited) 1 hour ago, Paul-D said: Hi. Can someone tell me why I cant use the else in this if statment please? Hi Paul-D, you are moving in-and-out of php (spaghetti code). You will need to use the curly braces in your branch, so that php knows that the else belongs to the previous if statement. You can echo or print by using a single-quote, double-quote combo: echo 'i can use "double-quotes" within single quotes'; <?php session_start(); $_SESSION["DE_Retain"] = 0; if(!empty($_SESSION["DE_Retain"])) { echo 'show if'; } else { echo 'show else'; } $x = 1; if ($x == 1) { ?> x = 0 <?php } else { ?> x = 0 <?php } ?> you should avoid spaghetti code whenever possible. Sometimes it is okay but an entire php script written with spaghetti code is very difficult to follow and you will find yourself asking alot of questions in phpfreaks forum because it is difficult to debug such code. edit: I meant to type x = 1. sorry. Edited February 24 by jodunno erroneous sample code Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650410 Share on other sites More sharing options...
Paul-D Posted February 24 Author Share Posted February 24 I thought that you only needed to use the brackets if there is more than one condition. So this is the correct format? Never thought of it as spagetti code. <?php if($_SESSION["DE_Retain"] == 1) { ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>" value="Des"> Max 32 Characters<br><br> <?php } else { ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>" value="Dave"> Max 32 Characters<br><br> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650422 Share on other sites More sharing options...
Barand Posted February 24 Share Posted February 24 18 minutes ago, Paul-D said: I thought that you only needed to use the brackets if there is more than one condition. You don't need the braces so long as there are only single statements to be executed but only if they are in the same <?php .. ?> block of code. EG this works... <?php if (date('d')==24) echo 'today'; else echo 'Not today'; ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650423 Share on other sites More sharing options...
gizmola Posted February 24 Share Posted February 24 The lack of indentation makes it entirely unclear what the control flow of your script has, since indentation is missing and it's hard to understand by looking at it, what runs or doesn't. This is generally considered to be spaghetti. The best practice for intermingling HTML and PHP is to utilize the PHP alternative syntax tags, which allow your code to integrate more cleanly with your html. You are already using the "<?= ?>" shorthand syntax for individual values, which is good. Extend that to also using the control tag structure, and add indentation. Make sure that your editor is configured to use spaces for tabs! Once you have done that, it's possible in most editors to go back and replace all your tab characters with your standard tab size. Typically people choose either 2 or 4 spaces per tab. If you adhere to https://www.php-fig.org/psr/psr-12/ then it should be 4 spaces of indentation (a tab should create 4 spaces). As this is a snippet from a larger script, we are obviously missing the context of the rest of the code, but I re-wrote your snippet using the alternative syntax for comparison. <?php if ($_SESSION["DE_Retain"] == 1): ?> <input type="text" maxlength="32" size="42" name= "<?= $Comment_Name ?>" value="Des"> Max 32 Characters<br><br> <?php else: ?> <input type="text" maxlength="32" size="42" name= "<?= $Comment_Name ?>" value="Dave"> Max 32 Characters<br><br> <?php endif; ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650424 Share on other sites More sharing options...
mac_gyver Posted February 24 Share Posted February 24 here's a slightly different approach. only write conditional code for the things that are - conditional. the only thing that's different in the output is the value (which actually doesn't make practical sense, since this is a field for a user to enter something into.) this also falls under Don't Repeat Yourself (DRY) coding. your if/else logic, which can then use the simple ternary operator, should only supply the desired 'Des' or 'Dave' value. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650428 Share on other sites More sharing options...
Paul-D Posted February 25 Author Share Posted February 25 Thanks for all your help. I will try and indent better. I am using Notepad++ but it doesn't always get it right. as for using {} it can be confusing when diving in and out of PHP and HTML so I will keep to always using them in the future. Thanks for all your help here Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650442 Share on other sites More sharing options...
jodunno Posted February 25 Share Posted February 25 (edited) 30 minutes ago, Paul-D said: it can be confusing when diving in and out of PHP and HTML that is where the expression Spaghetti code comes into play (weaving in-and-out of a language creates a noodle like appearance) and fortifies why it should be avoided if posible. Once you exit php, how is php to know that the else belongs to the previous if without further clarification? when php encounters the else, it responds with the error because an if does not precede it in the same section of code. Adding curly braces removes the ambiguity, or using end if as suggested by Gizmola. Edited February 25 by jodunno typo Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650448 Share on other sites More sharing options...
Dragon_Queen Posted February 25 Share Posted February 25 18 hours ago, Paul-D said: Hi. Can someone tell me why I cant use the else in this if statment please? <?php if($_SESSION["DE_Retain"] == 1) ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>"; value="Des"> Max 32 Characters<br><br> <?php else ?> <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>"; value="Dave"> Max 32 Characters<br><br> I am a beginner to php so correct me if I'm wrong, but I think if you're using if-else statements then both should be within the same php tag. Since you are trying to set a value "Des" or "Dave", you can use a single php tag in which you could create a php variable to store the value and then assign it after closing the php tag. This could also help avoid the spagetti code situation as explained by Jodunno. So to adjust the code you provided: <?php if ($_SESSION["DE_Retain"] == 1) { $value = "Des"; } else { $value = "Dave"; } ?> <input type="text" maxlength="32" size="42" name="<?=$Comment_Name ?>" value="<?=$value?>"> Max 32 Characters<br><br> See if this works and if it does, do let me know as well. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650458 Share on other sites More sharing options...
Paul-D Posted February 25 Author Share Posted February 25 (edited) Dragon queen it is a bit more complicated than that. I have a form with text boxes and drop-down boxes. I want one text box to retain the contents when after submitting the page to another code page and then returning back. This is stored in a session variable. I have added a check box to indicate that I want to retain the content. if unchecked then it forgets and I get a blank text box. Attached is an image of the page and the complete code. P.S. On working this through. Is there a function to clear all my session variables so I can start with a clean sheet? http://desmond-otoole.co.uk/Comment.jpg Comment <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>" value="<?=$_SESSION["DE_Comment"] ?>"><br><br> </div> <?php if($_SESSION["DE_Retain"] == 1) { ?> Retain comment: <input type="checkbox" maxlength="1" size="1" name="Retain" checked> <?php } else { ?> Retain comment: <input type="checkbox" maxlength="1" size="1" name="Retain"> <?php } ?> <div style="width:650px;height:40px;padding:10px;overflow:hidden;"> Edited February 25 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650468 Share on other sites More sharing options...
mac_gyver Posted February 25 Share Posted February 25 what this would look like using the method i posted above - <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name?>" value="<?=$_SESSION["DE_Retain"] == 1 ? $_SESSION["DE_Comment"] : ''?>"> Max 32 Characters<br><br> likewise for the checkbox logic - Retain comment: <input type="checkbox" maxlength="1" size="1" name="Retain" <?=$_SESSION["DE_Retain"] == 1 ? 'checked' : ''?>> i'm pretty sure that checkboxes don't have maxlength or size attributes. if any of these variable may not exist, to prevent php errors, you need to use the null coalescing operator ?? to set default values. if you want to clear all the session data, you can use session_destroy();. if you only want to clear this 'form' data you are passing around, while keeping the current user logged in, you should store this data in a session array variable, such as $_SESSION['post']. you can then clear this data by just unsetting it - unset($_SESSION['post']); 1 Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650472 Share on other sites More sharing options...
Paul-D Posted February 25 Author Share Posted February 25 (edited) With your checkbox logic. If a checkbox is not checked it does not get posted unlike other controls on a web page. Edited February 25 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650474 Share on other sites More sharing options...
gizmola Posted February 25 Share Posted February 25 5 hours ago, Paul-D said: With your checkbox logic. If a checkbox is not checked it does not get posted unlike other controls on a web page. That is how PHP works. An "unchecked" checkbox is not sent in the POST data. Your serverside validation should check for this, and handle it accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650486 Share on other sites More sharing options...
gizmola Posted February 25 Share Posted February 25 6 hours ago, Paul-D said: P.S. On working this through. Is there a function to clear all my session variables so I can start with a clean sheet? Yes, just unset any specific variables in $_SESSION. Generically you can use: foreach (array_keys($_SESSION) as $key) { unset($_SESSION[$key]); } This is the recommended way that doesn't cause the entire session mechanism to cease functioning. The user will still have a session, but it will be empty. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650487 Share on other sites More sharing options...
maxxd Posted February 25 Share Posted February 25 In any code that I work with that doesn't use a templating engine I try to label my conditionals, like so: <div class="slew-of-form-elements"> <?php if($myVar === true): ?> <input type="text" name="field_1"> <select name="field_2"> <option value="-1">Select an option</option> <?php foreach($options as $key=>$value): ?> <option value="<?= $key; ?>"><?= $value; ?></option> <?php endforeach; // $options as $option ?> </select> <?php endif; // $myVar === true ?> </div> Obviously this is a contrived example and is missing things like output sanitization and the many, many, many form elements some of the code deals with but hopefully the point comes across. 1 Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650494 Share on other sites More sharing options...
Paul-D Posted February 26 Author Share Posted February 26 The nightmare continues. I am getting an Undefined array key "DE_Retain" in /vhost/d/e/s/desmond-otoole.co.uk/www/bank2/DataInput.php on line 218 I have pre set this session in SecureFunctions.php Line 276 in function Session_Init() http://desmond-otoole.co.uk/SessionVariable.jpg SecureFunctions.php (part) function Session_Init() { if (!isset($_GET['counter'])) $_GET['counter'] = ""; if (!isset($_SESSION['current_page'])) $_SESSION['current_page'] = ''; if (!isset($_SESSION['Event_Log'])) $_SESSION['Event_Log'] = ''; if (!isset($_SESSION['K9'])) $_SESSION['K9'] = ''; if (!isset($_SESSION['Survalance'])) $_SESSION['Survalance'] = ''; if (!isset($_SESSION["K208"])) $_SESSION["K208"] = ''; if (!isset($_SESSION["Error_1"])) $_SESSION["Error_1"] = ''; if (!isset($_SESSION["Error_2"])) $_SESSION["Error_2"] = ''; if (!isset($_SESSION["Error_3"])) $_SESSION["Error_3"] = ''; if (!isset($_SESSION["Error_4"])) $_SESSION["Error_4"] = ''; if (!isset($_SESSION["Error_5"])) $_SESSION["Error_5"] = ''; if (!isset($_SESSION["Current"])) $_SESSION["Current"] = ''; if (!isset($_SESSION["DE_Retain"])) $_SESSION["DE_Retain"] = '0'; if (!isset($_SESSION["DE_Comment"])) $_SESSION["DE_Comment"] = ''; // Email Sessions if (!isset($_SESSION["Name"])) $_SESSION["Name"] = ''; if (!isset($_SESSION["Name2"])) $_SESSION["Name2"] = ''; if (!isset($_SESSION["Email"])) $_SESSION["Email"] = ''; if (!isset($_SESSION["Subject"])) $_SESSION["Subject"] = ''; if (!isset($_SESSION["Msg"])) $_SESSION["Msg"] = ''; } <?php // Banking Version 2.0.0 01-12-2023 Desmond O'Toole. error_reporting(E_ALL); ini_set('display_errors', '1'); include_once ("../secure/SecurePDO.php"); include_once ("secure/SecureFunctionsBankPDO.php"); date_default_timezone_set('Europe/London'); session_start(); Session_Init(); $page = "Libray Selection"; if(!isset($_SESSION["Pk"])) { header('Location: index.php'); exit; } $Pk = $_SESSION["Pk"]; if(KeyCheckX($Pk)== 0) { header('Location: index.php'); exit; } $EndTime = KeyTestX($Pk, 0); //SILLY this returns 1 or -1 $_SESSION["current_page"] = $page; $Comment_Name = time(); $Comment_Name = dechex(time()); $_SESSION['COMMENT_NAME'] = $Comment_Name; $DD = date('d'); // Default to current date $MM = date('m'); $YYYY = date('Y'); $qBalance = GetBalance(); $Date = $qBalance['EntryDate']; $Value = $qBalance['BalanceValue']; $stamp = strtotime($Date); $_SESSION["DD_Balance"] = date('d' ,$stamp); $_SESSION["MM_Balance"] = date('m' ,$stamp); $_SESSION["YYYY_Balance"] = date('Y' ,$stamp); $_SESSION['C_Forward'] = $Value; $SE_MyReason = $_SESSION['DE_MyReason']; if($_SESSION["DE_DD_Entry"] == "") { $_SESSION["DE_DD_Entry"] = $DD; $_SESSION["DE_MM_Entry"] = $MM; $_SESSION["DE_YYYY_Entry"] = $YYYY; } $ThisYear = date("Y",time()); $NextYear = $ThisYear + 1; $PreviouseYear = $ThisYear - 1; if (!isset($_SESSION['DE_Money_In'])) $_SESSION['DE_Money_In'] = '0.00'; if (!isset($_SESSION['DE_Money_Out'])) $_SESSION['DE_Money_Out'] = '0.00'; if (!isset($_SESSION['DE_Comment'])) $_SESSION['DE_Comment'] = ""; $SE_Money_In = $_SESSION['DE_Money_In']; $SE_Money_Out = $_SESSION['DE_Money_Out']; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Bank Home 2</title> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <META http-equiv="imagetoolbar" CONTENT="no"> <link rel="stylesheet" href="stylesheets/Library.css" type="text/css"> <link rel="stylesheet" href="stylesheets/LibraryMenu.css" type="text/css"> </head> <body> <div id="PageTitle"> <img src="images/LibraryBanner.jpg" alt=""> </div> <?php include('SideMenu.php'); ?> <div id="PageContent"> <form action="DataInputUpdate.php" method="post"> <h3>User: <?=greeting() . " Ends on " . $EndTime ?></h3> <div style="width:650px;height:60px;padding:10px;overflow:hidden;"> <div style="width:440px;float:left;"> Date: Carried forward: £<span style="color:red;"><?=$_SESSION['Error_3'] ?></span> <input type="text" maxlength="10" size="5" name="BF" style="text-align:right" value="<?=$_SESSION['C_Forward'] ?>" > <select name="DD_Balance" style="width:40px"> <?php for($days=1;$days<32;$days++) { ?> <option value="<?=$days ?>" <?php if ($_SESSION["DD_Balance"] == $days){ ?> SELECTED <?php } ?>><?=$days ?></option> <?php } ?> </select> <select name="MM_Balance" style="width:60px"> <option value="1" <?php if ($_SESSION["MM_Balance"] == "1"){ ?> SELECTED <?php } ?>>Jan</option> <option value="2" <?php if ($_SESSION["MM_Balance"] == "2"){ ?> SELECTED <?php } ?>>Feb</option> <option value="3" <?php if ($_SESSION["MM_Balance"] == "3"){ ?> SELECTED <?php } ?>>Mar</option> <option value="4" <?php if ($_SESSION["MM_Balance"] == "4"){ ?> SELECTED <?php } ?>>Apr</option> <option value="5" <?php if ($_SESSION["MM_Balance"] == "5"){ ?> SELECTED <?php } ?>>May</option> <option value="6" <?php if ($_SESSION["MM_Balance"] == "6"){ ?> SELECTED <?php } ?>>Jun</option> <option value="7" <?php if ($_SESSION["MM_Balance"] == "7"){ ?> SELECTED <?php } ?>>July</option> <option value="8" <?php if ($_SESSION["MM_Balance"] == "8"){ ?> SELECTED <?php } ?>>Aug</option> <option value="9" <?php if ($_SESSION["MM_Balance"] == "9"){ ?> SELECTED <?php } ?>>Sep</option> <option value="10" <?php if ($_SESSION["MM_Balance"] == "10"){ ?> SELECTED <?php } ?>>Oct</option> <option value="11" <?php if ($_SESSION["MM_Balance"] == "11"){ ?> SELECTED <?php } ?>>Nov</option> <option value="12" <?php if ($_SESSION["MM_Balance"] == "12"){ ?> SELECTED <?php } ?>>Dec</option> </select> <select name="YYYY_Balance" style="width:60px"> <?php if($MM == 1) { ?> <option value="<?=$PreviouseYear ?>" <?php if($_SESSION["YYYY_Balance"] == $PreviouseYear){ ?> SELECTED <?php } ?> ><?=$PreviouseYear ?></option> <?php } ?> <option value="<?=$ThisYear ?>" <?php if($_SESSION["YYYY_Balance"] == $ThisYear){ ?> SELECTED <?php } ?> ><?=$ThisYear ?></option> <?php if($MM == 12 || $MM == 11 && $DD > 19) { ?> <option value="<?=$NextYear ?>" <?php if($_SESSION["YYYY_Balance"] == $NextYear){ ?> SELECTED <?php } ?> ><?=$NextYear ?></option> <?php } ?> </select> </div> <br><br> <div style="float:left;clear:both;"><input class="MyButton"type="submit" name="direction" value="Prev Balance"></div> </div> <div style="width:650px;height:10px;padding:10px;overflow: hidden;"> <select name="DD_Entry" style="width:40px"> <?php for($days=1;$days<32;$days++) { ?> <option value="<?=$days ?>" <?php if ($_SESSION["DE_DD_Entry"] == $days){ ?> SELECTED <?php }?>><?=$days ?></option> <?php } ?> </select> <select name="MM_Entry" style="width:60px"> <option value="1" <?php if ($_SESSION["DE_MM_Entry"] == "1"){ ?> SELECTED <?php } ?>>Jan</option> <option value="2" <?php if ($_SESSION["DE_MM_Entry"] == "2"){ ?> SELECTED <?php } ?>>Feb</option> <option value="3" <?php if ($_SESSION["DE_MM_Entry"] == "3"){ ?> SELECTED <?php } ?>>Mar</option> <option value="4" <?php if ($_SESSION["DE_MM_Entry"] == "4"){ ?> SELECTED <?php } ?>>Apr</option> <option value="5" <?php if ($_SESSION["DE_MM_Entry"] == "5"){ ?> SELECTED <?php } ?>>May</option> <option value="6" <?php if ($_SESSION["DE_MM_Entry"] == "6"){ ?> SELECTED <?php } ?>>Jun</option> <option value="7" <?php if ($_SESSION["DE_MM_Entry"] == "7"){ ?> SELECTED <?php } ?>>July</option> <option value="8" <?php if ($_SESSION["DE_MM_Entry"] == "8"){ ?> SELECTED <?php } ?>>Aug</option> <option value="9" <?php if ($_SESSION["DE_MM_Entry"] == "9"){ ?> SELECTED <?php } ?>>Sep</option> <option value="10" <?php if ($_SESSION["DE_MM_Entry"] == "10"){ ?> SELECTED <?php } ?>>Oct</option> <option value="11" <?php if ($_SESSION["DE_MM_Entry"] == "11"){ ?> SELECTED <?php } ?>>Nov</option> <option value="12" <?php if ($_SESSION["DE_MM_Entry"] == "12"){ ?> SELECTED <?php } ?>>Dec</option> </select> <select name="YYYY_Entry" style="width:60px"> <?php if($MM == '1' && $DD < 7) { ?> <option value="<?=$PreviouseYear ?>" <?php if ($_SESSION["DE_YYYY_Entry"] == $PreviouseYear){ ?> SELECTED <?php } ?> ><?=$PreviouseYear ?></option> <?php } ?> <option value="<?=$ThisYear ?>" <?php if ($_SESSION["DE_YYYY_Entry"] == $ThisYear){ ?> SELECTED <?php } ?> ><?=$ThisYear ?></option> <?php if($MM > 10) { ?> <option value="<?=$NextYear ?>" <?php if ($_SESSION["DE_YYYY_Entry"] == $NextYear){ ?> SELECTED <?php } ?> ><?=$NextYear ?></option> <?php } ?> </select> <span style="color:red;"><?=$_SESSION['Error_5'] ?></span> </div> <div style="width:650px;height:60px;padding:10px;overflow: hidden;"> Money in: £ <input style="text-align:right;" type="text" maxlength="10" size="4" name="MoneyIn" value="<?=$SE_Money_In ?>" size="8"> <!-- <span style="color:red;"><?=$_SESSION['Error_6'] ?></span> --> <br><br> Money Out: £ <input style="text-align:right;" type="text" maxlength="10" size="4" name="MoneyOut" value="<?=$SE_Money_Out ?>" size="8"> <!-- <span style="color:red;"><?=$_SESSION['Error_7'] ?></span> --> </div> <div style="width:650px;height:90px;padding:10px;overflow:hidden;"> Reason: <?php $qReason = GetReason(); ?> <select name="Reason"> <?php while($row = $qReason->fetch()) { ?> <option value="<?=$row['ReasonID'] ?>" <?php if($SE_MyReason == $row['ReasonID']) { ?> selected <?php } ?> ><?=$row['Reason'] ?></option> <?php } ?> </select> <span style="color:red;"><?=$_SESSION['Error_8'] ?><br><br></span> Max 32 Characters<br> Comment <input type="text" maxlength="32" size="42" name= "<?=$Comment_Name ?>" value="<?=$_SESSION["DE_Comment"] ?>"><br><br> </div> <?php if($_SESSION["DE_Retain"] == 1) { ?> Retain comment: <input type="checkbox" maxlength="1" size="1" name="Retain" checked> <?php } else { ?> Retain comment: <input type="checkbox" maxlength="1" size="1" name="Retain"> <?php } ?> <div style="width:650px;height:40px;padding:10px;overflow:hidden;"> Hi-light <input type="checkbox" maxlength="1" size="1" name="Bold"> </div> <div style="width:460px;height:90px;padding:10px;overflow:hidden;"> <input class="MyButton"type="submit" name="direction" value="Add record"><br><br><br> <input class="MyButton"type="submit" name="direction" value="Statement"> </div> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650512 Share on other sites More sharing options...
mac_gyver Posted February 26 Share Posted February 26 if this is the only session variable you are getting an error for (i didn't get an error for this variable, but did for some other ones when i ran your code), here are some possibilities - your actual code has some non-printing characters in or smart/curly-quotes around the index name (that posting code on this forum filtered out). i would delete and retype the entire index name, including the initial and final double-quotes, in each reference to this array index name. is any of the other code that gets executed in the functions being called, referencing or setting that variable and could be unsetting it? are you sure that the latest code got saved/uploaded so that you are actually initializing that variable? most of these session variables exist solely to pass data from the form processing code back to the form. you should instead put the form processing code and the form on the same page. this will greatly simplify all the code. the code for any page should be laid out in this general order - initialization post method form processing get method business logic - get/produce data needed to display the page html document at the completion of the post method form processing code, you should preform a redirect to the exact same URL of the current page to cause a get request for that page. this will prevent the browser from trying to resubmit the form data should that page get browsed back to or reloaded. you should not copy variables to other variables for nothing. just use the original variables that data is in. in the current code, a significant number of lines are there only to copy variables back and forth, yet you have instances of using the original variable that data is in. you should apply htmlentities() to any dynamic value being output in a html context, right before/as it is being output, to prevent any html entity in value from breaking the html syntax. 1 Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650522 Share on other sites More sharing options...
Paul-D Posted February 26 Author Share Posted February 26 Seems that my host server has been cashing on me again. after a forced update ctrl+F5 it is now sorted. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650523 Share on other sites More sharing options...
Paul-D Posted February 27 Author Share Posted February 27 (edited) Sorry but the last post was wrong. I am still getting the werror message on the page. Can someone see where I am loosing a SESSION variable that was established in function Session_Init() inSecureFunctions.php at the start of the page code? http://desmond-otoole.co.uk/SessionVariable.jpg Edited February 27 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650547 Share on other sites More sharing options...
mac_gyver Posted February 27 Share Posted February 27 (edited) 5 hours ago, Paul-D said: Can someone see ... have you done or checked the 3 possible things that were already posted above? are you actually including SecureFunctions.php? also, do you have multiple sets of these files in different paths and there is more than one SecureFunctions.php and the wrong one got updated? Edited February 27 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650558 Share on other sites More sharing options...
gizmola Posted February 28 Share Posted February 28 I feel like I have advised this before, but change all your include_once() calls to require_once(). Is the code you provided above the code for the DataInput.php script? As @mac_gyver stated: you have assumptions as to what loads or doesn't, that we have no way of validating. Quote Link to comment https://forums.phpfreaks.com/topic/326862-syntax-error-unexpected-token-else/#findComment-1650583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.