0

Generally, when I want to change a page, I'll use a href to do so, or I'll use the header function.

For my current project, I need to use jquery to receive and modify the information that the client has entered, and I need to then use the post method to have php be able to access said information.

Upon receiving the information, I want a separate php file in an includes folder to do something with it, and then redirect the user away from the original page.

When working on the project, the page does not change, so I am wondering if the information is even reaches the includes folder.

Below you will find a miniature example of code that replicates the issue described above.

After the save button is pressed, I want the jquery to create $_POST['message'], check if it is set, send it to the changePage.inc.php page, check again if it is set, then redirect the user to the secondPage.php page which should display "Hello World".

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});
                    $.post("index.php", {message: message});
                }
            };

            changeText();
            send();
        });
    });
</script>
<?php
    if(isset($_POST['message'])){
        header("Location: includes.changePage.inc.php");
    }
?>
<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 changeDirectory.inc.php (within a folder named includes)

<?php

session_start();

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

exit();

File named secondPage.php

echo 'Hello World';

The first comment actually does not solve my problem!

I need to change the page from the php file within the includes folder, so I do not want to use jquery to redirect!

jquery is necessary here to format the text and have it be accessible as a post variable, and, once it is accessible as a post variable, I want the changePage file to be run.

I cannot stress this enough! The redirect must be done within the changePage.inc.php file

je123
  • 19
  • 6
  • Since you're using jQuery just perform the redirect using jQuery. – Jay Blanchard Oct 16 '17 at 18:24
  • Hi, thank you very much for your response! Can you check the edit that I've made with the bold text that I added to the very bottom. I'm just very curious as to why my initial approach was not working. – je123 Oct 16 '17 at 18:32
  • Ignore my above comment! For a quick moment you tricked me into thinking that my question was a duplicate of the question that you suggested! But it is not, I do not want to use jquery to redirect to the next page!!! – je123 Oct 16 '17 at 18:38
  • The changePage.inc.php needs access to the POST variable, for my specific project, I am saving the POST variable to a database in this step. I want the redirect to happen after the jquery has formatted the text, sent it as a POST variable to changePage.inc.php, and lastly saved to the database. – je123 Oct 16 '17 at 18:44
  • Jay, even when using jquery to redirect to changePage.inc.php, the second redirect from changePage.inc.php to secondPage.php does not happen. The second redirect should happen when isset($_POST['message']) is true, since that condition is within an if statement. – je123 Oct 16 '17 at 19:02
  • In short, your answer solves half of the problem that I am having. – je123 Oct 16 '17 at 19:02
  • I will ask this question again, adding in the small change from jay so that it cannot be ignored as a duplicate, if not answered in the next hour or so. – je123 Oct 16 '17 at 19:15
  • If you never go to the changePage.inc.php you cannot redirect from there. You would do a JavaScript redirect in the callback of the AJAX function. – Jay Blanchard Oct 16 '17 at 19:33
  • You have already addressed that, and I have made the proper edits to make sure that happens. What I'm saying is that now, it reaches changePage.inc.php, and it is still not working. – je123 Oct 16 '17 at 19:47
  • I will provide a link for the next attempt to ask the question, with the edited code, which uses window.location.href as you have suggested. – je123 Oct 16 '17 at 19:48
  • https://stackoverflow.com/questions/46778066/post-variable-from-jquery-redirecting-but-is-not-set – je123 Oct 16 '17 at 19:53

0 Answers0