how to insert value into data base using jquery/ajax? - php

index.php
<script>
$(document).ready(function(){
$(".submit").click(function(){
var formname = $(this).parents("form").attr("id");
cname = $("#"+formname+" #cname").val();
name = $("#"+formname+" #name").val();
fname = $("#"+formname+" #fname").val();
email = $("#"+formname+" #email").val();
phone = $("#"+formname+" #phone").val();
x = $("#"+formname+" #x").val();
xii = $("#"+formname+" #xii").val();
qualify = $("#"+formname+" #qualify").val();
$.ajax({
type:"POST",
data:{"cname":cname,"name":name,"fname":fname,"email":email,"phone":phone,"x":x,"xii":xii,"qualify":qualify},
url:"enquiry.php",
success:function(data){
$("#"+formname+" #msg").html(data);
}
});
});
});
</script>
Enquire</span>
<div class="modal fade" id="myModal<?php echo $id; ?>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div id="msg"></div>
<form method="post" name="formid<?php echo $id; ?>" name="formname<?php echo $id; ?>">
<input type="text" name="name" id="name" placeholder="Enter Your Name">
<input type="text" name="fname" id="fname" placeholder="Enter Your Father's Name"><br/>
<input type="text" name="email" id="email" placeholder="Enter Your Email">
<input type="text" name="phone" id="phone" placeholder="Enter Your Phone"><br/>
<input type="text" name="x" id="x" placeholder="Enter Your X Percent">
<input type="text" name="xii" id="xii" placeholder="Enter Your XII Percent"><br/>
<input type="text" name="qualify" id="qualify" placeholder="Enter Your Qualifying Exam(Optional)"><br/><br/>
<input type="submit" class="submit" name="" id="submit" value="submit" class="btn btn-primary">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
enquiry.php
<?php
$cname = $_POST['cname'];
$name = $_POST['name'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$x = $_POST['x'];
$xii = $_POST['xii'];
$qualify = $_POST['qualify'];
$sql = "insert into student_enquiry(college_name,name,father,email,phone,x_percent,xii_percent,qualify)values('$cname','$name','$fname','$email','$phone','$x','$xii','$qualify')";
$result = mysqli_query($link,$sql);
if($result == true)
{
echo "<p style='color:green;text-align:center;'>your data has been submitted successfully</p>";
}
else
{
echo "<p style='color:red;text-align:center;'>Error!</p>"
}
?>
In this code I am creating a modal having form inside it and I want to insert form value into database using jquery/ajax. when I clcik on submit button it hide the modal and show nothing like successfull msg or any error msg. So, How can I do this ?please help.
Thank You

Check Below jquery code and html code
$(document).ready(function () {
$(".submit").click(function () {
var formname = "formid"+$(this).attr("data-id");
cname = $("#" + formname + " #cname").val();
name = $("#" + formname + " #name").val();
fname = $("#" + formname + " #fname").val();
email = $("#" + formname + " #email").val();
phone = $("#" + formname + " #phone").val();
x = $("#" + formname + " #x").val();
xii = $("#" + formname + " #xii").val();
qualify = $("#" + formname + " #qualify").val();
$.ajax({
type: "POST",
data: {"cname": cname, "name": name, "fname": fname, "email": email, "phone": phone, "x": x, "xii": xii, "qualify": qualify},
url: "enquiry.php",
success: function (data) {
$("#" + formname + " #msg").html(data);
}
});
});
});
HTML CODE
Enquire</span>
<div class="modal fade" id="myModal<?php echo $id; ?>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div id="msg"></div>
<form method="post" name="formid<?php echo $id; ?>" name="formid<?php echo $id; ?>">
<input type="text" name="name" id="name" placeholder="Enter Your Name">
<input type="text" name="fname" id="fname" placeholder="Enter Your Father's Name"><br/>
<input type="text" name="email" id="email" placeholder="Enter Your Email">
<input type="text" name="phone" id="phone" placeholder="Enter Your Phone"><br/>
<input type="text" name="x" id="x" placeholder="Enter Your X Percent">
<input type="text" name="xii" id="xii" placeholder="Enter Your XII Percent"><br/>
<input type="text" name="qualify" id="qualify" placeholder="Enter Your Qualifying Exam(Optional)"><br/><br/>
<input type="button" class="submit" name="submit" data-id="<?php echo $id; ?>" id="submit" value="submit" class="btn btn-primary">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Give an id to the form element.
<form method="post" id="registration">
Submit the form as follows:
var formdata = jQuery('#registration').serialize();
jQuery.ajax({
type: 'POST',
url: 'enquiry.php',
dataType: 'json',
data: formdata,
success: function(result) {
$("#msg").html(data);
}
});

Try below example for save data using ajax,
JS/CDN
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$("#submit").click(function(){
cname = $("#cname").val();
name = $("#name").val();
fname = $("#fname").val();
email = $("#email").val();
phone = $("#phone").val();
x = $("#x").val();
xii = $("#xii").val();
qualify = $("#qualify").val();
$.ajax({
type:"POST",
data:{"cname":cname,"name":name,"fname":fname,"email":email,"phone":phone,"x":x,"xii":xii,"qualify":qualify},
url:"enquiry.php",
success:function(data){
$("#msg").html(data);
}
});
});
});
</script>
HTML
Enquire</span>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div id="msg"></div>
<form method="post">
<input type="text" name="name" id="name" placeholder="Enter Your Name">
<input type="text" name="fname" id="fname" placeholder="Enter Your Father's Name"><br/>
<input type="text" name="email" id="email" placeholder="Enter Your Email">
<input type="text" name="phone" id="phone" placeholder="Enter Your Phone"><br/>
<input type="text" name="x" id="x" placeholder="Enter Your X Percent">
<input type="text" name="xii" id="xii" placeholder="Enter Your XII Percent"><br/>
<input type="text" name="qualify" id="qualify" placeholder="Enter Your Qualifying Exam(Optional)"><br/><br/>
<input type="button" name="" id="submit" value="submit" class="btn btn-primary">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Ajax File.
In this file you can get posted data and save them.
<?php
// Your mysql query.
$insert = mysql_query("insert into `table_name` set `field1`=''");
if($insert){
echo "Inserted successfully !";
}
else{
echo "Not Inserted !";
}
?>

