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');
}
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" );
}
}
});
});
You need ajax call
$('#submit').click(function (){
var first_name = $('#first_name').val();
$.ajax({
url : '/supplier_controller/insertNewSupplier,
type : 'post',
data : first_name,
success : function(){
//do something;
console.log(first_name);
}
})
return false;
});
That's gonna work if you ad id to submit button and to your input. But you can also call them via html tags. It's just an idea or example for you.
If you want to use classic form submission instead of ajax call, write these code lines for the buttons parameter:
buttons: {
OK: function(){
$(this).dialog().find('form').submit();
},
CANCEL: function() {
$(this).dialog( "close" );
}
}
Related
I have the following code:
HTML
<div id="resume-begin" style="background:#CCCCCC; display:inline-block;">
<img src="/images/plus-plus.png" style="display:inline;" class="trigger"/>
</div>
<div class="input-resume-data" style="display:none;">
<form id="project-form">
<label for="resume-create-name">Resume Project Name:</label>
<input type="text" id="resume-create-it" name="resume-create-it" class="text ui-widget-content ui-corner-all">
</form>
</div>
jQuery
<script>
$(document).ready(function() {
$('#resume-begin').click(function() {
('#resume-begin').hide();
$('.input-resume-data').dialog("open");
});
});
</script>
Once the div resume-begin is clicked, it opens my UI Dialog box, which is as follows:
<script>
$(function() {
$( ".input-resume-data" ).dialog({
title: 'Name your Resume Project',
autoOpen: false,
resizable: false,
height: 450,
width: 380,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
buttons: {
"Submit": function() {
var $this = $(this);
var string = $('#resume-create-it').serialize();
$.ajax({
url: 'functions.php',
type: 'POST',
data: string,
success: function(data){
alert("Project created!");
$this.dialog('close');
}
});
},
Cancel: function() {
$( this ).dialog( "close" );
$( "#resume-begin" ).show( "slow", function() {
}); //end function
} //end cancel button
} //end buttons
}); //end dialog
}); //end jquery function
</script>
What I would like:
Once the user enters the text in the UI-input, and clicks Submit, I want to post the data to the PHP page, where I would save it to the database. Currently, if I enter in data and push submit, the success function works, but the data is not saved in the database.
In my PHP, I try to grab the data like:
//Sanitize the POST values
$resumeProjectname = $_POST['resume-create-it'];
I am unsure if I am passing the data correctly, which is through the variable "string" in jQuery, as well as if I'm retrieving the data correctly in the php file.
Any help would be greatly appreciated. Please let me know if you have any questions!
Thanks.
From my view i found two errors
('#resume-begin').hide();//$ missing
and another
var string = $('#resume-create-it').serialize();
should be
var string = $('#project-form').serialize();
As serialize applies to Form.
https://api.jquery.com/serialize/
To send your data do the following
data: {
resume_name: string
},
dataType: "json"
And then in PHP access it with
$resume_name = $_POST['resume_name'];
And as a final note, I wouldn't call a variable string since string is a keyword in many languages. At first I thought you were trying to tell it that the data you're sending is a string. Also don't call the response from the server data because you use the word data two lines above it. Use something like response.
I should mention
You don't have to use JSON. You can use other data types but that is one of the critical pieces that you are missing. You need to set the dataType so that jQuery knows how to format your response. I personally think that since you are using JavaScript to begin with it makes sense to use JavaScript Object Notation.
I have a series of checkboxes that correspond to entries, and a delete all button that I have attached a jquery ui modal confirm. Fiddle here: http://jsfiddle.net/BxdWc/. The issue is that, it does not submit the form (is not processed by php) after hitting yes in the confirmation dialog. The php code is include at the top of the page, but I am fairly certain it has nothing to do with this, as without the modal the code processes, and deletes the checked entries properly.
JS:
$(function () {
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$("#confirm").submit();
$(this).dialog("close");
},
"No": function () {
$(this).dialog("close");
}
}
});
$("#doDelete").click(function (e) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
});
});
HTML:
<form post="self.php" id="confirm">
<!-- some inputs .etc -->
<input name="doDelete" type="submit" id="doDelete" value="Delete" class="btn btn-danger">
</form>
It looks like your PHP is checking for which button is clicked. jQuery does not include the buttons when you submit a form via $('#confirm').submit();. The browser will only include the buttons in the submission if the button was actually clicked.
There are a couple ways to get the button name/value pair to be included in the submission.
You can use $('form').serializeArray(); and push the doDelete/Delete pair as described in this answer
Or you might be able to get away with doing something like this. Notice the click of the delete button is called a second time. The button click event checks if it was initiated via a trigger (aka the dialog). The second time around, it will not be prevented from being submitted.
$(function () {
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$(this).dialog("close");
$("#doDelete").click();
},
"No": function () {
$(this).dialog("close");
}
}
});
$("#doDelete").click(function (e,ui) {
if(e.originalEvent) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
}
});
});
The fiddle is here
I modified this a little bit so that it can work when you denote the confirm button with a class rather than an id. I'm a javascript novice so beware! Feel free to improve on this.
$(function () {
$("input.doDelete").click(function (e,ui) {
var button = $(e.target);
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$(this).dialog("close");
button.click();
},
"No": function () {
$(this).dialog("close");
}
}
});
if(e.originalEvent) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
}
});
});
i am working on a code igniter ..In my view page, i have one modal popup. In the modalpopup, i have put form.so what i want is that When user click save button ..the model pop up close through jquery..
this is my save and close button ... the model is closes fine if i click the close button but because for save button i have given control to the jquery so i want to close there which i dont know
Close
<a id = "save" class="btn x" data-dismiss="modal">Save changes</a>
<script type="text/javascript">
$('#save').click(function() { // $("#form").serialize()
var check_no = $('#check_no').val();
var form_data = {
check_no: $('#check_no').val(),
};
$.ajax({
url: "<?php echo site_url('checkDetailsController/addCheckDetails'); ?>",
type: 'POST',
data: form_data,
dataType: 'json',
success: function(msg) {
if(msg.res == 1)
{
alert('true')
}
else{
alert("false");
}
}
});
return false;
});
</script>
The save function must use $('#dialog').dialog("close"); instead of $(this).dialog('close'). This causes the close method attached to dialog object to be called.
If you use the dialog from jqueryUI you can use the buttons option.
$('#yourcontainer').dialog({
buttons : {
// Save and close button
'Save' : function () {
// your javascript code for saving
$(this).dialog('close');
},
// Cancel button
'Close' : function () {
$(this).dialog('close');
}
},
// and your other settings
});
I created a conformation box from jquery. I want to submit the page and view PHP echo message when click the confirm button in the confirmation box. Can you help me with code that comes for the confirmation button?
My jQuery/javascript function is here:
$(document).ready(function(){
$('#click').click(function(){
//$(function() {
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$( "#dialog:ui-dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Ok": function() {
// I WANT THE CODE FOR HERE TO SUBMIT THE PAGE TO WIEW PHP ECHO MESSAGE
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
});
You can use jQuery Form submit library. This will give you many options.
And according to your requirement you can call
beforeSubmit: 'YOUR FUNCTION To OPEN DIALOG',
If this returns true your form will get post and you will definitely get response.
see example here : http://malsup.com/jquery/form/#ajaxForm
Use the jQuery Post function...
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
Here is a version with a callback when the post completed...
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
}
);
Or if you have an existing form that you want to post use the jQuery submit function...
$("form").submit();
But before you do this you need to make sure that you populated the input fields in the form.
try this code,
var parameter_1 = 'test';
var parameter_2 = 'test';
$.ajax(
{
type: "POST",
url: "/submit_url.php",
data: "parameter_1="+ parameter_1 +"& parameter_2 ="+ parameter_2,
success: function(html)
{
alert('Submitted');
}
});
I want to open a jQuery UI dialog box and the box will have an input field which will take username. I want that when user presses the OK key on the dialog box, the dialog should return the username back to jQuery somehow
$("#dialog").dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
$(this).dialog("close");}}
});
A dialog is asynchronous, which makes it meaningless to "return" something.
You can, however, use one of the callback functions to perform actions with the user input.
For example, using the 'OK' button callback:
$("#dialog")
.append(
$("<input>")
.attr({
"type" : "text",
"id" : "user-input"
})
)
.dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
alert( $("#user-input").val() );
$(this).dialog("close");
}}
});
If you just have a div on your page with ID dialog that contains the input field, just select it using a regular jQuery selector when the user presses OK.
Try this on "OK" funcion
var myvalue= $("#yourinputtext").val();
alert(myvalue);
You can capture the value in the ok button click handler, and then do with it as your require:
$("#dialog").dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
var inputValue = $("#myInput").val();
// do stuff with the value...
$(this).dialog("close");}
}
}
);