Input fields are empty after .submit jquery statement - php

I'm trying to send data using an ajax call inside of a ('#form').submit call but all the inputs of the forms are empty when I didn't intend them to be. I thought it may of had something to do with the $(document).ready statement being in the incorrect location for things to work properly but I couldn't get it to work.
<script>
(function($){
$(document).ready( function() {
$("#success").hide();
$("#inval_email").hide();
$("#text_selection").on("change", function() {
var id = $("#text_selection option:selected").attr("value");
$("#inv_text").html($("#"+id).html());
});
$("#invitation_form").submit(function(e){
var email = $('#email').val();
var first_name = $('#first_name').val();
var last_name = $('#last_name').val();
var message_title = $('#message_title').val();
var article_title = $('#article_title').val();
var reference_location = $('#reference').val();
var inv_text = $('#inv_text').html();
var request;
alert(first_name);
e.preventDefault();
request = $.ajax({
type: 'POST',
url: BASE_URL+"wp-content/themes/Flatter/ajax/send_invite.php",
dataType: "text",
data: { first_name: first_name, last_name: last_name, email: email, message_title: message_title, article_title: article_title, reference_location: reference_location, inv_text: inv_text },
success: function(data){
console.log(data)
if(data=="true"){
$("#invitation").hide();
$("#success").show();
}
if(data=="false"){
$("#inval_email").show();
}
}
});
});
});
})(jQuery);
</script>
<div class="col-md-4">
<div id="success" hidden><br><br>Invitation sent successfully!</div>
<div id="invitation">
<br><br><br></br><h1>Invitation Form</h1>
<form id = "invitation_form">
First Name:<input id="first_name" type="text" name="first_name" required>
Last Name:<input id="last_name" type="text" name="last_name" required>
Email Address:<input id="email" type="text" name="email" required>
Message Title:<input id="message_title" type="text" name="message_title" required>
Article Title:<input id="article_title" type="text" name="article_title" required>
Reference Location:<input id="reference" type="text" name="reference" required>
Message:<textarea id="inv_text" style="resize: none" rows="10" placeholder="Select invitation type..." readonly required></textarea>
Invitation Type:
<select id="text_selection">
<option value="empty_field"></option>
<option value="community_inv_text">Community Invitation</option>
<option value="content_submission_inv" >Content Submission Invitation</option>
<option value="individual_inv">Individual Invitation</option>
<option value="content_submission_and_individual_inv">Content Submission and Individual Invitation</option>
<option value="contributor_content_submission_inv">Contributor Content Submission Invitation</option>
</select>
<input id="submit_inv" type="submit" value="Invite">
</form>
I would greatly appreciate some help in this matter. The point of this code is to collect information from the user and send the data to another file so it can be processed. Thanks for any help in advance.

I figured it out. There was a CSS file that was modifying elements under the same name as the ones I am using in my file (i.e. first_name, last_name, email, etc.).

Related

how do I make two phpmailer contact forms work on the same page

