Reputation: 67
I've been trying to figure this out for a while now.
I'm using jquery ajax post to send form data using session variables on home.php to thankyou.php and then redirect the user (window.location.href='') to thankyou.php with the data from the form on home.php printed out on thankyou.php.
Also, the data that gets carried over to thankyou.php is emailed to the user.
I'm trying to do it this way so that the user will not see the query string at the end of thankyou.php.
Every time i test it out my session data doesn't carry over but when the email is sent, it has all the data.
Can someone tell me if im doing something wrong? Can jQuery pass session data to a php page?
Here is my code:
home.php
<?php
session_start();
?>
<script>
var formData = $('form').serialize();
$.post('thankyou.php', formData, function(){
$('form').submit();
window.location.href = '/thankyou.php;
});
</script>
thankyou.php
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['phone'];
?>
Upvotes: 0
Views: 2272
Reputation: 176
It is nothing wrong with your session. You have just overwritten your session variables with POST variables. When your form is submitted, your variables are not sent correctly. Verify if you put method="POST" in your form.
Upvotes: 0
Reputation: 569
$('form').submit();
- first call, stores you $_POST variables to $_SESSION
window.location.href = '/thankyou.php;
- second call, replaces $_SESSION variables with undefined $_POST
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
try something like this:
if(isset($_POST) && $_POST) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
}
echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['phone'];
Upvotes: 1