Welcome Guest, Not a member yet? Register   Sign In
Ajax request using site_url not working
#1

(This post was last modified: 01-19-2015, 02:51 AM by DreamOfSleeping.)

Hello. I'm new to php, ajax and codeigniter so I'm sure I'm doing something silly. But this code works...

edit: Forgot to mention I'm using Jquery for the first time as well!


Code:
$.ajax({
           type: 'POST',
           url: "http://localhost/websitename/index.php/signup/email_available",
           data: {email: e},
           success: function(response) {
               signupValidator.emailAvailableCallback(response);
           }
       })

But this code does not work....


Code:
$.ajax({
           type: 'POST',
           url: "<?php echo site_url('signup/email_available')?>",
           data: {email: e},
           success: function(response) {
               signupValidator.emailAvailableCallback(response);
           },
           
           error: function(s, s2, s3) {
               alert(s.responseText);
           }
       })

Basically the problem is with the php echo site url line. This does not seem to be working. I'm not sure how to even check what it is printing out. When I check the source on the page the php code is written out and not the site address. I've searched many examples on the internet and they all seem to do it the way I have tried.

*websitename isn't actually the name of my site. I changed that as I copied the code in because I don't want then name of my site known yet.
Reply
#2

Use Alert to show it, then you can see what it is doing.

I use global js vars for passing in the url's.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Cool thanks. I'm happy to use global js vars if that's deemed ok. It makes sense. That means when my url changes I only have one extra place to change it.

I've used alerts to show errors, but I've not found anything that tells me what the url it's been sent to. I'm not really sure how php works when it's embedded in javascript.
Reply
#4

Well... after the echo you should put a semicolon: 
Code:
<?php echo site_url('signup/email_available');?>

To see if the echo worked, you should see in the html code (right click and View source) the address.
Reply
#5

Thanks for replying. I've added the semicolon and it still does not work. I've checked the js source and it just reads like I've wrote the php. Like this...

url: "<?php echo site_url('signup/email_available');?>",
Reply
#6

(This post was last modified: 01-20-2015, 06:22 AM by Avenirer.)

Does the view have ".html" as extension? Because maybe the server is not allowing for .html files to execute php lines. If that is the case, the solution would be to change the extension to .php
Reply
#7

in your ajax code: data: {email: e}

where is 'e' defined to have a value?

plus your only showing the client side jquery code, if you show some code from the controller it may help us help you better
"I reject your reality and substitute my own" - Adam Savage, M5 Inc.
Reply
#8

Avenirer is on the right track. If you're seeing the <?php tag in "View Source", the code isn't being processed by the server. If it's a JavaScript (.js) or HTML (.htm/html) file, the server may not be sending it through the PHP parser, so you may have to change the file extension or load the file from a PHP script to ensure the PHP tags are parsed properly before being sent to the client.
Reply
#9

(This post was last modified: 01-20-2015, 09:30 AM by DreamOfSleeping.)

Thanks everyone. For those asking about the e variable. The only difference between my example that works and my example that doesn't work is the url variable. So everything in the controller must be ok.

It's in a js file so that must be the problem. The view has a php extension but that loads this code in a separate js file. I only have a couple of hours each morning to work on this so I'll check it out your suggestions tomorrow. Thanks again.
Reply
#10

(01-19-2015, 02:50 AM)DreamOfSleeping Wrote: Hello. I'm new to php, ajax and codeigniter so I'm sure I'm doing something silly. But this code works...

edit: Forgot to mention I'm using Jquery for the first time as well!



Code:
$.ajax({
           type: 'POST',
           url: "http://localhost/websitename/index.php/signup/email_available",
           data: {email: e},
           success: function(response) {
               signupValidator.emailAvailableCallback(response);
           }
       })

But this code does not work....



Code:
$.ajax({
           type: 'POST',
           url: "<?php echo site_url('signup/email_available')?>",
           data: {email: e},
           success: function(response) {
               signupValidator.emailAvailableCallback(response);
           },
           
           error: function(s, s2, s3) {
               alert(s.responseText);
           }
       })

Basically the problem is with the php echo site url line. This does not seem to be working. I'm not sure how to even check what it is printing out. When I check the source on the page the php code is written out and not the site address. I've searched many examples on the internet and they all seem to do it the way I have tried.

*websitename isn't actually the name of my site. I changed that as I copied the code in because I don't want then name of my site known yet.

In your first example you use index.php but not in your second example. Why? Have you changed config.php from $config['index_page'] = 'index.php'; to $config['index_page'] = ''; and made a .htaccess file in the root directory ?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB