Reputation: 127
is there a work around on how I can pass the value from 'value' going to an if statement?
I get the value now showing 1 or 0. What im trying to accomplish now is how I can pass it in the if statement. I wanted to have something like this. If val = 0 return true; else return false. Im not sure I'm in the right track of using the ajax function. But if there a better way you can show me I will appreciate it.
$(function(){
$('#add_product').click(function(){
var i = $('#product_name').val();
param = 'product_name='+i;
check_product(param).done(function(value) {
var val = value; //value return is 1 or 0 - this is fine
});
//val should be past here so that I can determine if I will return true or false
if(val == 0){
return true;
} else{
return false;
}
});
});
function check_product(param){
return $.ajax({
type : 'POST',
data : param,
url : baseurl+'cart/check_product_name/'
});
}
html form:
<?php echo form_open_multipart('cart/save_product'); ?>
<h3>Product Name:</h3>
<?php echo form_input(array('name' => 'product_name', 'id' => 'product_name'), set_value('product_name')); ?>
<h3>Price:</h3>
<?php echo form_input(array('name' => 'price', 'id' => 'price'), set_value('price')); ?>
<h3>Status:</h3>
<?php
$options = array(
'1' => 'Active',
'0' => 'Not Active'
);
echo form_dropdown('status', $options);
?>
<h3>Short Description:</h3>
<?php echo form_textarea(array('name' => 'short_description', 'id' => 'short_desc'), set_value('short_description')); ?>
<h3>Image:</h3>
<?php echo form_upload('userfile'); ?><br>
<p class="submit" id="post_submit"><input type="submit" value="Submit" id="add_product" /></p>
</form>
Upvotes: 0
Views: 4028
Reputation: 318202
You'll need to prevent the form from submitting, and then check with ajax if the product name exists, and when the ajax call has completed do the form submit with javascript if the returned value is zero:
$(function() {
$('#add_product').click(function(e) {
e.preventDefault();
var i = $('#product_name').val(),
param = 'product_name=' + i,
self = this;
check_product(param).done(function(value) {
if (value === 0) self.submit();
});
});
});
function check_product(param) {
return $.ajax({
type: 'POST',
data: param,
url: baseurl + 'cart/check_product_name/'
});
}
Upvotes: 1