Welcome Guest, Not a member yet? Register   Sign In
How to submit a form to an external controller without leaving the current page??
#1

[eluser]umbongo[/eluser]
Initially, pressing 'add to cart' submits a form to the cart controller which then loads the cart view.

I am trying to make the cart function as a pop-up layer (whilst keeping the original action for non-js). I used javascript to unhide a div containing $this->load->view(cart_view). Of course, this does not update the cart.. but if i submit the form then I would navigate away from the current page.

Can anyone point me in the right direction as to how to handle this??
#2

[eluser]skunkbad[/eluser]
[quote author="umbongo" date="1292405942"]Initially, pressing 'add to cart' submits a form to the cart controller which then loads the cart view.

I am trying to make the cart function as a pop-up layer (whilst keeping the original action for non-js). I used javascript to unhide a div containing $this->load->view(cart_view). Of course, this does not update the cart.. but if i submit the form then I would navigate away from the current page.

Can anyone point me in the right direction as to how to handle this??[/quote]

I think you just need to implement a simple AJAX solution.
#3

[eluser]cahva[/eluser]
Yes if js is enabled, use AJAX to submit and update the page. If not, send the form and redirect to the page where the form was submitted. You can either trust $_SERVER['HTTP_REFERER'] or like I did, set flashdata(for example previous_url) that you can use to redirect to the previous page.
#4

[eluser]vitoco[/eluser]
why you said "external controller", is there an "internal controller" that i don't know of ??
#5

[eluser]umbongo[/eluser]
@vitoco; i just meant not the controller relating to the page currently being viewed.

I am a complete noob when it comes to ajax / JS and pretty lost..

This is the form i have;
Code:
foreach($products as $product):

echo '<input type="hidden" name="id" value="'.$product->id.'" />';
echo '<input type="text" name="basket['.$product->id.']" value=""/>';
echo '<input type="submit" name="submit['.$product->id.']" value="Add" onclick="loadXMLDoc(this.form)"/>';

and this is the relevant part of my failed attempt at the js;
Code:
function loadXMLDoc(form)
{
xmlhttp.open("post","/basket/add",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xmlhttp.send(form);
}
Any ideas??
#6

[eluser]vitoco[/eluser]
i can't help you, i'm not use to make pure js ajax calls, and i will advice you to check some jquery examples, it's more easy to do complicated things.

Saludos
#7

[eluser]umbongo[/eluser]
Anyone?

I'm not planning on doing many complicated things so trying to avoid using jquery.




Theme © iAndrew 2016 - Forum software by © MyBB