Form is not submitting php and html - php

Good afternoon, I have a web project, ready, and the form validated with ajax and jquery, have also a PHP function where sending all data on my form, and I have to rest a modal window boostratp, activated by clicking on the submit, the problem is that jquery is blocking the passage of the form to php and is not sending only shows the modal. I guess I activate the jquery from php, but not exactly how to do it
this my form:
// What to do on submit - example of success message
// -------------------------------------------------
submitHandler: function(form) {
$('#successForm').modal('show');
return false;
}
});
// Reset the contact form when success message shown as bootstrap modal is dismissed
// ---------------------------------------------------------------------------------
$('#successForm').on('hidden.bs.modal', function() {
$('#contactForm').trigger("reset");
$('#contactForm .form-group').find('.glyphicon').removeClass('glyphicon-ok');
});
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/js-theme/jquery-1.11.3.js"></script>
<!-- Include all plugins (below), or include individual files as needed -->
<script src="js/js-bootstrap/affix.js"></script>
<script src="js/js-bootstrap/alert.js"></script>
<script src="js/js-bootstrap/button.js"></script>
<script src="js/js-bootstrap/carousel.js"></script>
<script src="js/js-bootstrap/collapse.js"></script>
<script src="js/js-bootstrap/dropdown.js"></script>
<script src="js/js-bootstrap/modal.js"></script>
<form id="contactForm" name="contactForm" action="enviar.php" method="post" enctype="multipart/form-data" target="_top">
<div class="col-lg-6">
<div class="form-group has-feedback wow flipInX" data-wow-duration="600ms">
<label for="nombre">Nombre*</label>
<input id="nombre" class="form-control input-lg" type="text" name="nombre" value="" placeholder="" aria-label="nombre" />
<span id="nombre1" class="glyphicon form-control-feedback"></span>
</div>
<!-- end .form-group -->
<div class="form-group wow flipInX" data-wow-duration="900ms">
<label for="celular">Celular</label>
<input id="celular" class="form-control input-lg" type="text" name="celular" value="" placeholder="" aria-label="celular" />
</div>
<!-- end .form-group -->
<div class="form-group has-feedback wow flipInX" data-wow-duration="800ms">
<label for="email">Email*</label>
<input id="email" class="form-control input-lg" type="email" name="email" value="" placeholder="" aria-label="email" />
<span id="email1" class="glyphicon form-control-feedback"></span>
</div>
<!-- end .form-group -->
</div>
<div class="col-lg-6">
<div class="form-group has-feedback wow flipInX" data-wow-duration="1000ms">
<label for="mensaje">Mensaje*</label>
<textarea id="mensaje" class="form-control" name="mensaje" rows="10" ></textarea>
<span id="mensaje1" class="glyphicon form-control-feedback"></span>
</div>
<!-- end .form-group -->
</div>
<div class="clearfix"></div>
<div class="text-center">
<div class="form-group wow zoomInDown" data-wow-duration="800ms">
<button id="submitContact" type="submit" class="btn btn-default btn-lg">Enviar</button>
<button class="btn btn-danger btn-lg" type="reset">Borrar</button>
</div>
<!-- end .form-group -->
</div>
<!-- end .text-center -->
</form>
<!-- Modal success on submit -->
<div class="theme-submit-success-modal">
<div class="modal fade" id="successForm" tabindex="-1" role="dialog" aria-labelledby="successFormModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="text-center">
<span class="fa fa-comments-o"></span>
</div>
<h4 class="text-center" name="modal_contact" id="successFormModalLabel">Enviado Con Éxito</h4>
<p class="text-center">Gracias por contactarnos, en breve contestaremos a su mensaje. </p>
</div>
<div class="modal-footer">
<div class="text-center">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
</div>
</div>

You should start learning how to make websites. So much code for a simple form?!
You better show the modal after the form is sent not just submitted, I think.
Is there a reason for returning false here:
submitHandler: function(form) {
$('#successForm').modal('show');
return false;
}
This maybe preventing execution of other necessary code.

Related

Applying Validation to Input field before opening Bootstrap Model in Vue Laravel

