3

//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.

Mohan Perera
3701 gold badge4 silver badges15 bronze badges
asked Apr 1, 2015 at 7:03
2
  • 1
    Session variable already available at all page Just start session at every page. Commented Apr 1, 2015 at 7:04
  • @SunilPachlangia :- if other than session variable. see my edited question once again. Commented Apr 1, 2015 at 7:06

5 Answers 5

3

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)
 }
 });
 });
});
answered Apr 1, 2015 at 7:06
Sign up to request clarification or add additional context in comments.

2 Comments

should use var sessionId = <?php echo json_encode($_SESSION['id']); ?>;
For 2 reasons: 1. automatic type declaration (string with ", int without, array with [] etc.) 2. for automatic escaping
1

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

answered Apr 1, 2015 at 7:15

3 Comments

when you use input as hidden then why assign value to attribute ? you can assign data to value
what if he wants to put it in a button?, i did that for additional info, i know the OP knows that he can put it in value, its a common knowledge
well then, i can change my example
1

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.

answered Apr 1, 2015 at 7:27

Comments

1

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.

answered Apr 1, 2015 at 8:21

Comments

1

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 :-)

answered Apr 2, 2015 at 16:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.