jeudi 1 janvier 2015

Is it possible to disable/enable a jquery function on change of radio button


I am working on the checkout part of an online store. I created a script to be triggered once a form was submitted. i want this script to activate only if the selected payment method is not paypal since i want to skip the ajax process.


I am using codeigniter framework


Here's the code for the view



<form action="<?php echo site_url('products/order')?>" method="post" id="checkoutForm" name="checkoutForm">
...
<input type="radio" name="paymentMethod" value="cod">CASH ON DELIVERY
<input type="radio" name="paymentMethod" value="paypal">PAYPAL
...
<input type="submit" id="btnOrder" value="ORDER NOW">
</form>

<script type="text/javascript">
var paymentMethod = '';
$(document).ready(function(){
$('input:radio[name="paymentMethod"]').change(function(){
paymentMethod = $(this).val();

});

if(paymentMethod != 'paypal' && paymentMethod != ''){
$('#checkoutForm').submit(function(){
...
$.post($('#checkoutForm').attr('action'), $('#checkoutForm').serialize(), function( data ) {
...
}, 'json');
return false;
});
}
});
</script>


Here's the code for the function in my controller (products.php)



public function order() {
$paymentMethod = $this->input->post('paymentMethod');
...
if($paymentMethod != 'paypal'){
echo json_encode(array('stat' => 1,'msg' => 'Registered Successfully', 'order_id' => $order_id));

}else{
// ... Some paypal configurations
$this->paypal->pay(); //Proccess the payment
}


} # End order


What i've tried so far was to put an if statement outside the $('#checkoutForm').submit to check if radio button value is not paypal, but it's not working.


Thanks in advance





Aucun commentaire:

Enregistrer un commentaire