This is my laravel blade page content of
<!-- Main Container -->
<main id="main-container">
<!-- Page Content -->
<div class="content" id="app">
<!-- Lock Forms -->
<!-- <h2 class="content-heading">Add Accessory</h2> -->
<!-- Register Forms -->
<div class="row">
<div class="col-md-12">
<!-- Bootstrap Register -->
<div class="block block-themed">
<div class="block-header bg-gd-dusk">
<h3 class="block-title">Add Accessory</h3>
</div>
<div class="block-content">
#include('errors.error')
<form enctype="multipart/form-data">
#csrf
<div class="form-group row">
<div class="col-12">
<label>Select Store</label>
<select class="form-control" name="store_id" v-model="form.store_id" required>
<option v-for="store in stores" :key="store.id" v-bind:value="store.id">#{{store.channel_id}}</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label>Requested Date</label>
<input type="text" class="flatpickr form-control" v-model="form.request_date" name="request_date" required>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<button type="button" #click="AddAccessory()" class="btn btn-primary">Add New Accessory</button>
</div>
</div>
</form>
</div>
</div>
<!-- END Bootstrap Register -->
</div>
</div>
</div>
<div class="modal fade" id="accessory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Accessory Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form #submit="CreateAccessory()" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group row">
<div class="col-12">
<label>Choose Accessory Type</label>
<select class="form-control" name="type" required>
<option>Case</option>
<option>Projector</option>
<option>Glass</option>
<option>Charger</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label>Description</label>
<textarea class="form-control" name="description" placeholder="Enter Description"> </textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</main>
<script>
var app = new Vue({
el: '#app',
mounted: function() {
},
data() {
return {
stores: {!! $stores !!},
users: {!! $users !!},
form: {
user_id: '',
request_date: '',
store_id: '',
type: '',
description: ''
},
}
},
methods: {
AddAccessory() {
$('#accessory').modal('show');
},
CreateAccessory() {
axios.post('/accessory/store',{
data: this.form,
})
.then(response => {
//$(location).attr('href', '/cashoutdetails')
})
.catch(function (error) {
alert('Error');
});
}
}
})
</script>
'''
In web.php, this is my route --
Route::post('accessory/store','AccessoryController#store');
My Issues are --
I want to open my bootstrap model only if my input fields of store id and date is being filled by user but where as it is opening in all cases whenever i click on add accessory button. Please suggest !!
accesssory/store route is not working ... whenever i click on submit button of model, it takes me to this page --- /accessory/create?type=Case&description=+ .
As you're not using any client side validation engine and relying on native HTML form constraints, we can continue on with the native constraints api. You can get all the elements of a form (in this case, by name) and call checkValidity on them. We can then loop each of those elements within the form to determine the validity state prior to showing our modal:
AddAccessory() {
const valid = [].slice.call(document.forms.add_accessory.elements).map((el) => {
return el.checkValidity()
}).filter((v) => v === false).length === 0
if (valid) {
$('#accessory').modal('show');
}
}
Make sure to give your form element a name so it can be found correctly:
<form name="add_accessory" type="multipart/form-data"

Add ckeditors fields dynamically in modal

i have a cms where on click open modal and inside i have few fields and then i have another button inside of modal where i can add dynamically ckeditors fields by clicking on button, here is what i was trying to do:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-md-12">
<button style="margin-bottom:5px;" type="button" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i> Adauga</button>
</div>
<div id="add_data_Modal" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Adauga categorie</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="container">
<form action="add.php" method="POST">
<div class="form-group">
<label for="input-1">Nume Categorie</label>
<input type="text" class="form-control" name="nume" id="nume" placeholder="Nume categorie">
</div>
<div class="form-group">
<label for="input-1">Categorie</label>
<select class="form-control" id="categorie" name="categorie">
<option value="0">Categorie principala</option>
</select>
</div>
<div class="form-group">
<label for="input-1">Vizibil</label>
<select class="form-control" id="activ" name="activ">
<option selected>Selecteaza vizibilitatea</option>
<option value="1">Da</option>
<option value="0">Nu</option>
</select>
</div>
<div class="form-group">
<label for="input-1">Pozitia</label>
<input type="text" class="form-control" name="pozitie" id="pozitie" placeholder="Pozitie">
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox1" checked="">
<label for="user-checkbox1">Modele Produse</label>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox2" checked="">
<label for="user-checkbox2">Paletar Materiale</label>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox3" checked="">
<label for="user-checkbox3">Estimare Pret</label>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="rowa">
<div class="add-plus">Adauga tab-uri</div>
<div class="col-md-12">
<span id="add_aut" class="btn btn-primary">+</span>
<div id="auth-rows"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fa fa-times"></i> Inchide</button>
<button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-check-square-o"></i> Salveaza</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function(e){
// variable
var html3 = '<div class="row" id="parent-autor"> <p id="auth-del"><i class="fa fa-minus-square" aria-hidden="true"></i></p> <div class="row"> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="titlu_tab" name="titlu_tab[]"> </div> </div> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="Pozitie" name="pozitie_tab[]"> </div> </div> </div> <div class="col-md-12"> <div class="form-group"> <textarea name="editor[]" class="ckeditor" id="continut" rows="8" cols="80"></textarea> </div> </div></div>';
$("#add_aut").click(function(e){
$('#auth-rows').append(html3);
});
$('#auth-rows').on('click', '#auth-del', function(E){
$(this).parent('div').remove();
});
});
</script>
In this modal when i click on add i want to add more fields, is done this but not appear ckeditor just a plain textarea which i don`t need that.
Thank you!
You are probably going to want to give the textarea a custom id if you plan to have multiple of them.
let currentId = 0;
I moved the 'var html' code inside the create function so that we can edit the id every time the add button is clicked because we need a unique id each time. After the textarea exists, I call .ckeditor() on that textarea which should enable the ckeditor tools for that field. Then I increment the customId to make sure that the next form that is added has a different id.
$("#add_aut").click(function(e){
var html3 = '<div class="row" id="parent-autor"> <p id="auth-del"><i class="fa fa-minus-square" aria-hidden="true"></i></p> <div class="row"> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="titlu_tab" name="titlu_tab[]"> </div> </div> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="Pozitie" name="pozitie_tab[]"> </div> </div> </div> <div class="col-md-12"> <div class="form-group"> <textarea name="editor[]" class="ckeditor" id="continut' + currentId + '" rows="8" cols="80"></textarea> </div> </div></div>';
$('#auth-rows').append(html3);
$('#continut' + currentId).ckeditor();
currentId += 1;
});
You may not want the customId in the id of the textarea depending on what you are trying to accomplish, but this should get you going in the correct direction.
Also, you need to add the ckeditor.js as well as a jquery adapter script for this to work.
See this fiddle https://jsfiddle.net/bkqxnu8f/3/

Button inside modal is not working with Bootstrap 4 but is working with Bootstrap 3

Im working in a laravel project and I can use a button in order to show a modal and I have a delete button inside this modal and is working using botstrap 3 but is not working using bootrsap 4. ¨
This is my html code,
these CDN are working:
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
and when I use these cdn my delete button inside my modal doesnt work anymore:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
My modal:
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-cliente">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-orden_de_compra"></h4>
</div>
<div class="modal-body">
<h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
<br />
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="id">id:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id_delete" disabled>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="orden_de_compra_delete" disabled>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-danger delete" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-trash'></span> Delete
</button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
My js:
<script type="text/javascript">
// delete a post
$(document).on('click', '.delete-modal', function() { //this function works with bootstrap 4
$('.modal-orden_de_compra').text('Delete');
$('#id_delete').val($(this).data('id'));
$('#orden_de_compra_delete').val($(this).data('orden_de_compra'));
$('#deleteModal').modal('show');
id = $('#id_delete').val();
orden_de_compra = $('#orden_de_compra_delete').val();
alert(id);
});
$('.modal-footer').on('click', '.delete', function() { //this function doesnt work with bootstrap 4
$.ajax({
type: 'DELETE',
url: 'posts/' + id,
data: {
'_token': $('input[name=_token]').val(),
},
success: function(data) {
toastr.success('Pedido eliminado exitosamente!', 'Notificación', {timeOut: 5000});
$('.item' + data['pedido_id']).remove();
alert('.item' + data['pedido_id']);
}
});
});
</script
Okay... I will do both snippets for you to see the difference.
BS3:
$('.modal-footer').on('click', '.delete', function() {
console.log("I'm here.");
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/js/bootstrap.min.js"></script>
<button type="button" data-toggle="modal" data-target="#deleteModal">Open Modal</button>
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-cliente">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-orden_de_compra"></h4>
</div>
<div class="modal-body">
<h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
<br />
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="id">id:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id_delete" disabled>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="orden_de_compra_delete" disabled>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-danger delete" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-trash'></span> Delete
</button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
After a look at the Migrating to v4 documention and finding an absolute nothing about this...
I finally found it by comparing the v4 markup in search of what the %&* can be different.
It's pretty subtile... I don't know what class="modal-cliente" was for... Potentially something wrong that wasn't affecting v3. But it seems it is the issue with v4. The role="document" may be important too... Sorry, I can't explain in more details. ;)
Look for the 2 slightly changed lines in the markup below.
BS4:
$('.modal-footer').on('click', '.delete', function() {
console.log("I'm here.");
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<button type="button" data-toggle="modal" data-target="#deleteModal">Open Modal</button>
<div id="deleteModal" class="modal fade" role="dialog">
<!-- Those 2 lines are slightly changed -->
<div class="modal-dialog" role="document">
<div class="modal-content">
<!-- ============================ -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-orden_de_compra"></h4>
</div>
<div class="modal-body">
<h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
<br />
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="id">id:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id_delete" disabled>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="orden_de_compra_delete" disabled>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-danger delete" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-trash'></span> Delete
</button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>

How to display error message if the user clicks on second tab before filling the data of the first tab

On button click, four bootstrap tabs are displayed and by default first tab information is displayed.
But if the user clicks on the second tab instead of filling the information in the first tab an error message should be displayed and he should not be allowed to click on any of the tabs unless he fills the information in the first tab.
After filling the required information on the first tab he should be directed to the second tab and the information filled by the user in the first tab should be saved in the database on click of save button, which is there on the first tab.
For answer your question I just wrote a simple snippet where you can get when the form is filled or not.
inside isValid, true or false you can run all your custom code.
For enable disabled the bootstrap form I just removed or add the 'data-toggle="tab"' without add class or other css.
For check if the form is filled I added a class "required" for the fields that you want to controll and wrote a function where check 'on click' event if that fields are not null.
Alternatively if you want to implement a ready plugin for validate your form and spend less time to code your custom code well check this link
http://formvalidation.io/examples/bootstrap-wizard/
function validateForm() {
var isValid = true;
$('#installationForm .form-group .required').each(function() {
if ( $(this).val() === '' )
isValid = false;
});
return isValid;
}
$('.next-tab').on('click', function(event) {
var result = validateForm();
if (result) {
$('.next a').attr('data-toggle', 'tab');
} else {
$('.next a').removeAttr('data-toggle');
alert('You have to fill the form before!');
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<form id="installationForm" class="form-horizontal">
<ul class="nav nav-pills">
<li class="active">Site information</li>
<li class="next"><a class="next-tab" href="#database-tab" data-toggle="tab">Database</a></li>
</ul>
<div class="tab-content">
<!-- First tab -->
<div class="tab-pane active" id="basic-tab">
<div class="form-group">
<label class="col-xs-3 control-label">Site name</label>
<div class="col-xs-5">
<input type="text" class="required form-control" name="name" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">URL</label>
<div class="col-xs-7">
<input type="text" class="required form-control" name="url" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Owner email</label>
<div class="col-xs-5">
<input type="text" class="required form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Description</label>
<div class="col-xs-7">
<textarea class="required form-control" name="description" rows="6"></textarea>
</div>
</div>
</div>
<!-- Second tab -->
<div class="tab-pane" id="database-tab">
<div class="form-group">
<label class="col-xs-3 control-label">Server IP</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbServer" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Database name</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbName" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Database user</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbUser" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Password</label>
<div class="col-xs-5">
<input type="password" class="form-control" name="dbPassword" />
</div>
</div>
</div>
<!-- Previous/Next buttons -->
<ul class="pager wizard">
<li class="previous">Previous</li>
<li class="next">Next</li>
</ul>
</div>
</form>
<div class="modal fade" id="completeModal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Complete</h4>
</div>
<div class="modal-body">
<p class="text-center">The installation is completed</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal">Visit the website</button>
</div>
</div>
</div>
</div>

Dynamically inserting a bootstrap modal partial view into a view

I have a bootstrap 2.32 modal form which is kind of long ( see below ). I'd like to implement this as a separate partial view to be dynamically inserted in the HTML of another view ( for maintainability ), but I'm not sure exactly how to do this. I'm working with Codeigniter 2.1.
The button of the main view is:
<div id="thanks"><p><a data-toggle="modal" href="#form-content" class="btn btn-primary btn-large">Modal powers, activate!</a></p></div>
Here is what I'd like to store separately as a partial view:
<div id="form-content" class="modal hide fade in" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Send me a message</h3>
</div>
<div class="modal-body">
<form class="contact" name="contact">
<fieldset>
<!-- Form Name -->
<legend>modalForm</legend>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="user">User</label>
<div class="controls">
<input id="user" name="user" type="text" placeholder="User" class="input-xlarge" required="">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="old">Old password</label>
<div class="controls">
<input id="old" name="old" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="new">New password</label>
<div class="controls">
<input id="new" name="new" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="repeat">Repeat new password</label>
<div class="controls">
<input id="repeat" name="repeat" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
</fieldset>
</form>
</div>
<div class="modal-footer">
<input class="btn btn-success" type="submit" value="Send!" id="submit">
Nah.
</div>
</div>
You can do this in any point into a view where you need to load the 'partial' view.
$this->load->view('modal_view');
EDIT: Load dynamic content
In this example I will use an jQuery AJAX call to retrieve the view dynamically.
In your view you have to include a target div to insert the AJAX response.
<div id="modal_target"></div>
The button to load the modal and show.
<button type="button" class="btn btn-primary btn-medium" onclick="get_modal();" >Show Modal</button>
The javascript function that do the magic
<script type="text/javascript">
function get_modal(){
$.ajax({
type : 'POST',
url : '<?php base_url() ?>controler/get_modal',
cache : false,
success : function(data){
if(data){
$('#modal_target').html(data);
//This shows the modal
$('#form-content').modal();
}
}
});
}
</script>
You have also to include in your contoller the function called in AJAX
public function get_modal()
{
$this->load->view('modal_view');
}

Categories