Jithin Varghese
Jithin Varghese

Reputation: 2228

PHP code inside jquery not working

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

Upvotes: 3

Views: 1399

Answers (5)

jithinvarghese111
jithinvarghese111

Reputation: 64

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

Upvotes: 1

Jithin Varghese
Jithin Varghese

Reputation: 2228

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.

Upvotes: 1

Akhil.p.p
Akhil.p.p

Reputation: 11

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.

Upvotes: 1

Ceeee
Ceeee

Reputation: 1442

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

Upvotes: 1

Nishit Maheta
Nishit Maheta

Reputation: 6031

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)
      }
   });
  });
});

Upvotes: 3

Related Questions