//My jquery
$(document).ready(function() {
var form = $('#form1'); // contact form
var submit = $('#submit1'); // submit button
var alert = $('.alert1'); // alert div for show alert message
// form submit event
form.on('submit', function(e) {
e.preventDefault(); // prevent default form submit
// sending ajax request through jQuery
$.ajax({
url: 'giftcard_check.php', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Checking....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Apply'); // reset submit button text
var $container = $("#result1");
var refreshId = setInterval(function()
{
$container.load("result.php?code=<?php echo $variable; ?>");
}, 500);
},
error: function(e) {
console.log(e)
}
});
});
});
The above code is not working while using php code inside jquery. If iam not using php code its working fine. But i want to send session variables to another page (result.php). How can i solve this. Is there any method.
-
1Session variable already available at all page Just start session at every page.Sunil Pachlangia– Sunil Pachlangia2015年04月01日 07:04:39 +00:00Commented Apr 1, 2015 at 7:04
-
@SunilPachlangia :- if other than session variable. see my edited question once again.Jithin Varghese– Jithin Varghese2015年04月01日 07:06:31 +00:00Commented Apr 1, 2015 at 7:06
5 Answers 5
use below code . assing php session to javascript variable. make sure this code is in side PHP file . php will not work inside .js file
var sessionID = "<?php echo $_SESSION['id']; ?>";
$(document).ready(function() {
var form = $('#form1'); // contact form
var submit = $('#submit1'); // submit button
var alert = $('.alert1'); // alert div for show alert message
form.on('submit', function(e) {
e.preventDefault(); // prevent default form submit
// sending ajax request through jQuery
$.ajax({
url: 'giftcard_check.php', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Checking....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Apply'); // reset submit button text
var $container = $("#result1");
var refreshId = setInterval(function()
{
$container.load("result.php?code="+sessionID);
}, 500);
},
error: function(e) {
console.log(e)
}
});
});
});
lets look on a different angle
you can do on your html something like this:
<form>
<input type="submit" id="f_the_world" data-session-id="<?php echo $variable; ?>"/>
</form>
then on your JS
$(document).ready(function() {
var form = $('#form1'); // contact form
var submit = $('#submit1'); // submit button
var alert = $('.alert1'); // alert div for show alert message
// form submit event
form.on('submit', function(e) {
e.preventDefault(); // prevent default form submit
// sending ajax request through jQuery
$.ajax({
url: 'giftcard_check.php', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Checking....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Apply'); // reset submit button text
var $container = $("#result1");
var refreshId = setInterval(function()
{
var code = $('#f_the_world').attr('data-session-id');
$container.load("result.php?code=".code);
}, 500);
},
error: function(e) {
console.log(e)
}
});
});
});
its just dont feel right seeing server scripts on client scripts
3 Comments
why are you sending the session id to the next page...session values are stored in server. You can access session values from any page.
Comments
we can easily get the session variable in result.php by adding session_start(); at the begining of result.php. So that we can have access to the session variable created.
Comments
First Step: In your jQuery code written page just start the session variable $_SESSION['id'].
Second Step: In your result.php page, write session_start(); at the beginning. Then just call the $_SESSION['id'].
Hope this will help :-)