How to submit a form using jquery dialog button?
12,934
Try by just changing this line and your usual js:
$attr = array('id'=>'addSupplier');
echo form_open('supplier_controller/insertNewSupplier', $attr);
If still does not submits the form then try to submit by ajax call.
$(".hero-unit input[name=add_supplier]").on('click',function(){
$('#popup').load("<?php echo site_url("supplier_controller/addNewSupplier/"); ?>").dialog({
title: "Add New Supplier",
autoOpen: true,
width: 800,
modal:true,
position: "center",
buttons: {
OK: function(){
$.ajax({
url : '<?=base_url()?>supplier_controller/insertNewSupplier',
type : 'POST',
data : $("#addSupplier").serializeArray(),
success : function(resp){
alert("Submitted !!!");
$(this).dialog( "close" );
},
error : function(resp){
//alert(JSON.stringify(resp));
}
});
},
CANCEL: function() {
$(this).dialog( "close" );
}
}
});
});
Author by
rochellecanale
Updated on June 04, 2022Comments
-
rochellecanale almost 2 years
Hello guys just want to ask about how can i process a form submission using the jquery dialog button. In my code I have a button that when you click. It will pop up a form in a dialog box. Below is the jquery buttons for OK and CANCEL. My problem is I can't submit my form the only option that I have is to create a submit button inside my form. But i want to use the jquery button instead. Im using CodeIgniter I hope you can help me.
My view (showAllSuppliers.php)
/* FOR ADD PAGE */ $(".hero-unit input[name=add_supplier]").on('click',function(){ $('#popup').load("<?php echo site_url("supplier_controller/addNewSupplier/"); ?>").dialog({ title: "Add New Supplier", autoOpen: true, width: 800, modal:true, position: "center", buttons: { OK: function(){ $("#addSupplier").submit(); //HOW CAN I SUBMIT MY FORM USING THIS? }, CANCEL: function() { $(this).dialog( "close" ); } } }); });
my form (addNewSupplier.php)
<?php $attr = array('id'=>'addSupplier'); echo form_open('supplier_controller/insertNewSupplier'); .. .. .. ... MY TEXTBOX FIELDS HERE .. .. //echo "<input type='submit' value='ADD' />"; ANOTHER OPTION FOR SUBMISSION echo form_close(); ?>
my controller function(supplier_controller.php)
public function insertNewSupplier(){ $this->supplier_model->insertNewSupplierDetail(); redirect('supplier_controller/index','refresh'); }
-
rochellecanale almost 11 yearsThe previous example works only for submit button. Is there a way how can i get the button id of the jquery ui dialog button?
-
rochellecanale almost 11 yearsi did that. After i fill up the fields and click the OK button there's an alert pop up but the form does not go to my controller.
-
Nil'z almost 11 yearswhat does it alerted?
-
Nil'z almost 11 yearschange this line :
data : $("#addSupplier").serializeArray(),
todata : {'data' : $("#addSupplier").serializeArray()},
and thisalert("Submitted !!!");
toalert("Submitted !!!"+resp);
. On your controller writeprint_r($_POST);die;
in the first line. Let me know the alert it gave. -
rochellecanale almost 11 yearswhen i clicked the OK button a pop up appeared displaying the word Submitted and a sets of array.
-
Nil'z almost 11 yearsYeah right. Now you just need to pass that array in your model and write the code for the insert. That's it. Your form is submitted ! :D
-
Nil'z almost 11 yearsAccept the answer if it helped you.
-
rochellecanale almost 11 yearsone more question how can i pass the array in my model?
-
rochellecanale almost 11 yearsas of now my controller contains the print_r($_POST);
-
rochellecanale almost 11 yearsHow can i access or pass it in my model?
-
rochellecanale almost 11 yearsShould i directly call it like $this->input->post('sample_name'); ?
-
Nil'z almost 11 yearswrite :
$this->model_name->function_name($this->input->post('data'))
. In your model declare a function likefunction function_name($form_array){//your code here}
-
rochellecanale almost 11 yearswhat does $this->input->post('data') means? is it my textfield name? so if i have many text field i need to call my model according to the number of the textboxes.
-
rochellecanale almost 11 yearsor should i place $this->input->post('data') in an array and pass it to my model?
-
Nil'z almost 11 years