3

I want to specify the controller path inside the .ajax function of jquery.

type:'POST',
                    url:"<?php site_url('login/validate'); ?>",
                    //dataType: "json",
                    data:data1,

                    success: function (response){

                            //alert (response);

                            $("span.error").html('Congrats! You are successfully registered with our site').addClass('error');
                            //window.location="site";

                        },

Currently i am doing this but the path is taken as a string. Its going into the success function i dont know whay? but when i alert the response it gives an error of "Disallowed key characters"

6
  • And what is code inside the controller and what data are you sending? Commented Mar 24, 2014 at 8:23
  • 1
    The code you have presented in your example, where is it placed? In a view (.php) file or in a Javascript (.js) file? Commented Mar 24, 2014 at 8:27
  • 1
    the code is placed in the .js file and i am validating the form fields and want to send the data to the php controller after validation to be inserted into the database Commented Mar 24, 2014 at 8:29
  • @Mudi Did you check my answer?I think it will help you Commented Mar 24, 2014 at 8:30
  • 1
    if you want to add php code to JS file, you need to serve the JS from a PHP file. Add a controller and change its header information and then add the JS in it's view and include the controller URL instead of the JS file. Commented Mar 24, 2014 at 8:50

3 Answers 3

1

This was due to url:"<?php site_url('login/validate'); ?>", create only url:"",You need to echo the url.

  url:"<?php site_url('login/validate'); ?>",

Change to

  url:"<?php echo site_url('login/validate'); ?>",

OR

 url:"<?= site_url('login/validate'); ?>",
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this and chk on the firefox debugger it is still taking that url as a string :(
var url="<?= site_url('login/validate'); ?>"; and try url:url,
1

Change your line

 url:"<?php site_url('login/validate'); ?>",

To

 url:"<?php echo site_url('login/validate'); ?>",

Comments

1

Rather than doing

url:"<?php site_url('login/validate'); ?>",

You need to do

url:"<?php echo site_url('login/validate'); ?>",

Because you want to echo out the return value of site_url

Note: The above won't work if you are attempting to call a PHP function from a .js file. You will need to build the JavaScript from a .php file. Alternatively you could declare a global JS variable with the result of site_url before you call your JS file and access it that way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.