0

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?

3
  • You empty $_SESSION['prevUrl'] manually every time (row 3). How could it be anything else but empty? Commented Jul 31, 2016 at 21:36
  • @lserni I want to empty it when the user first comes on to the page. The first time the user submits the form, $_SESSION['prevUrl'] gets set to $_POST["articleUrl"] and I've tested this and found that $_SESSION['prevUrl'] is not empty anymore once the user submits, but PHP is viewing it as empty Commented Jul 31, 2016 at 21:39
  • @lserni Would it be a good idea to make the user redirect to a certain page that will empty specific SESSION variables and then that page will redirect back to the original page? Commented Jul 31, 2016 at 22:16

2 Answers 2

1

I would do the following:

  1. Reset everything there is no $_POST variable, because it means that the page was freshly loaded
  2. Add a flag to session that is set to true on the first submit.
  3. Look for that flag each time. If it is unset, meaning the form hasn't been submitted since the last reload, empty the prevUrl

Putting these steps together:

session_start();
ob_start();

//reset all session vars when page is freshly loaded
if(empty($_POST)) session_unset(); 

//clear prevURL only on first submission after a page load
if(empty($_SESSION['inserted'])  $_SESSION["prevUrl"] = "";

...
if (empty($_SESSION["prevUrl"]))
{       
    $result = $db->query("INSERT INTO Stories (`url`) VALUES ('hi')");
    if(!$result) die($db->error);
    $_SESSION['inserted'] = true;
    $_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}

else...
Sign up to request clarification or add additional context in comments.

17 Comments

With this, on the first attempt $_SESSION['submitted'] will be empty, and it will work properly. Afterwards, $_SESSION['submitted'] will carry the value true. When the user refreshes the page, $_SESSION['submitted'] will still be true and therefore will not be empty. I need everything to reset upon entering the page
@user2896120 Have you tried it or are you judging results just by reading the answer? the point is to only insert once right? On subsequent requests, you want to update instead? That's what my code accomplishes. The flag (I renamed it from submitted to inserted) is just a way to check whether the record should be inserted or updated. If everything is reset as you suggest, the code will always follow the same path; no matter what you do you will be unable to get in the else branch
Yes the point is to only insert once, but if the person refreshes the page they are required to insert again. The SESSION variables should be emptied out as though the user is first visiting the page. Think of this as a CMS, if the user refreshes the page then the data should be gone and they are writing a new article.
@user2896120 "the point is to insert once" and "they are required to insert again" are contradictory. In which case do you want update to run?
The user will enter the page. They are going to build the article they want. They will enter the URL, title, etc. These details are not added into the table till the user goes to the next page of the current page and actually submits a page for their article. If the user decides to go back to the previous page, then we need to check if the user already submitted a page with the current SESSION if they decide to update the URL. If there is a current SESSION then the URL will be updated based on the prevUrl of what we had when we first submitted. If at any point the user decides to refresh,
|
0

because in the beginning of your code you wrote $_SESSION["prevUrl"] = "";(line 3), so, everytime the page executes the SESSION['prevUrl'] goes empty. To solve this, put it as:

if(!isset($_SESSION['prevUrl'])){
    $_SESSION['prevUrl'] = "";
}

3 Comments

Should it be just isset and not a "!" ? Since, I want to empty $_SESSION['prevUrl'] when the user first enters the page?
with "!" will be when it is not setted. It will happen when the user first enters in the page in that session.
Let's say my $_SESSION['prevUrl'] was empty then this code will be invoked and set it to empty. I then proceed to submit the form, $_SESSION['prevUrl'] is then set to whatever $_POST is. I refresh the page, $_SESSION['prevUrl'] is still whatever $_POST is and when it comes to your code, it'll skip it because it's already been set. It will then proceed to go to the else statement when I first hit submit. Now it will UPDATE what $_SESSION['prevUrl'] was before I refreshed the page, instead of inserting a new entry for this URL. If that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.