using code from here
https://www.codingsnow.com/2021/01/create-php-send-email-contact-form.html
<h4 class="sent-notification"></h4>
<form id="myForm">
<h2>Send an Email</h2>
<label>Name</label>
<input id="name" type="text" placeholder="Enter Name">
<br><br>
<label>Email</label>
<input id="email" type="text" placeholder="Enter Email">
<br><br>
<label>Subject</label>
<input id="subject" type="text" placeholder=" Enter Subject">
<br><br>
<p>Message</p>
<textarea id="body" rows="5" placeholder="Type Message"></textarea><!--textarea tag should be closed (In this coding UI textarea close tag cannot be used)-->
<br><br>
<button type="button" onclick="sendEmail()" value="Send An Email">Submit</button>
</form>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function sendEmail() {
var name = $("#name");
var email = $("#email");
var subject = $("#subject");
var body = $("#body");
if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) {
$.ajax({
url: 'sendEmail.php',
method: 'POST',
dataType: 'json',
data: {
name: name.val(),
email: email.val(),
subject: subject.val(),
body: body.val()
}, success: function (response) {
$('#myForm')[0].reset();
$('.sent-notification').text("Message Sent Successfully.");
}
});
}
}
function isNotEmpty(caller) {
if (caller.val() == "") {
caller.css('border', '1px solid red');
return false;
} else
caller.css('border', '');
return true;
}
</script>
what do i need to change in both the files for it to work
right now when i put two contact forms on one page, both top working even though individually both of them are working..I have tried changes variable names and function names but cant figure out the error. in the network tab it says "failed, something went wrong"
I'll explain it with an example. Let's say you have a function to add up two numbers:
function add() {
return 3 + 5;
}
If you ever need to add a different set of numbers, this function is useless. However, if you pass variable information as function argument you have a multiple purpose function:
function add(a, b) {
return a + b;
}
As about assigning IDs in HTML, it adds more burden than benefits. Compare these two snippets and figure out which one is easier to use and extend:
jQuery(function ($) {
$("#input1, #input2, #input3").each(function (index, input) {
console.log(input.value);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input id="input1" value="One">
<input id="input2" value="Two">
<input id="input3" value="Three">
</form>
jQuery(function ($) {
$("input").each(function (index, input) {
console.log(input.value);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input value="One">
<input value="Two">
<input value="Three">
</form>
This is just an example to illustrate the point. The usual way to handle a form element is to assign it a name rather than an ID.

Ajax multipart/form-data method gives error

Am getting a error while uploading multipart/form-data form, i have two input getting one image and pdf.
here is html code
<form class="form-group" id="form-add" >
<input name="title" size="30" class="form-control margin-bottom-25" id="title" placeholder="Enter Title" spellcheck="true" autocomplete="off" type="text" />
<input name="code" size="30" class="form-control margin-bottom-25" id="code" placeholder="Enter Course Code" spellcheck="true" autocomplete="off" type="text" />
<div class="wp-media-buttons"><input type="file" id="image_select" name="image" class="form-control margin-bottom-50" ></div>
<div class="wp-media-buttons"><input type="file" name="pdf" id="pdf_select" class="form-control margin-bottom-50" ></div>
<div class="col-md-6"><input type="text" id="batch" name="batch" class="form-control margin-bottom-25" placeholder="Enter Batch Size" /></div>
<div class="margin-bottom-25 col-md-6"><select id="batch-dec" name="batch-dec" class="form-control margin-bottom-25">
<option >Select</option>
<option value="Batch Size">Batch Size</option>
<option value="Field Work">Field Work</option>
<option value="Project">Project</option>
</select></div>
<div class=" col-md-6">
<input type="text" id="duration" name="duration" class="form-control margin-bottom-25" spellcheck="true" placeholder="Enter Duration"/></div>
<div class="col-md-6">
<select id="duration-dec" name="duration-dec-dec" class="form-control margin-bottom-25">
<option >Select</option>
<option value="Time Duration">Time Duration</option>
<option value="Class Room">Field Work</option>
</select>
</div>
<div class="clearfix"></div>
<div class="margin-bottom-25">Add Course Description Below</div>
<div id="editor-container" class="editor-container margin-bottom-25" style="position:relative">
<textarea spellcheck="true" class="editor-area form-control margin-bottom-25" style="height: 326px; margin-top: 37px;" cols="40" name="content" id="content" aria-hidden="false"></textarea>
</div>
<input class="btn btn-primary publish" type="submit" name="submit" id="publish" value="Publish" />
</form>
Ajax code
$(document).ready(function(){
$(document).on('click', '#publish', function(){
/*var code = $('#code').val();
var title= $('#title').val();
var image= $('#image_select').files[0];
var pdf= $('#pdf_select').files[0];
var batch = $('#batch').val();
var batch_dec = $('#batch-dec').val();
var duration = $('#duration').val();
var duration_dec = $('#duration-dec').val();
var content = $('#content').val();
if($(document).on('click','#draft')){
var status = $('#draft').val();
}
else if(($(document).on('click','#publish'))){
var status =$('#publish').val();
}
var status =$('#publish').val();
var formdata = new FormData();
formdata.append('code',code);
formdata.append('title',title);
formdata.append('image',image);
formdata.append('pdf',pdf);
formdata.append('batch',batch);
formdata.append('batch_dec',batch_dec);
formdata.append('duration',duration);
formdata.append('duration_dec',duration_dec);
formdata.append('content',content);
formdata.append('status',status);*/
//alert(formdata);
var formdata = new FormData($('#form-add')[0]);
formdata.append('image',$('#image_select')[0].files[0] );
formdata.append('pdf',$('#pdf_select')[0].files[0]);
$.ajax({
url:"include/courses-add.php",
// method:"POST",
type:'post',
enctype:'multipart/form-data',
data:formdata,
contentType:false,
cache:false,
processData: false,
success:function(data){
alert(data);
$('#result').html("<div class='alert alert-success'>"+data+"</div>");
} ,
error:function(e)
{
console.log('error:',e);
}
});
});
});
I Used same method separately adding files and other data it working fine, but i need to post both data in same form. get a console.log like
Error:
Object { readyState: 0, getResponseHeader: getResponseHeader(), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), then: then(), … }
Can seen in JavaScript tried many code still it giving error, post just submitting but
ajax block is not working, it's throwing an error in console.
Use ajaxSubmit that is easy:
https://github.com/claviska/jquery-ajaxSubmit
$('#postreportform').on('submit', function (e) {
var postreportform = $(this);
e.preventDefault();
$(this).ajaxSubmit(
{
success: function (data) {
.....
},
error: function (jqXhr) {
.....
}
});
});
$("#form-add").on('submit', (function(e) {
e.preventDefault();
var code = $('#code').val();
var title= $('#title').val();
var image= $('#image_select').files[0];
var pdf= $('#pdf_select').files[0];
var batch = $('#batch').val();
var batch_dec = $('#batch-dec').val();
var duration = $('#duration').val();
var duration_dec = $('#duration-dec').val();
var content = $('#content').val();
var status =$('#publish').val();
var formdata = new FormData(this);
formdata.append('code',code);
formdata.append('title',title);
formdata.append('image',image);
formdata.append('pdf',pdf);
formdata.append('batch',batch);
formdata.append('batch_dec',batch_dec);
formdata.append('duration',duration);
formdata.append('duration_dec',duration_dec);
formdata.append('content',content);
formdata.append('status',status);
$.ajax({
url:"include/courses-add.php",
// method:"POST",
type:'post',
enctype:'multipart/form-data',
data:formdata,
contentType:false,
cache:false,
processData: false,
success:function(data){
alert(data);
$('#result').html("<div class='alert alert-success'>"+data+"</div>");
} ,
error:function(e)
{
console.log('error:',e);
}
});
}));

How to pass a value from input-text through Jquery Ajax to php function while I type? Wordpress

I am using Wordpress, and here is my html code:
<input class="form-control text-right" name="majorhead" required="" type="number" id="majorhead"/>
<div class="result_majorhead"></div>
JS code:
<script>
jQuery("#majorhead").on("keyup",function(){
jQuery.get(ajaxurl,{'action': 'majorhead'},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
</script>
Here is my code in function.php
add_action('wp_ajax_nopriv_majorhead', 'majorhead_function');
add_action('wp_ajax_majorhead', 'majorhead_function');
function majorhead_function(){
echo 'hello';
exit();
}
For now I am able to display a simple message "Hello" in the same web page whatever I type in the input box. Now I want to display the exact value from the input-text in the display message while typing.
for example: if I type 1, it should display 1 and If I type another another 2 then it should display 12.
What you need to do in your Javascript is:
Add "change" event so that you could use the arrows to control the "number" input.
Get the value of the input.
Add the value to your GET parameters.
jQuery("#majorhead").on("keyup change",function(){
var majorhead_value = $(this).val();
jQuery.get(ajaxurl,{'action': 'majorhead','majorhead_value': majorhead_value},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
In your PHP script you should display the GET value:
function majorhead_function(){
echo $_GET['majorhead_value'];
exit();
}
If you have multiple inputs you may process them like this:
jQuery("#text1, #text2, #text3").on("keyup change",function(){
var majorhead_value = parseInt(0+$("#text1").val());
majorhead_value += parseInt(0+$("#text2").val());
majorhead_value += parseInt(0+$("#text3").val());
jQuery.get(ajaxurl,{'action': 'majorhead','majorhead_value': majorhead_value},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
I assume that your HTML will look like this:
<input class="form-control text-right" name="text1" required="" type="number" id="text1"/>
<input class="form-control text-right" name="text2" required="" type="number" id="text2"/>
<input class="form-control text-right" name="text3" required="" type="number" id="text3"/>
<div class="result_majorhead"></div>
HTML CODE
<input class="form-control" placeholder="Search Service" id="search_service" name="service" type="text" autocomplete="off" value="">
Here is my Ajax Function in function.php
function search_service() {
$search_ser = $_POST['search_ser'];
echo $search_ser;
die();
}
add_action('wp_ajax_search_service', 'search_service');
add_action('wp_ajax_nopriv_search_service', 'search_service');
JS Code:
jQuery( "#search_service" ).keyup( function() {
var search_ser = jQuery(this).val();
var ajax_url = jQuery('#ajax_url_input').val();
jQuery.ajax({
type:'POST',
url: ajax_url,
data: {
action: 'search_service',
search_ser: search_ser,
},
beforeSend: function(){
},
success:function(data){
jQuery("._services_list").html(data);
}
});
});

Undefined result from Ajax

I am passing some form variables through to a php page using Ajax, however when the php code runs the table rows are filled with the value undefined.
I have checked the php code, substituting the form variables and that works fine and so i am thinking that the problem is with the Ajax code,
AJAX
$(document).ready(function(){
$('form.submit').submit(function () {
var name = $(this).find('.name').attr('value');
var address = $(this).find('.address').attr('value');
var number = $(this).find('.number').attr('value');
var price = $(this).find('.price').attr('value');
var deposit = $(this).find('.deposit').attr('value');
var product = $(this).find('.product').attr('value');
var payment_type = $(this).find('.payment_type').attr('value');
var deal_date = $(this).find('.deal_date').attr('value');
var install_date = $(this).find('.install_date').attr('value');
var installed = $(this).find('.installed').attr('value');
var notes = $(this).find('.notes').attr('value');
var contract_received = $(this).find('.contract_received').attr('value');
// ...
$.ajax({
type: "POST",
url: "add.php",
data: "name="+ name +"& address="+ address +"& number="+ number +"& price="+ price +"& deposit="+ deposit +"& product="+ product +"& payment_type="+ payment_type +"& deal_date="+ deal_date +"& install_date="+ install_date +"& installed="+ installed +"& notes="+ notes +"& contract_received="+ contract_received,
success: function(){
$('form.submit').hide(function(){$('div.success').fadeOut();});
}
});
return false;
});
});
HTML
<form id="submit" name="submit" class="submit">
<input name="name" id="name" type="text" class="form-control" placeholder="Name"/><br />
<input name="address" id="address" type="text" class="form-control" placeholder="Address"/><br />
<input name="number" id="number" type="text" class="form-control" placeholder="Number"/><br />
<input name="price" id="price" type="text" class="form-control" placeholder="Price"/><br />
<input name="deposit" id="deposit" type="text" class="form-control" placeholder="Deposit"/><br />
<input name="product" id="product" type="text" class="form-control" placeholder="Product"/><br />
<input name="payment_type" id="payment_type" type="text" class="form-control" placeholder="Payment"/><br />
<input name="deal_date" id="deal_date" type="text" class="form-control" placeholder="Deal Date"/><br />
<input name="install_date" id="install_date" type="text" class="form-control" placeholder="Install Date"/><br />
<input name="installed" id="installed" type="text" class="form-control" placeholder="Installed"/><br />
<textarea name="notes" id="notes" cols="" rows="" class="form-control" placeholder="Notes"></textarea><br />
<input name="contract_received" id="contract_received" type="text" class="form-control" placeholder="Contract Received"/><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
PHP
$name = htmlspecialchars(trim($_POST['name']));
$address = htmlspecialchars(trim($_POST['address']));
$number = htmlspecialchars(trim($_POST['number']));
$price = htmlspecialchars(trim($_POST['price']));
$deposit = htmlspecialchars(trim($_POST['deposit']));
$product = htmlspecialchars(trim($_POST['product']));
$payment_type = htmlspecialchars(trim($_POST['payment_type']));
$deal_date = htmlspecialchars(trim($_POST['deal_date']));
$install_date = htmlspecialchars(trim($_POST['install_date']));
$installed = htmlspecialchars(trim($_POST['installed']));
$notes = htmlspecialchars(trim($_POST['notes']));
$contract_received = htmlspecialchars(trim($_POST['contract_received']));
$addClient = "INSERT INTO DATA (
name, address,number,price,deposit,product,payment_type,deal_date,install_date,installed,notes,contract_recieved)VALUES('$name','$address','$number','$price','$deposit','$product','$payment_type','$deal_date','$installed_date','$installed','$notes','$contract_received')";
mysql_query($addClient) or die(mysql_error());
You are making it very hard for yourself. There is no need to get all individual values from the form if you are sending the whole form. But if you do, you need to encode each value correctly using encodeURIComponent(). And don't send any spaces in your query string either.
The easiest solution is to have jQuery serialize your form and send that:
$.ajax({
type: "POST",
url: "add.php",
data: $('form.submit').serialize(), // or just: data: $(this).serialize(),
success: function(){
$('form.submit').hide(function(){$('div.success').fadeOut();});
}
});
Now all key-value pairs from the form will be sent correctly and jQuery will also take care of the encoding for you.
The correct syntax is http://api.jquery.com/jQuery.ajax/
data: {name: name, address: address, number: number}
and so on.

Laravel 5.2 - ajax check if value exists in database

I am creating an employee hierarchy and while setting up the superior for new employee I would like to check if the employee already exists in database ... but :) I would like to do it with AJAX to know it realtime without sending the form ..
I have absolutely no idea how to do it, since I am a newbie to Laravel ..
***UPDATED BASED ON ADVICES:***
I have a form in add_emp.blade.php:
<form action="../create_employee" method="POST">
<button class="button" type="submit" style="float:right"><span>Save</span></button>
<div style="clear:both"></div>
<fieldset>
<legend>Personal data</legend>
<label for="first_name">First name:</label><input type="text" class="add_emp required" name="first_name" value="" /><br />
<label for="last_name">Last name:</label><input type="text" class="add_emp required" name="last_name" value="" /><br />
<label for="superior">Superior:</label><input type="text" class="add_emp" name="superior" value="" id="superior_list" /><br />
</fieldset>
</form>
Here is a script in add_employee.blade.php
<script type="text/javascript">
$('#superior_list').blur(function(){
var first_name = $('#superior_list');
$.ajax({
method: "POST",
url: '/check_superior',
data: { superior: superior }
})
.done(function( msg ) {
if(msg == 'exist') {
//employee exists, do something...
alert( "good." );
} else {
//employee does not exist, do something...
alert( "bad." );
}
});
})
</script>
route for handling the superior:
Route::post('check_superior', 'EmployeeController#check_superior');
This is the Controller function check_superior:
public function check_superior(Request\AjaxUserExistsRequest $request){
if(Employee::where('superior','=',$request->input('superior'))->exists()){
return "exist";
}else{
return "not exist";
}
}
But still not working ... can you advice where could be the issue?
*** FINAL SOLUTION ***
Form:
<form action="../create_employee" method="POST">
<button class="button" type="submit" style="float:right"><span>Save</span></button>
<div style="clear:both"></div>
<fieldset>
<legend>Personal data</legend>
<label for="first_name">First name:</label><input type="text" class="add_emp required" name="first_name" value="" /><br />
<label for="last_name">Last name:</label><input type="text" class="add_emp required" name="last_name" value="" /><br />
<label for="superior">Superior:</label><input type="text" class="add_emp" name="superior" value="" id="superior_list" /><span id="check-superior-status"></span><br />
</fieldset>
</form>
Add to app.blade.php
meta name="csrf-token" content="{{ csrf_token() }}"
Controller
public function check_superior(Request $request){
if(Employee::where('first_name','=',$request->input('superior_fname'))
->where('last_name','=',$request->input('superior_lname'))
->exists()){
return "exist";
}else{
return "not exist";
}
}
final emp.blade.php AJAX script
// place data after SEPERIOR selection
$( "#superior_list" ).blur(function() {
var sup_list = $(this).val();
var sup_arr = sup_list.split(' ');
var superior_fname = sup_arr[0];
var superior_lname = sup_arr[1];
var superior = superior_fname+" "+superior_lname;
// control print out
//$('#check-superior-status').text(superior);
// get real data
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
method: "POST",
url: '/check_superior',
data: { superior_fname: superior_fname, superior_lname: superior_lname },
/* // debug only
error: function(xhr, status, error){
$('#check-superior-status').text(xhr.responseText);
},
*/
success: function(data){
$('#check-superior-status').text(data);
}
})
});
This works like a charm :) thank you guys .. hope this will help someone ..
First make the request.
php artisan make:request AjaxUserExistsRequest
Then open the request file (App\Http\Requests) and find the following:
public function validate(){
return [
//rules
];
}
This is where you would stick your validation rules so you can check against the form elements being submit.
Then you should use dependency injection to force your request into the first argument of the user_exists() function:
public function user_exists(Requests\AjaxUserExistsRequest $request){
return User::where('first_name', $request->first_name)->first();
}
This will return nullif no user exists, otherwise we don't care about the response.
Finally, of course we need our route.
Route::post('employee_exists', 'EmployeeController#user_exists');
Lastly, we'll go ahead and capture the form submit and check if the user exists with our jQuery.
$('#employee_form').submit(function(e){
e.preventDefault();
var first_name = $('#first_name').val(),
$this = this; //aliased so we can use in ajax success function
$.ajax({
type: 'POST',
url: '/employee_exists',
data: {first_name: first_name},
success: function(data){
if(data == null){
//then submit the form for real
$this.submit; //doesn't fire our jQuery's submit() function
} else {
//show some type of message to the user
alert('That user already exists!');
}
}
});
});
The below will give alert message the user already exists! if the first_name exists in your db or it will give alret nothing.(if you want to check with superior change the code vice versa)
first make sure you have jquery.min.js in your public folder.
Now in blade.php add id for first_name, last_name, and superior as below:
<form action="../create_employee" method="POST">
<button class="button" type="submit" style="float:right"><span>Save</span></button>
<div style="clear:both"></div>
<fieldset>
<legend>Personal data</legend>
<label for="first_name">First name:</label><input type="text" id="first_name" class="add_emp required" name="first_name" value="" /><br />
<label for="last_name">Last name:</label><input type="text" id="last_name" class="add_emp required" name="last_name" value="" /><br />
<label for="superior">Superior:</label><input type="text" class="add_emp" name="superior" value="" id="superior_list" /><br />
</fieldset>
</form>
<script>
$(document).ready(function(){
$("#superior_list").blur(function(){
var first_name = $('#first_name').val();
var last_name = $('#last_name').val();
var superior = $('#superior_list').val();
$.ajax({
type: 'POST',
url: '/check_superior',
data: {first_name: first_name, last_name: last_name, superior: superior},
success: function(data){
if(data == 0){
alert('nothing');
} else {
alert('the user already exists!');
}
}
});
});
});
</script>
and in your route.php
Route::post('/check_superior', array('as' => '', 'uses' => 'EmployeeController#check_superior'));
in EmployeeController.php
public function check_superior(){
// can get last_name, superior like first_name below
$first_name = Input::get('first_name');
$data = YourModel::where('first_name',$first_name)->get();
return count($data);
}
It should work. if it doesn't please show us your error
Give your form an id:
<form action="../create_employee" method="POST" id="employee_form">
<button class="button" type="submit" style="float:right"><span>Save</span></button>
<div style="clear:both"></div>
<fieldset>
<legend>Personal data</legend>
<label for="first_name">First name:</label><input type="text" class="add_emp required" name="first_name" id="first_name" value="" /><br />
<label for="last_name">Last name:</label><input type="text" class="add_emp required" name="last_name" value="" /><br />
<label for="superior">Superior:</label><input type="text" class="add_emp" name="superior" value="" id="superior_list" /><br />
</fieldset>
</form>
your js will look like this
$('#employee_form').submit(function(e){
e.preventDefault();
var first_name = $('#first_name');
$.ajax({
method: "POST",
url: "checkUserExistence.php",
data: { first_name: first_name }
})
.done(function( msg ) {
if(msg == 'exist') {
//employee exists, do something...
} else {
//employee does not exist, do something...
}
});
})
also add csrf_field in your form to generate token, and use this token while sending request.
in your form:
{{ csrf_field() }}
in your ajax request:
$.ajax({
headers: {'X-CSRF-Token': $('input[name="_token"]').val()},
//other data....
})
you can also do it with meta teg. in your head teg
<meta name="csrf-token" content="{{ csrf_token() }}">
in your request
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content');
//other data...
}
});

Categories