admin管理员组

文章数量:1022443

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

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

Share Improve this question edited Mar 30, 2018 at 7:30 Mohan Perera 3701 gold badge4 silver badges15 bronze badges asked Apr 1, 2015 at 7:03 Jithin VargheseJithin Varghese 2,2484 gold badges33 silver badges61 bronze badges 2
  • 1 Session variable already available at all page Just start session at every page. – Sunil Pachlangia Commented Apr 1, 2015 at 7:04
  • @SunilPachlangia :- if other than session variable. see my edited question once again. – Jithin Varghese Commented Apr 1, 2015 at 7:06
Add a ment  | 

5 Answers 5

Reset to default 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)
      }
   });
  });
});

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

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.

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.

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

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

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

Share Improve this question edited Mar 30, 2018 at 7:30 Mohan Perera 3701 gold badge4 silver badges15 bronze badges asked Apr 1, 2015 at 7:03 Jithin VargheseJithin Varghese 2,2484 gold badges33 silver badges61 bronze badges 2
  • 1 Session variable already available at all page Just start session at every page. – Sunil Pachlangia Commented Apr 1, 2015 at 7:04
  • @SunilPachlangia :- if other than session variable. see my edited question once again. – Jithin Varghese Commented Apr 1, 2015 at 7:06
Add a ment  | 

5 Answers 5

Reset to default 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)
      }
   });
  });
});

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

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.

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.

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

本文标签: javascriptPHP code inside jquery not workingStack Overflow