I have a form where the user inputs data and submits. This form does not refresh when the user submits and uses Ajax to relay the information to PHP. Upon submission, the user goes to a new page (in reality it's the same page, but the user views it as another). The user can go back to the previous page to change some information if they want. When the user first submits the page, I am having no problem with it. It's when the user goes back and decides to change again is where I'm having trouble. With my current code, I initiate an empty SESSION["prevUrl"] upon refresh. Afterwards, when the user submits the form, the SESSION["prevUrl"] gets assigned the new POST data. Let's say the user decides to submit again, the if/else statement I have should go to the else statement as the SESSION variable is not empty, but instead it decides to invoke the if block. I have used INSERT statements for both if and else block to see which is being invoked, and at both time the if statement is being invoked. Here's my code:
session_start();
ob_start();
$_SESSION["prevUrl"] = "";
if (isset($_POST["articleType"], $_POST["articleTitle"], $_POST["articleUrl"], $_POST["numberOfPages"], $_POST["typeOfArticle"]))
{
$_SESSION["articleType"] = $_POST["articleType"];
$_SESSION["articleTitle"] = filter_data($_POST["articleTitle"]);
$_SESSION["articleUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
if (empty($_SESSION["prevUrl"]))
{
$db->query("INSERT INTO Stories (`url`) VALUES ('hi')");
$_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}
else
{
$db->query("INSERT INTO Stories (`url`) VALUES ('bye')");
$db->query("UPDATE Stories SET url = '{$_POST['articleUrl']}' WHERE url = '{$_SESSION['prevUrl']}'");
$_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}
$_SESSION["numberOfPages"] = $_POST["numberOfPages"];
$_SESSION["typeOfArticle"] = $_POST["typeOfArticle"];
}
How can I fix it so that the second time around, the else block is invoked? And why is it that the second time around, my SESSION["prevUrl"] is considered empty?