views:

164

answers:

1

Hey all

i am trying to create a function that well keep the user in lightbox images while he adds to cart, for a demo you can visit

secure.sabayafrah.com username: mahmud password: mahmud

when you click at any image it well enlarge using lightbox v2, so when the user clicks at the image add, it well refresh the page, when i asked about it at jcart support form they informed me to use jquery live, but i dont know how to do it but as far as i tried this code which i used but still nothing is happening

        jQuery(function($) {

$('#button') 
    .livequery(eventType, function(event) { 
        alert('clicked'); // to check if it works or not
        return false; 
    }); 
 });

i also used

jQuery(function($) {

$('input=[name=addto') 
    .livequery(eventType, function(event) { 
        alert('clicked'); // to check if it works or not
        return false; 
    }); 
 });

yet nothing worked

for code to create those images

http://pasite.org/code/572

Update 1:

i have done this

function adding(form){
$( "form.jcart" ).livequery('submit', function() {var b=$(this).find('input[name=<?php echo $jcart['item_id']?>]').val();var c=$(this).find('input[name=<?php echo $jcart['item_price']?>]').val();var d=$(this).find('input[name=<?php echo $jcart['item_name']?>]').val();var e=$(this).find('input[name=<?php echo $jcart['item_qty']?>]').val();var f=$(this).find('input[name=<?php echo $jcart['item_add']?>]').val();$.post('<?php echo $jcart['path'];?>jcart-relay.php',{"<?php echo $jcart['item_id']?>":b,"<?php echo $jcart['item_price']?>":c,"<?php echo $jcart['item_name']?>":d,"<?php echo $jcart['item_qty']?>":e,"<?php echo $jcart['item_add']?>":f}                                        
});
 return false;                                          
}

and it seems to add to jcart but yet it still refreshes

+1  A: 

.live() is to assign handlers to future creating elements. On your site, however, you are re-loading the page so .live would have no bearing. (you are submitting a form)

It sounds like you want to make an ajax request to add the item to the cart and update that display on the site? That would be in the submit of the form and if jcart is dynamically created then yes, live is the answer.

$('.jcart').live('submit', function() {
    // aggregate form elements into object and send via ajax
    // update the cart on the page, since we haven't reloaded the page the light box is still displayed
});

Regarding comment:

When you send an ajax request, jquery takes an object as an argument. Such as $.post('urlToPostTo.php', { title: 'title of whatever', id: 5 } );

The server sees this the same as:

<form id="myForm" action="uroToPostTo.php" method="POST" >
    <input type="text" name="title" value="title of whatever" />
    <input type="hidden" name="id" value="5" />
    <input type="submit" name="submit" value="submit" />
</form>

So if you were to aggregate the form inputs into an object, there's a few ways (even some jquery plugins to help you out). The primitive way would be:

var $form = $('#myForm'); // instead of finding myForm over and over, cache it as a variable to use
var objToSend = {};
objToSend.title = $form.find('input[name=title]').val();
objTosend.id =    $form.find('input[name=id]').val();
$.post( 'urlToPostTo.php', objToSend );

A more Elegant solution is to have something loop through all form elements and put them into an object for you. Plugins like http://docs.jquery.com/Plugins:Forms make that a bit easier.

The end result is the form elements are stuffed into an object to send to your script.

Dan Heberden
@Dan Heberden i am a newbie what do you mean aggregate form elements into object and send via ajax
Mahmoud
Updated answer for you. Past that, I'd look into good ways to make ajax forms, what works for you to retrieve data, form validation before you send it, that kind of stuff.
Dan Heberden
@Dan Heberden i have successfully stop the lightbox from quitting but yet the form is not submitted to jcart what i did is just simple added beside the form ` onsubmit='return false'` but this caused the form not to be submited to jcart
Mahmoud
@Dan Heberden i have updated my question
Mahmoud
Think about what you're doing: if the form submits, it takes you to a new page (even though it's the same page, it's a 'new' instance of it) which means it's reloaded. If you want to submit the form content whilst the lightbox is present, you have to to that without submitting the page; you have to use ajax. The other option would be to somehow flag the lightbox to re-fire on the new page, which wouldn't look very good, ya? Your saying 'return false' does just that, dis-allows the form from submitting.
Dan Heberden
i think i found the error why its refreshing its becasue when i debugged the page its telling me that ` $('#cart').live('submit', function() { ` is null, is it possible
Mahmoud
@Dan Heberdan i successfully managed to make thing right you have helped a lot and i appreciate it
Mahmoud