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

#1
[eluser]ethio[/eluser]
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
Code:
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
Code:
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;
}

#2
[eluser]ethio[/eluser]
Code:
[removed]
        $(document).ready(function() {
            $('#myform').ajaxForm(function() {
                alert("Product added to Basket!");
            });
        });
1.9.1.min.js
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.

#3
[eluser]niki_mihaylov[/eluser]
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:
Code:
$.ajax({
  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:
Code:
var _root = "&lt;?php echo site_url() ?&gt;";


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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