Jump to content

Including php file instead of load()

Recommended Posts

Hello,I'm using this script right now:mainpage.php

<?php$mainpage = 'secret';?><a href="#" id="renamegroups">Rename groups</a><div id='renamegroupsl'></div>  <script type="text/javascript">$('#renamegroups').click(function(){   $("#renamegroupsl").load("../information/renamegroups.php");   return false;});</script>


<?phpecho $mainpage;?>

The problem is that I get this error:Notice: Undefined variable: mainpage in (...)So how could I include a file with ajax / jquery (because if I include the file this will work and the error won't show up) Thanks!

Link to post
Share on other sites

because by the time jquery makes the load call, the PHP in mainpage has already executed and $mainpage is longer defined (and doesn't exist within the context of mainpage.php anymore). you have to realize that PHP and Javascript are server side and client side, respectively. Server side code runs first, then client side code. if you could explain the intent of your functionality, then we can probably suggest a for you to do what you are looking for.

Edited by thescientist
Link to post
Share on other sites

to send the value to renamegroups you would use

 $("#renamegroupsl").load("renamegroups.php?mainpage=<?php echo $mainpage; ?>");

then in renamegroups.php read the GET value

<?php$mainpage="";if(isset($_GET['mainpage'])){$mainpage=$_GET['mainpage'];}echo $mainpage;?>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...