0

I asked a question recently about sending a POST variable off of jquery to another php page, redirecting to that page, doing some action with the sent information, and redirecting off of the original page.

jQuery post method and directory confusion

I am asking for help on almost the exact same code, since the person who commented on it only answered half of my question and marked it as a duplicate. While it did help, it did not solve the problem.

I have made edits according what was suggested in the previous attempt to get this problem solved. As you will see, hitting the save button in this example will bring you to changePage.inc.php, the first redirect, but the second redirect does not happen, which should bring you to secondPage.php.

File named index.php

<?php
    session_start();
?>

<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
    crossorigin="anonymous">
</script>
<script>
    $(document).ready(function(){
        $('#saveForm').submit(function(event){
            event.preventDefault();
            var message = "";

            var changeText = function(){
                message = "You wrote: " + $('#text').val();
            };

            var send = function(){
                if(message !== ""){
                    $.post("includes/changePage.inc.php", {message: message});
                    window.location.href = "includes/changePage.inc.php";
                }
            };

            changeText();
            send();
        });
    });
</script>
<body>
<textArea id="text"></textArea>
<form id="saveForm" action="includes/saveEssay.inc.php" method="POST">
  <button type="submit" id="saveButton">Save!</button>    
</form>
</body>
</html>

File named changePage.inc.php (within a folder named includes)

<?php

session_start();

if(isset($_POST['message'])){
    header("Location: ../secondPage.php");
}

exit();

File named secondPage.php

<?php
    echo 'Hello World';
?>
je123
  • 19
  • 6
  • Where is the code for changePage.inc.php? – Jay Blanchard Oct 16 '17 at 20:11
  • My apologies, should be changePage.inc.php instead of changeDirectory.inc.php. I will make the edit. – je123 Oct 16 '17 at 20:13
  • May I ask, why do you want to "format" the information in any way before submitting? You can do this in the target page, e.g. `changePage.inc.php`. That way you can totally avoid the jquery part. –  Oct 16 '17 at 20:27
  • And is `secondPage.php` the same with `includes/saveEssay.inc.php`? You haven't mention nothing about the `includes/saveEssay.inc.php`anywhere. –  Oct 16 '17 at 20:30
  • Your explanations in both questions are confusing, because you are referring yourself to your own solution, which seems to be other than a smooth one. So, if you want a solid solution, please take my advice: forget jquery, forget php. Just describe in words your exact problem and the workflow you want to have done. But in your description use page names as much as possible, not something like `the page from the php file within the includes folder, so I do not want to use jquery to redirect`. Thanks. –  Oct 16 '17 at 20:43
  • I know it's not pretty to must reediting your question ;-) But, regarding time, is a win-win situation. For you and for us. –  Oct 16 '17 at 20:46
  • aendeerei! thank you very much for your responses. I have tried many times before to write things out in words and was discouraged by other users to do so, with comments like not minimal, complete, or verifiable. That is why I have tried to satisfy those conditions as much as possible this time. – je123 Oct 16 '17 at 20:59
  • That being said, if you check my comments in response to the answer provided by B. Fleming, I have tried to describe my project in words. I hope this helps, thanks again! – je123 Oct 16 '17 at 21:00

1 Answers1

1

The jQuery $.post() method is what is referred to as an asynchronous call. What this means is that while $.post() is trying to execute, any line of code after it will try to execute.

Note that you're redirecting immediately after your $.post(). This means that while $.post() is trying to run, you're redirecting to includes/changePage.inc.php, which is probably preventing the PHP redirect to ../secondPage.php from running. This is what we call a race condition, where two events are running simultaneously and either one could finish before the other with no way to keep the events synchronized.

What you're really trying to do, from what I can tell, is hand off processing of your data to PHP, then upon finishing, you want to redirect to a given page.

If I'm correct about this, then remove the window.location.href = "includes/changePage.inc.php"; line because otherwise you're always going to run into issues with this race condition. Handle the redirect in PHP--it should work on its own. If you're having trouble with redirecting directly in PHP, then you can redirect through jQuery:

Your jQuery POST

$.post("includes/changePage.inc.php", {message: message}, function(data) {
    var obj = $.parseJSON(data);
    window.location.href = obj.redirect_url;
});

What to return via PHP

echo json_encode(array('redirect_url'=>'includes/secondPage.php'));

If this doesn't meet your needs, then you'll need to really sit down and better explain your use case and your intent. If you can't explain it well enough to communicate exactly what you want, then you probably don't have a complete grasp of the problem you need to solve.

B. Fleming
  • 7,170
  • 1
  • 18
  • 36