2

i want to call base_url() in js file because i want to call ajax in jquery

its my php file

$.ajax({
        url: '<?php echo site_url('home/update');?>',
        data: {
        email:email 
        },
        type: "POST",
        success: function(data) {
            $('#feedback').text(data).css('color','green');
        },

        });       
         } 
        else
        {
            $('#feedback').text('insert a valid email').css('color','red');
        }

        });

but i want to call in js file but i am confused how can i fix this because we can't call tags in js file

3
  • JS files do not get parsed for server-side code. If you want to provide data to a function, add it as a data attribute on the element which raises the event, then read it back in JS using $(this).data('foo'); Commented Feb 4, 2015 at 15:56
  • i am new in CI please send the code with example.. Commented Feb 4, 2015 at 15:58
  • Number of options, the best thing to do is to have your ajax call pull the action url. Then you can assign the site url to the form without worrying about the JS. Commented Feb 4, 2015 at 15:59

3 Answers 3

2
<script>
    var base_url = '<?php echo base_url(); ?>';
</script>
  • Write the above code in header.php file
  • Then use 'base_url' variable in all js files you want..

    $.ajax({
    url: base_url+'home/update');?>',
    data: {
    email:email 
    },
    type: "POST",
    success: function(data) {
        $('#feedback').text(data).css('color','green');
    },
    
    });       
     } 
    else
    {
        $('#feedback').text('insert a valid email').css('color','red');
    }
    
    });
    
Sign up to request clarification or add additional context in comments.

Comments

2

there is one more solution define

<script>
 var base_url=<?php echo base_url();?>
</script>

in your PHP file and use this in your js file.

If the code is in JS script file you can always store this value in hidden input field and access that. like:

<input type="hidden" id='base_url' value="<?php echo base_url();?>" >

And in JS file

var base_url = $('#base_url').val();

Comments

1

JS files do not get parsed for server-side code. If you want to provide data to a function, add it as a data attribute on the element which raises the event, then read it back in JS using $(this).data('foo');, something like this:

<button id="foo" data-action="<?php echo site_url('home/update');?>" data-email="<?php echo $email; ?>">Update</button>
$('#foo').click(function() {
    $.ajax({
        url: $(this).data('action'),
        data: { email: $(this).data('email') },
        type: "POST",
        success: function(data) {
            $('#feedback').text(data).css('color','green');
        },
    });   
});

1 Comment

i did it thanx @stackoverflow.. :) and thax Rory McCrossan

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.