• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send some data to a function without loading a new page

I'm trying to send a product id to a method without loading a new page, and since I do not have a sentence in the method to load another view I get a blank page.

This is the from inside the product view
echo form_open('shopping_basket/add_to_basket/');
echo form_hidden('pid', $id);
echo form_submit('add', 'Add to Basket');
echo form_close();

I have also tried using JavaScript which also loads a blank page
echo "<a href=''  return false;'>Add to Basket</a>";

function addBasket(id)
[removed].href = 'http://localhost/CI/index.php/shopping_basket/add_to_basket/' + id;
return false;

        $(document).ready(function() {
            $('#myform').ajaxForm(function() {
                alert("Product added to Basket!");
I'm using this jquery script now and it doesn't work. Looking at the console tab in Chrome it gives this error:
Uncaught TypeError: Object [object Object] has no method 'ajaxForm'

EDIT: ajaxForm is not in the JQuery documents, I think it's deprecated and that's why I got that error message. I'm using the .submit function now.

Jquery ajax form is a plugin that helps you handle ajax calls.
More info here: http://malsup.com/jquery/form/

In your case you can do something like:
  url: _root+'controller/method',
  data: {
   'id' : id
  type: 'POST',
  success: function($data){
     alert(' added to basket');

in the example i often use _ROOT as helper variable for my javascript file. I usualy set it on top of my documment like so:
var _root = "&lt;?php echo site_url() ?&gt;";

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.