Related

PHP/AJAX: Insert BLOB into MySQL Database

I have some hard times to save a blob (image file) to my sql-database.
I have three files: a html form (form.php), an ajax script (ajax.js) and a php-file (save.php).
I think i have some errors in my ajax.js, but after a long google search i couldn`t find a good solution. There must be a problem with the blob handling in JS....
HTML Form:
<div id="editEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form id="update_form" enctype="multipart/form-data">
<div class="modal-header">
<h4 class="modal-title">table bearbeiten</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<input type="hidden" id="id_u" name="id" class="form-control" required>
<div class="form-group">
<label>Image</label>
<input type="file" id="image_u" name="image" class="form-control" required>
</div>
<div class="form-group">
<label>Überschrift 1</label>
<input type="text" id="header1_u" name="header1" class="form-control" required>
</div>
<div class="form-group">
<label>Überschrift 2</label>
<input type="text" id="header2_u" name="header2" class="form-control" required>
</div>
<div class="form-group">
<label>Text</label>
<input type="text" id="text_u" name="text" class="form-control" required>
</div>
<div class="form-group">
<label>Link</label>
<input type="text" id="link_u" name="link" class="form-control" required>
</div>
<div class="form-group">
<label>Link-Text</label>
<input type="text" id="linkText_u" name="linkText" class="form-control" required>
</div>
<div class="form-group">
<label>Datum</label>
<input type="date" id="datum_u" name="datum" class="form-control" required>
</div>
</div>
<div class="modal-footer">
<input type="hidden" value="2" name="type">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<button type="button" class="btn btn-info" id="update">Update</button>
</div>
</form>
</div>
</div>
</div>
In form.php i specified all my Inputs and for the image type=file
Ajax Code:
$(document).on('click','.update',function(e) {
var id=$(this).attr("data-id");
var image=$(this).attr("data-image");
var header1=$(this).attr("data-header1");
var header2=$(this).attr("data-header2");
var text=$(this).attr("data-text");
var link=$(this).attr("data-link");
var linkText=$(this).attr("data-linkText");
var datum=$(this).attr("data-datum");
$('#id_u').val(id);
$('#image_u').val(image);
$('#header1_u').val(header1);
$('#header2_u').val(header2);
$('#text_u').val(text);
$('#link_u').val(link);
$('#linkText_u').val(linkText);
$('#datum_u').val(datum);
});
$(document).on('click','#update',function(e) {
var data = $("#update_form").serialize();
$.ajax({
data: data,
enctype: 'multipart/form-data',
type: "post",
url: "save.php",
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$('#editEmployeeModal').modal('hide');
alert('Data updated successfully !');
location.reload();
}
else if(dataResult.statusCode==201){
alert(dataResult);
}
}
});
});
My PHP-Code:
if($_POST['type']==2){
$id=$_POST['id'];
$image= base64_encode(file_get_contents($_FILES['image']['tmp_name']));
$header1=$_POST['header1'];
$header2=$_POST['header2'];
$text=$_POST['text'];
$link=$_POST['link'];
$linkText=$_POST['linkText'];
$datum=$_POST['datum'];
$sql = "UPDATE `tblTable` SET `image`='$image',`header1`='$header1',`header2`='$header2',`text`='$text',`link`='$link',`linkText`='$linkText',`datum`='$datum' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo json_encode(array("statusCode"=>200));
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
}
My Datbase-Connection works fine. I can write and read all other Data (Text and Date) to my local Database. I think my problem is in the ajax.js. How can i specify $('#image_u').val(image); that it must handle a blob file and not just normal text input. And do i have to modify these lines?
var data = $("#update_form").serialize();
$.ajax({
data: data,
enctype: 'multipart/form-data',
type: "post",
url: "save.php",

Update Data with Bootstrap Modal using CodeIgniter and Ajax

I'm trying to figure it out why I can't get the alert warning or success from the form validation in a modal. all I want at first is to update the data in the database and it work. now I can't see the alert whether it is wrong or correct, the modal close after I submit and how can I transfer the echo json_encode to alert in modal
view: layout view
$('#login_form').on('submit', function( event ){
event.preventDefault();
var Admin_id = $('#Admin_id').val();
var username = $('#username').val();
var email = $('#email').val();
var contact = $('#contact').val();
var hoax = $('#hoax').val();
var confirm = $('#confirm').val();
var date = $('#date').val();
$.ajax({
url: "<?php echo base_url('profile/profile_update'); ?>",
method: "POST",
data: {Admin_id:Admin_id, username:username, email:email,
contact:contact, confirm:confirm, date:date, hoax:hoax},
dataType: "json",
success: function(data){
if (data.result){
$('#result').html('<p class="alert-success"> The inputs are insufficient </p>');
}else{
$('#result').html('<p class="alert-warning"> The profile successfully updated</p>');
}
//return false;
//event.preventDefault();
}
});
return false;
//event.preventDefault();
});
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="profile" id="1" href="#Profile_modal" data-toggle="modal">Profile</a>
<div class="modal" id="Profile_modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg modal-dialog-scrollable" >
<div class="modal-content">
<div class="modal-header bg-secondary">
<h5 class="modal-title text-white">Profile</h5>
<button type="button " class="close" data-dismiss="modal" aria-label="Close">
<span class="text-white" aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" >
<div class="text-center" id="result"> </div>
<div id="data_profile"> </div>
</div>
</div>
</div>
</div>
Controller: profile/profile_update
public function profile_update(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[13]');
$this->form_validation->set_rules('contact', 'Contact', 'trim|required|min_length[11]');
$this->form_validation->set_rules('hoax', 'Password',
'trim|required|min_length[6]|matches[confirm]');
$this->form_validation->set_rules('confirm', 'ConfirmPassword', 'trim|required|min_length[6]');
if ($this->form_validation->run() == FALSE) {
$data = array('profile_errors' => validation_errors());
echo json_encode(array("result"=>false));
}else{
$this->load->model('admin_model');
$data=$this->profile_model->update_data();
//$data = 'Your Profile Data has been Updated Successfully';
echo json_encode(array("result"=>true));
}
}
profile/get_fetchdata()
public function get_fetchdata(){
$Admin_id = $this->input->post('Admin_id');
if(isset($Admin_id) and !empty($Admin_id)){
$records = $this->profile_model->fetch_data($Admin_id);
$output = '';
foreach ($records->result_array() as $row) {
$attributes = array('id'=>'login_form', 'class'=>'form');
$output = '<div class="text-center" id="result"> </div>';
echo form_open('profile/profile_update', $attributes);
$output = '
<div class="form-group text-left">
<label> Username </label>
<input type="text" id="username" class="form-control" value='. $row["username"] .' placeholder="Enter Username" name="username" maxlength="10" required>
</div>
<div class="form-group text-left">
<label> Email </label>
<input type="Email" id="email" class="form-control" value='. $row["email"] .' placeholder="Enter Email" name="email" maxlength="20" required>
</div>
<div class="form-group text-left">
<label> Phone Number</label>
<input type="number" id="contact" class="form-control" value='.$row["contact"].' placeholder="Enter Phone Number" name="contact" maxlength="11" required>
</div>
<div class="form-group text-left">
<label> Password </label>
<input type="password" id="hoax" class="form-control" value='. $row["hoax"].' placeholder="Enter Password" name="hoax" maxlength="15" required>
</div>
<div class="form-group text-left">
<label> Confirm Password </label>
<input type="password" id="confirm" class="form-control" value='.$row["hoax"].' placeholder="Confirm Password" name="confirm" maxlength="15" required>
</div>
<input type="hidden" id="Admin_id" class="form-control" value='. $row["Admin_id"] .' name="Admin_id">
<input type="hidden" id="date" class="form-control" value='. $row["date_modified"] .' name="date">
<button class="btn btn-primary float-right new_profile" id="1" name="submit" type="submit" >Save</button>
<button class="btn btn-danger mx-2 float-right" data-dismiss="modal" type="button"> Cancel </button>';
echo form_close();
}
echo $output;
}else {
echo "Nothing to show";
}
}
if you use json as dataType, the browser will wait always for a JSON response, so the best thing to do here is the following:
if($this->form_validation->run() == FALSE){
echo json_encode(array("result" => false, "message" => "Failed"));
}else{
echo json_encode(array("result" => true, "message" => "Success!"));
}
and you can check the results on ajax side by using:
$('#login_form').on('submit', function( event ){
event.preventDefault();
var Admin_id = $('#Admin_id').val();
var username = $('#username').val();
var email = $('#email').val();
var contact = $('#contact').val();
var hoax = $('#hoax').val();
var confirm = $('#confirm').val();
var date = $('#date').val();
$.ajax({
url: "<?php echo base_url('profile/profile_update'); ?>",
method: "POST",
data: {Admin_id:Admin_id, username:username, email:email,
contact:contact, confirm:confirm, date:date, hoax:hoax},
dataType: "json",
success: function(data){
if(data.result){
$('#adminId').val("");
$('#username').val("");
$('#email').val("");
$('#contact').val("");
$('#hoax').val("");
$('#confirm').val("");
$('#date').val("");
$('#result').html('<p class="alert-success"> ' + data.message + ' </p>');
}else{
$('#result').html('<p class="alert-warning"> ' + data.message + ' </p>');
}
//return false;
//event.preventDefault();
}
});
return false;
//event.preventDefault();
});

send emails from phonegap android application

I want to send a feedback form from android PhoneGap application i have used following code which is not working
1) i have used bellow Ajax code and JQuery files to send ajax request and HTML form, i want to send the email of 4 html fields
<script type="text/javascript" src="assets/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.js"></script>
<script type="text/javascript" src="assets/js/geturi.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#send").click(function() {
var fullName = $("#fullName").val();
var emailId = $("#emailId").val();
var mobileNo = $("#mobileNo").val();
var message = $("#message").val();
var dataString = "fullName=" + fullName + "&emailId=" + emailId + "&mobileNo=" + mobileNo + "&message=" + message + "&send=";
if ($.trim(fullName).length > 0 & $.trim(emailId).length > 0 & $.trim(mobileNo).length > 0 & $.trim(message).length > 0) {
$.ajax({
type: "POST",
url: "https://www.activebittechnologies.com/phonegap/mail.php",
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function() {
$("#send").val('Sending Enquiry...');
},
success: function(data) {
if (data == "success") {
alert("Mail Sent");
$("#send").val('submit');
} else if (data == "error") {
alert("error");
}
}
});
}
return false;
});
});
</script>
<div class="content form">
<div class="header">
<div class="header-overlay"></div><img src="assets/banner/5.jpg">
<div class="info">
<h3 class="title">Send Contact Enquiry</h3>
<span data-close="#enquirepop" class="closeit"><i class="fa fa-times" aria-hidden="true"></i></span>
</div>
</div>
<div class="form-group">
<input id="fullName" name="fullName" type="text" class="form-control" placeholder="Full Name">
</div>
<div class="form-group">
<input id="emailId" name="emailId" type="text" class="form-control" placeholder="Email Id">
</div>
<div class="form-group">
<input id="mobileNo" name="mobileNo" type="text" class="form-control" placeholder="Mobile No">
</div>
<div class="form-group">
<textarea class="form-control" id="message" name="message" placeholder="Your Message" style="color:#fff;"></textarea>
</div>
<div class="text-right">
<input type="button" id="send" class="btn btn-primary" value="Send">
</div>
</div>
unable to go on this page from phone gap when installed on android phone bellow is php script which is on server
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
$toEmail = "hotelinkonkan#gmail.com";
$mailHeaders = "From: " . $_POST["fullName"] . "<". $_POST["emailId"] .">\r\n";
$sentml=mail($toEmail, $_POST["fullName"], $_POST["message"], $mailHeaders);
if($sentml)
echo"success";
else
echo"error";
?>
I use $("#form1").serialize() to get all the values from the form, and I validate the values on the server side. You should always validate your values server side, because users can send a direct post to your php without any javascript validation.
Hope it helps.
<script type="text/javascript" src="assets/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.js"></script>
<script type="text/javascript" src="assets/js/geturi.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#send").click(function() {
$.ajax({
type: "POST",
url: "https://www.activebittechnologies.com/phonegap/mail.php",
data: $("#form1").serialize(),
crossDomain: true,
cache: false,
beforeSend: function() {
$("#send").val('Sending Enquiry...');
},
success: function(data) {
console.log(data);
if (data == "success") {
alert("Mail Sent");
$("#send").val('submit');
} else if (data == "error") {
alert("error");
}
}
});
return false;
});
});
</script>
<form id='form1' >
<div class="content form">
<div class="header">
<div class="header-overlay"></div><img src="assets/banner/5.jpg">
<div class="info">
<h3 class="title">Send Contact Enquiry</h3>
<span data-close="#enquirepop" class="closeit"><i class="fa fa-times" aria-hidden="true"></i></span>
</div>
</div>
<div class="form-group">
<input id="fullName" name="fullName" type="text" class="form-control" placeholder="Full Name">
</div>
<div class="form-group">
<input id="emailId" name="emailId" type="text" class="form-control" placeholder="Email Id">
</div>
<div class="form-group">
<input id="mobileNo" name="mobileNo" type="text" class="form-control" placeholder="Mobile No">
</div>
<div class="form-group">
<textarea class="form-control" id="message" name="message" placeholder="Your Message" style="color:#fff;"></textarea>
</div>
<div class="text-right">
<input type="button" id="send" class="btn btn-primary" value="Send">
</div>
</div></form>

Ajax request is retrieving an empy array in PHP

I have a "form" with this code:
<div class="form-wrap" id="contact-form">
<div class="form-innerwrap">
<div class="text-input">
<input type="text" name="name" id="name" required />
<label for="name"><?= CONTACT_FORM_NAME ?></label>
</div>
<div class="text-input">
<input type="email" name="email" id="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}$" required />
<label for="email">E-mail</label>
</div>
<div class="text-input">
<input type="text" name="contact" id="contact" required />
<label for="name"><?= CONTACT ?></label>
</div>
<div class="text-input text-textarea">
<textarea type="info" id="info" name="info">
</textarea>
<label for="email"><?= CONTACT_FORM_MENSSAGE ?></label>
</div>
<input type="hidden" name="lang" id="lang" value="<?= $_SESSION['language'] ?>">
<div class="form-button">
<button type="submit" class="btn contacts-button btn-gowe" id="btn-info" data-btnhover="<?= CONTACT_FORM_BTN ?>"> <span><?= CONTACT_FORM_BTN ?></span> </button>
</div>
</div>
</div>
Then I have a ajax request to sent the input values to php:
$("#btn-info").click(function(){
var name = $("#name").val();
var email = $("#email").val();
var contact = $("#contact").val();
var info = $("#info").val();
var lang = $("#lang").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'name='+ name + '&email='+ email +'&contact='+contact+'&info='+info+'&lang='+lang;
$.ajax({
type: "POST",
url: "includes/sendEmail.php",
data: dataString,
cache: false,
success: function(result){
var result = result.split("||");
if (result[0]== "true"){
$("#contact-modal").show();
}
}
});
return false;
});
However when I make a var_dump of $REQUEST the return is an empty array.
Can anyone help me with this problem?
I had already spent several hours to try to understand what is wrong in my code.
Thanks for helping.

Can not remove selected select field in bootstrap modal after jquery ajax request to php

after jQuery ajax request with bootstrap modal, i want make the form empty .
This is my code for getting data from modal and send it server with Jquery Ajax.
Before toggling the modal, i used val() to make fields empty and prop() to deselect, selected radio button and Select Element.
HTML Code of Modal
<div id="addEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form id="AddUserForm">
<div class="modal-header">
<h4 class="modal-title">Add User</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>First Name</label>
<input type="text" id="first_name" name="first_name" class="form-control" required>
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" id="last_name" name="last_name" class="form-control" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" id="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" id="phone" name="phone" class="form-control" required>
</div>
<div class="form-group">
<center>
<label class="radio-inline">
<input type="radio" id="gender" name="gender" value="1" required>Male
</label>
<label class="radio-inline">
<input type="radio" id="gender" name="gender" value="0" required>Female
</label>
</center>
<!--<input type="radio" id="gender" name="gender" class="form-control" required>
<input type="radio" id="gender" name="gender" class="form-control" required>-->
</div>
<div class="form-group">
<label>Type</label>
<select class="selectpicker form-control show-tick" data-live-search="true" title="Select Please" id="role" name="role">
<option value="student">Student</option>
<option value="teacher">Teacher</option>
<option value="clerk">Clerk</option>
<option value="lab_staff">Lab Staff</option>
<option value="admin">Admin</option>
</select>
<!-- <textarea class="form-control" required></textarea>-->
</div>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" id="InsertSubmit" class="btn btn-success" value="Add">
</div>
</form>
</div>
</div>
</div>
JQuery Code
<script>
$( document ) . ready( function () {
$( "#AddUserForm" ) . on( 'submit', function () {
var first_name = $( "#first_name" ) . val();
var last_name = $( "#last_name" ) . val();
var email = $( "#email" ) . val();
var phone = $( "#phone" ) . val();
var gender = $( "#gender" ) . val();
var role = $( "#role" ) . val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'first_name=' + first_name + '&last_name=' + last_name + '&email=' + email + '&phone=' + phone + '&gender=' + gender + '&role=' + role;
// AJAX Code To Submit Form.
$ . ajax( {
type: "POST",
url: "add_user.php",
data: dataString,
cache: false,
success: function ( result ) {
$("#addEmployeeModal #first_name").val('');
$("#addEmployeeModal #last_name").val('');
$("#addEmployeeModal #email").val('');
$("#addEmployeeModal #phone").val('');
$("#addEmployeeModal #gender").prop('checked', false);
$("#addEmployeeModal #role option").prop('selected', false);
$( '#addEmployeeModal' ) . modal( 'toggle' );
toastr . options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr[ "success" ]( "User Added" );
}
} );
return false;
} );
} );
</script>
Now everything works fine, but the Selected field having id="role" does not geting cleared .
what should i do now.

Categories