简体   繁体   中英

Passing jQuery paramenter between pages

I am trying to pass variables between php pages with jQuery.

My code: I am using this on fullcalendar.js API:

First page: EventMain.php

select: function (start, end) {

   $.ajax({
      url: '/dev/Event/Event.php',
      type: 'POST',
      data: {name: 'John'}
   });

  PopupCenter(url,'Event', 1000, 450)
}

Then the pop open with a second Page: Event.php. I need to get the name variable

var Name = <? php echo $_POST['name']; ?>;

alert(Name);

The alert is blank.. I don't understand why?

I know I can send the variables on the url as parameters and then on Event.php use getUrlParameter to get the variables:

function getUrlParameter(sParam){
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}

But I don't want to send the variables on the url. Is there any other way to send the variable?

You can't access POST variables in another session.

The popup you open is a new request to a page that doesn't have the data.

There is no reasonable solution to what you are trying to achieve.

Pass the variable from EventMain.php to Event.php

$.ajax({
         type: 'post',
         url : '/dev/Event/Event.php',
         data: { name : 'John'},
         success: function(response) {   // return content from Event.php (result) page
                // Do your operation with result
          }
  });

In Event.php

<?php 
$name = $_POST['name'];
// Perform your actions with $name and return result as
echo $yourresult;
exit;
?>

Try this way ...

eventMain.php

$.post('/dev/Event/Event.php',{'name':'John'}, function(data){
   // do something with response 
});

Event.php

<?php
   $name = $_POST['name'];
 ?>

<script>
   console.log('<?= $name; ?>');
</script>

I think you forgot the "" when assign a value to variable name in the second page.

First Page:

<!-- you don't forget this tag to see the result //-->
<div id="content"></div>

$.ajax({
    url: 'page.php',
    type: 'POST',
    data: {name: 'John'},
    success: function (data) {
        $('#content').html(data);
    }
});

Second Page (event.php):

<script type="text/javascript">
    var name = "<?php echo $_POST['name']; ?>";
    alert(name);
</script>

The previous code work for me, I've tested it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM