StevenOliver Posted March 6, 2019 Share Posted March 6, 2019 (edited) If order is finalized, I want Customer to start over from the "very beginning" if they want to create another order. The "very beginning" is the page that has the Submit Button. Without exception, all of these versions work. Which one is the best? (and which one is the worst? ?) 1.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } if(isset($_SESSION["order_is_finalized"])){ include('order_page.php'); exit; } -vs- 2.) if(isset($_SESSION["order_is_finalized"])){ if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } else { include('order_page.php'); exit; } } -vs- 3.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } elseif (isset($_SESSION["order_is_finalized"])) { include('order_page.php'); exit; } -vs- 4.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } else { if (isset($_SESSION["order_is_finalized"])) { include('order_page.php'); exit; } } Thank you!! Edited March 6, 2019 by StevenOliver Quote Link to comment https://forums.phpfreaks.com/topic/308431-else-vs-two-if-statements/ Share on other sites More sharing options...
NotionCommotion Posted March 6, 2019 Share Posted March 6, 2019 What is best is based on what you want your script to do. That being said, I would probably start with the session conditional first. if(isset($_SESSION["order_is_finalized"])){ if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } else { include('order_page.php'); exit; } } Quote Link to comment https://forums.phpfreaks.com/topic/308431-else-vs-two-if-statements/#findComment-1565057 Share on other sites More sharing options...
StevenOliver Posted March 6, 2019 Author Share Posted March 6, 2019 6 minutes ago, NotionCommotion said: What is best is based on what you want your script to do. That being said, I would probably start with the session conditional first. Thank you!! Sometimes it's hard to articulate exactly what I want... but here goes: If the order is finalized, I don't want customer hitting back button to create a new order or modify their order. Rather, I want them to start fresh from the beginning. The way I know that "order is finalized" I create $_SESSION["order_is_finalized"] and set it to TRUE. The way I know that Customer is "starting fresh from the beginning" is if they click the initial Start Button ( $_POST["SubmitButton"] ). Since all 4 versions work, I can't seem to figure out which is the most efficient. They all seem to work equally well. Unless you have a better idea, I'll use the "session conditional" version that you suggested. Quote Link to comment https://forums.phpfreaks.com/topic/308431-else-vs-two-if-statements/#findComment-1565058 Share on other sites More sharing options...
NotionCommotion Posted March 6, 2019 Share Posted March 6, 2019 Your not going to get the results you are looking for. The server will not know whether the user submitted the form using the submit button or clicked the back button which caused it to be submitted. There are most likely better ways to do this, but something like the following might work. <?php session_start(); function display($counter) { $_SESSION["counter"]=$_SESSION["counter"]+1; //create your page and include $counter in a hidden input field. } if(!isset($_SESSION["counter"])) { $_SESSION["counter"]=0; } if(isset($_POST["SubmitButton"])) { if(isset($_POST["counter"])) { if($_POST["counter"]==$_SESSION["counter"]) { //Process your order } else { display(); } } else { echo('missing counter error'); } } else { display(); } Quote Link to comment https://forums.phpfreaks.com/topic/308431-else-vs-two-if-statements/#findComment-1565062 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.