Jump to content

ProcessWire and JQuery AJAX


nickmccomb
 Share

Recommended Posts

Hi guys,

i've been trying to fix an issue all day and can't figure it out!

i have a php page that calls another php page through AJAX.  The problem is that the session variables i set in the page i call through AJAX don't stick when i go back to the calling page.

I believe it may be because of the way i'm calling the page url with AJAX, which could be what makes another session once it gets to the AJAX page  

     url:"<?php echo $config->urls->templates?>logincode.php/",

the session ID's are completely different on both pages and the name of the session from the calling page is 'wire'.  The name of the session on the AJAX page is called 'PHPSESSID'.   So the sessions are not the same, which is why the session variables set in the AJAX page aren't available in the calling page when the AJAX page is returned to the calling page.  

i've tried renaming the ProcessWire session name in the config file to 'PHPSESSID', although that didn't work.  

When i go to the page 'logincode/' as a regular post from a form it works perfectly and has the same session ID and variables, although through AJAX the sessions on both pages are completely different.  

Does anyone know how to do this properly and make the session the same?  Please help, it's driving me mad!

see the AJAX calling code below:

$.ajax({ 
     type : 'POST',
     xhrFields: {withCredentials: true},
     data : {'username':username, 'password':password},
     url:"<?php echo $config->urls->templates?>logincode.php/",
     success:function(result){  
          if(result == "1"){
               alert('Please input username and password');
          }
     }, 
error:function(xhr, status, error) {  
     alert('error');
}); 
Link to comment
Share on other sites

First thing, calling the template files directly won't work by design. Try to address real URLs, meaning implementations of these template(s). In the template files you can detect whether the page is called via AJAX:

<?php
if($config->ajax) {
    // page was requested from ajax
}

More info: https://processwire.com/talk/topic/225-how-to-work-with-ajax-driven-content-in-processwire/

Once you changed this, the session management should work, I think.

Link to comment
Share on other sites

i've also tried setting the AJAX url to:

url:"<?php echo $config->urls->templates?>logincode/",

to try and load it through processwire, but my AJAX call returns an error (which is set up as a javascript alert).  I'm guessing AJAX doesn't understand it's in ProcessWire so it can't find the page at all when doing it this way, so trying it this way also did not work :(

Link to comment
Share on other sites

i've also tried setting the AJAX url to:

url:"<?php echo $config->urls->templates?>logincode/",

to try and load it through processwire, but my AJAX call returns an error (which is set up as a javascript alert).  I'm guessing AJAX doesn't understand it's in ProcessWire so it can't find the page at all when doing it this way, so trying it this way also did not work :(

Why are you refering to the paths inside the template folder? This would return an error even if you wouldn't call it via AJAX. Try: 

url: '<?php echo pages->get('/logincode/')->url ?>'

:)

  • Like 4
Link to comment
Share on other sites

marcus thankyou!  

i think you missed out a $ sign thought, but i got it to work using:

url: '<?php echo $pages->get('/logincode/')->url ?>'

finally!  Thank you so much.  I should really have posted in here 8 hours ago although i wanted to figure it out myself, and learnt a hell of a lot by trying :)

Thanks mate!

  • Like 2
Link to comment
Share on other sites

marcus thankyou!  

i think you missed out a $ sign thought, but i got it to work using:

url: '<?php echo $pages->get('/logincode/')->url ?>'

finally!  Thank you so much.  I should really have posted in here 8 hours ago although i wanted to figure it out myself, and learnt a hell of a lot by trying :)

Thanks mate!

Oh yes, typo.

Pleasure! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...