I am new with AJAX, I have to make a modal contact form that pops up. I have everything the html the css the validation etc. Only the AJAX not, I don't know where to start, please can you help me out. I just want to preventDefault it and without pageload sending the e-mail.
This is the Javascript ( i think only the id is needed I wont post the whole code )
form id = #contact-form
The HTML
<div class="modal fade" id="contact" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 style="color:#31708f" class="modal-title" id="myModalLabel">Contact</h4>
</div>
<div class="modal-body">
<div class="col-lg-6">
<form role="form" name"contact-form" id="contact-form" method="post" action="contactengine.php">
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" id="name" type="text" name="name" />
</div>
<div class="form-group">
<label for="e-mail">E-mail adres</label>
<input class="form-control" id="email" type="email" name="email" />
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" id="subject" type="text" name="subject" />
</div>
<div class="form-group">
<label for="bericht">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="" cols="30" rows="5"></textarea>
</div>
<div class="form-group">
<input class="btn btn-info" id="submit" type="submit" value="Send" />
</div>
</form>
</div>
First, this is not JavaScript: form id = #contact-form.
Basically what you want to do is first making it work without AJAX and JavaScript. You need to have the PHP stuff working on the server.
Then, you use some JavaScript to prevent the Form from being sent (this is with jQuery):
$('#contact-form').on('submit', function(e) {
e.preventDefault();
//Here you send the ajax-request to the php-script.
$.ajax('send.php', {
data: $(this).serialize(),
type: 'POST',
success: function(data) {
alert(data); //for testing
}
});
});
See for example the jQuery documentation about ajax for further information about how to send the request.
Related
How do I give an error message inside modal after clicking update?
In my example here I gave a required attribute on the input field.
I want to change the error message based on Form Validation Best Practices.
(This code is part of CRUD PHP file for updating data in a table I had)
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="form-update" action="proses_edit.php" name="modal_popup" enctype="multipart/form-data" method="POST">
<div class="form-group" style="padding-bottom: 20px;">
<label for="Name">Name</label>
<input type="hidden" name="modal_id" id="edit-id" class="form-control" value="<?php echo $r['modal_id']; ?>"/>
<input type="text" name="modal_name" id="edit-name" class="form-control" value="<?php echo $r['modal_name']; ?>" required />
</div>
<div class="form-group" style="padding-bottom: 20px;">
<label for="Description">Age</label>
<input type="text" id="edit-description" class="form-control" value="<?php echo $r['description']; ?>" required />
</div>
<div class="modal-footer">
<button class="btn btn-success" type="submit">
Update
</button>
<button type="reset" class="btn btn-danger" data-dismiss="modal" aria-hidden="true">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
Here’s how form validation works with Bootstrap, taken from the documentation:
HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form>. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server side validation. They do not require a .was-validated parent class.
To achieve somewhat of what you're trying to do, taken from the example here you can use custom styles and create tooltips for each input and display a response based on the validation result. You will need to add the novalidate property to your <form> in order to prevent the default browser validation for this to work.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">First name</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Last name</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustomUsername">Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupPrepend">#</span>
</div>
<input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom03">City</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom04">State</label>
<input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom05">Zip</label>
<input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>
More information on Bootstrap Validation: https://getbootstrap.com/docs/4.0/components/forms/#validation
I have one pop popup form after filling that form(validations) then it will call another popup form
This is my form:
<form class="form-horizontal" action="#" method="post" name="mobnumber" enctype="multipart/form-data" id="frm-otp" style="display:none;">
<div class="form-groups">
<div class="col-sm-12">
<input type="text" class="form-control" id="mobileNumber" placeholder="otp" name="mobileNumber" required="required">
</div>
</div>
<div class="form-groups">
<div class=" col-sm-12 text-center">
<!-- <button type="submit" class="btn btn-info">SEND OTP</button>-->
<input type="submit" value="SendOTP'" id="submit" class="btn btn-info" data-toggle="modal" >
</div>
</div>
</form>
OTP Popup form
<!--enter otp-->
<div class="modal fade" id="enter-otp" role="dialog">
<div class="modal-dialog ">
<div class="modal-body">
<form action="ex1.php" method="post" name="otp" enctype="multipart/form-data" novalidate="novalidate">
<div class="form-group">
<input class="form-control" name="userotp" id="userotp" minlength="5" maxlength="5" required="required" placeholder="Enter OTP"
aria-required="true" type="text">
</div>
<input name="loginOTP" value="1" type="hidden">
<div class="form-group text-center">
<input class="btn btn-info" value="Verify" type="submit">
</div>
</form>
</div>
</div>
</div>
<!--enter otp form end-->
Popup calling fine but i want to validate that form form and after that will call popup, but validation is not calling only popup calling..
How to call popup after validating another form with popup and submission using jquery ajax...........I mean after clicking on id submit in first form then open other popup(id=enter-otp)
Ajax:
$(document).ready(function(){
alert("fsdg");
$("#frm-otp").submit(function() {
var mobnum = $(this).val('mobileNumber');
$.ajax({
type: "POST",
url: "<?php echo base_url('verify-mobilenumber'); ?>",
data: {mobnum: mobnum},
success: function(data) {
alert("Form With PopUp Opened");
}
});
});
});
I want mobile number value in second popup.How to put popup in this function
I have a site that has two contact forms (One is avaliable in a modal and the other is on the bottom of the page). Both forms have the same structure e.g same inputs and names etc... they both also have the same action method.
My question is - how can I use the same action method on both forms where the $_POST variables are dependant on what form is submitted? For example if I fill out the second contact form, the action gets the data from the first contact form.
I thought that one way would be to give both forms different id's e.g firstForm and secondForm and then pass the id of the form as a parameter to the action method. Although this could work I am not sure if it is an efficient way or if there is an easier way?
I have completely disregarded the fact that I could actually make two separate action methods but that is just ridiculous and a lot of redundant programming when I know I can just have one action method to serve both forms, but I'm just at a loss of how to implement it properly.
This is the first one:
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Flick me an email!</h3>
<p>I will get back to you ASAP!</p>
</div>
<div class="modal-body">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php">
<div class="form-group"><input class="form-control required" type="text" name="name" placeholder="Name" data-placement="top"></div>
<div class="form-group"><input class="form-control email" type="email" name="email" placeholder="example#email.com" data-placement="top" ></div>
<div class="form-group"><textarea class="form-control" name="message" placeholder="Your message here.." data-placement="top" style="height: 200px;" ></textarea></div>
<div class="form-group"><button type="submit" id="modal-submit" class="btn btn-danger btn-lg">Send Message</button><button class="btn" data-dismiss="modal" aria-hidden="true" style="float: right;">Cancel</button> </div>
</form>
</div>
</div>
</div>
</div>
And the second one:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required"
placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" required="required"
placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control"
rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-danger btn-lg">Send Message
</button>
</div>
</div>
</div>
</form>
In each of your forms you can add a hidden input element between your <form>...</form> tags to indicate which form is being submitted.
In your first form you could have this element:
<input type="hidden" name="type" value="form1">
Then in your second form add this element
<input type="hidden" name="type" value="form2">
When your form is submitted, the receiving PHP script can read the type value as a post variable that could you act on, e.g.,
if ($_POST['type']=='form1') {
// Do this
} else if ($_POST['type']=='form2') {
// Do this instead
}
Maybe you don't want to add a hidden input and you could add a parameter to the GET string of your form's action.
So, in your first form you can do this - add ?type=form1 to the end of your action:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php?type=form1">
And, in your second form you can do this - add ?type=form2 to the end of your action:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php?type=form2">
In the sendmail.php script you can access the value using the $_GET variable:
if ($_GET['type']=='form1') {
// Do this
} else if ($_GET['type']=='form2') {
// Do this instead
}
Place a:
<input type="hidden" name="formPosted" value="1">
inside every form that has action to specific php file, and for each form change input value, then in php use if else to see which form is submitted :) and then your code... :)
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');
}
I am using a form in a bootstrap modal window to post data to another page. However when i try to print the post values on the other page all I am getting is an empty array.
Here's my modal code:
<div class="modal hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
X</button>
<h3 id="myModalLabel">
Enter your Credentials</h3>
</div>
<div class="modal-body">
<form id="modal-form" class="form-horizontal" accept-charset="UTF-8" method="POST" action="login.php" data-remote="true">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="email" id="email" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="passwd" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<input type="submit" class="btn btn-primary" value="Submit"/>
Sign up
</div>
</div>
</form>
</div>
</div>
Here's the command I'm giving in login.php:
echo "Email is:";
echo $_POST["email"];
echo "Password is:";
echo $_POST["passwd"];
However all I'm getting is an empty array.
Is there some sort of special way I'm supposed to do this?
You have't define name in input just change two line from your code. add name of that input.
<input type="email" id="email" name="email" placeholder="Email" />
<input type="password" id="passwd" name="passwd" placeholder="Password" />