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