I am sending form parameters using serialize() method but the problem is parameters sent are visible in address bar. Even I am using POST method for AJAX request.
Here is the HTML Code for the form I am try to submit.
<form id="addform" method="post">
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputReminder">Reminder Type</label>
<select id="inputReminder" class="form-control" name="inputReminder">
<option value="I">Reminder I</option>
<option value="II">Reminder II</option>
<option value="III">Reminder III</option>
<option value="General">General</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputLetterref">Reminder Reference No.</label>
<input type="text" class="form-control" id="inputLetterref" name="inputLetterref" placeholder="Reminder Reference No." required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputLetterdate">Reminder Date</label>
<input type="text" readonly="readonly" class="form-control sel-date" id="inputLetterdate" name="inputLetterdate" placeholder="Reminder Date" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<button type="submit" class="btn btn-primary">Add Reminder </button>
</div>
</div>
</form>
jQuery AJAX request:
$("#addform").submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'submitdetails.php',
data: $(this).serialize()
}).done(function(data) {
$('#result').html(data);
}).error(function(data) {
$('#result').html(data);
});
});
You can try:
$("#addform").submit(function(event){
event.preventDefault();
$.ajax({
type: 'POST',
url : 'submitdetails.php',
data: $(this).serialize(),
done: function(data){
console.log(data)
$('#result').html(data);
},
fail: function(data){
$('#result').html(data);
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="addform" method="post">
<div class="form-row" id="reminderresult">
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputReminder">Reminder Type</label>
<select id="inputReminder" class="form-control" name="inputReminder">
<option value="I">Reminder I</option>
<option value="II">Reminder II</option>
<option value="III">Reminder III</option>
<option value="General">General</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputLetterref">Reminder Reference No.</label>
<input type="text" class="form-control" id="inputLetterref" name="inputLetterref" placeholder="Reminder Reference No." required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputLetterdate">Reminder Date</label>
<input type="text" readonly="readonly" class="form-control sel-date" id="inputLetterdate" name="inputLetterdate" placeholder="Reminder Date" required>
</div>
</div>
<button type="submit" class="btn btn-primary">Add Reminder </button>
</form>
Related
I'm having some troble with AJAX call to update a page after insert the info in the DB.
I update the form, but i need to update the form after the update.
Need some help, i'm not so good whit javascript.
Thks!
<script>
function chk()
{
var nome=document.getElementById('nome').value;
var dataString= 'nome='+ nome;
$.ajax({
type:"post",
url: default,
data:dataString,
cache:false
//success: function(html){
// $('#msg').html(html);
//}
});
return false;
}
</script>
All the code i use, it's not final and not uptimized, its a form, update the field, and i want to update with the changes, without make a page refresh
<div class="col-md-12 personal-info">
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label class="col-lg-4 control-label">Nome:</label>
<div class="col-lg-8">
<input class="form-control" type="text" id="nome" name="nome" value="<?php echo name($resultado); ?>">
<!--<input class="form-control" type="text" value="<?php $nome; ?>">-->
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Apelido:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo apelido($chamada); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Morada:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo morada(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Código Postal:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo codigopostal(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Localidade:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo localidade(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo email(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Username:</label>
<div class="col-md-8">
<input class="form-control" type="text" value="<?php echo $current_user->user_login ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password:</label>
<div class="col-md-8">
<input class="form-control" type="password" value="11111122333">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm password:</label>
<div class="col-md-8">
<input class="form-control" type="password" value="11111122333">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-8">
<input type="submit" name="submit" class="btn btn-primary" value="Save Changes" onclick="return chk()">
<!--<input type="submit" name="submit" class="btn btn-primary" value="Save Changes">-->
<span></span>
<input type="reset" name="reset" class="btn btn-default" value="Cancel">
</div>
</div>
</form>
</div>
</div>
</div>
<hr>
<!--teste final-->
<?php
global $wpdb;
global $seconddb;
global $current_user;
if ($_POST['submit']){
$new = $_POST['nome'];
$query = $seconddb->query("UPDATE {$wpdb->prefix}sgc_socios SET nome='$new' WHERE email = '{$current_user->user_email}' ");
}
?>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
function chk()
{
var nome=document.getElementById('nome').value;
var dataString= 'nome='+ nome;
$.ajax({
type:"post",
url: default,
data:dataString,
cache:false
//success: function(html){
// $('#msg').html(html);
//}
});
return false;
}
</script>
Thks for the help!
i have been working on some project and getting some experience on php and ajax, jquery. So i have checked 20+ question on stackoverflow but couldnt find the solution even if its already answered the same question.
I have tryed 10 diffrent solution but i guess im skipping something.
I have simple select box and im using ajax to send mysql but only select box doesnt work for some reason.
HTML Code:
<section class="resume-section p-3 p-lg-5 d-flex flex-column" id="rezervasyon">
<div class="my-auto">
<h2 class="mb-5">Online Rezervasyon</h2>
<form id="loginForm" method="" action="" novalidate="novalidate">
<div class="form-group row">
<label for="rez_ad" class="col-sm-2 col-form-label">İsim Soyisim</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_ad" id="rez_ad" placeholder="İsim Soyisim">
</div>
</div>
<div class="form-group row">
<label for="rez_saat" class="col-sm-2 col-form-label">Rezervasyon Saati</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_saat" id="rez_saat" placeholder="Rezervasyon saati">
</div>
</div>
<div class="form-group row">
<label for="rez_gsm" class="col-sm-2 col-form-label">Cep Numaranız</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="rez_gsm" id="rez_gsm" placeholder="İletişim numaranız.">
</div>
</div>
<div class="form-group row">
<label for="rez_tarih" class="col-sm-2 col-form-label">Tarih</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="rez_tarih" id="rez_tarih" placeholder="Tarih belirtiniz.">
</div>
</div>
<div class="form-group row">
<label for="rez_email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="rez_email" id="rez_email" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="rez_tip" class="col-sm-2 col-form-label" name="rez_tip" id="rez_tip">Rezervasyon Tipi</label>
<div class="col-sm-10">
<select id="rez_tip" class="custom-select">
<option value="" selected>Rezervasyon Tipi Seçiniz</option>
<option value="1">Cuma Fix Menü</option>
<option value="2">Cumartesi Fix Menü</option>
<option value="3">Haftaiçi ALKOLSÜZ</option>
<option value="4">Haftaiçi ALKOLLÜ</option>
<option value="5">Kutlama / Doğum Günü</option>
<option value="6">Diğer</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="rez_sayi" class="col-sm-2 col-form-label" id="rez_sayi" name="rez_sayi">Kişi Sayısı</label>
<div class="col-sm-10">
<select id="rez_sayi" class="custom-select">
<option value="" selected>Kişi Sayısı Belirtiniz</option>
<option value="1">2 Kişilik Rezervasyon</option>
<option value="2">4 Kişilik Rezervasyon</option>
<option value="3">4+ Kişilik Rezervasyon</option>
<option value="4">6+ Kişilik Rezervasyon</option>
<option value="5">8+ Kişilik Rezervasyon</option>
<option value="6">20+ Kişilik Rezervasyon</option>
</select>
</div>
</div>
<textarea name="rez_aciklama" id="rez_aciklama" class="form-control" rows="6" cols="21" required="required"
placeholder="Ek açıklama girebilirsiniz."></textarea>
<form class="was-validated">
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="myModal" 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 text-center" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="divider"></div>
<div class="right-side">
<button type="button" class="btn btn-danger btn-link">Delete</button>
</div>
</div>
</div>
</div>
</div>
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label" for="customControlValidation1">Rezervasyon Şartlarını Kabul Ediyorum!</label>
<div class="invalid-feedback">Rezervasyon şartlarını okuyunuz!</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="button" class="btn btn-success" name="insert-data" id="insert-data" onclick="insertData()">Rezervasyonu Tamamla</button>
</div>
</div>
<br>
<p id="message"></p>
</form>
Here is my php file ;
<?php
include('db.php');
$rez_ad=$_POST['rez_ad'];
$rez_saat=$_POST['rez_saat'];
$rez_gsm=$_POST['rez_gsm'];
$rez_tarih=$_POST['rez_tarih'];
$rez_tip=$_POST['rez_tip'];
$rez_sayi=$_POST['rez_sayi'];
$rez_aciklama=$_POST['rez_aciklama'];
$stmt = $DBcon->prepare("INSERT INTO student(rez_ad,rez_saat,rez_gsm,rez_tarih,rez_tip,rez_sayi,rez_aciklama) VALUES(:rez_ad, :rez_saat,:rez_gsm,:rez_tarih,:rez_tip,:rez_sayi,:rez_aciklama)");
$stmt->bindparam(':rez_ad', $rez_ad);
$stmt->bindparam(':rez_saat', $rez_saat);
$stmt->bindparam(':rez_gsm', $rez_gsm);
$stmt->bindparam(':rez_tarih', $rez_tarih);
$stmt->bindparam(':rez_tip', $rez_tip);
$stmt->bindparam(':rez_sayi', $rez_sayi);
$stmt->bindparam(':rez_aciklama', $rez_aciklama);
if($stmt->execute())
{
$res="Data Inserted Successfully:";
echo json_encode($res);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
?>
Here is my jquery code;
function insertData() {
var rez_ad=$("#rez_ad").val();
var rez_saat=$("#rez_saat").val();
var rez_gsm=$("#rez_gsm").val();
var rez_tarih=$("#rez_tarih").val();
var rez_tip=$("#rez_tip").val();
var rez_sayi=$("#rez_sayi").val();
var rez_aciklama=$("#rez_aciklama").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_ad:rez_ad,rez_saat:rez_saat,rez_gsm:rez_gsm,rez_tarih:rez_tarih,rez_tip:rez_tip,rez_sayi:rez_sayi,rez_aciklama:rez_aciklama},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
Thanks for help !
You should then pass the rez_tip option in ajax call also.
<script type="text/javascript">
function insertData() {
//var rez_tip=$("#rez_tip option:selected").text();
//var rez_sayi=$("#rez_sayi option:selected").text();
var rez_tip=$("select#rez_tip").val();
var rez_sayi=$("select#rez_sayi").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_sayi:rez_sayi,rez_tip:rez_tip},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
</script>
I want to create a submit form to enter MySQL db data and image. I want to make do this using same form through jquery ajax. I tried my best with following code. I couldn't be success. When I enter id I want auto fill email and name field auto. I did it through ajax successfully. I couldn't submit both file and data through ajax.
<?php
print_r($_POST);
print_r($_FILES);
?>
<form class="form-horizontal" action="postvaccode2.php" method="post" id="data" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Post Vacancy</legend>
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-5">
<select id="cato" name="cato" class="form-control" id="cato">
<option value="IT">IT</option>
<option value="Finance">Finance</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-6">
<input id="comid" name="comid" type="text" placeholder="comid" class="form-control input-md" required="" >
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-6">
<input id="loc" name="loc" type="text" placeholder="city or town" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="qul" name="qul" type="text" placeholder="qulification" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="indate" name="indate" type="date" placeholder="indate" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-4">
<input id="expdate" name="expdate" type="date" placeholder="expdate" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="gpa" name="gpa" type="text" placeholder="gpa" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="des" name="des" type="text" placeholder="description" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="title" name="title" type="text" placeholder="title" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="comname" name="comname" type="text" placeholder="name" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="email" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="image" name="image" type="file" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="vacpost" name="vacpost" type="button" class="form-control input-md">
</div>
</div>
</fieldset>
</form>
<script>
$('#comid').on('input',function(e){
$.ajax({
type: "POST",
url: "postvaccode.php",
data: {id:$('#comid').val()},
success: function (response) {
var partsOfStr = response.split(',');
$('#comname').val(partsOfStr[0]);
$('#email').val(partsOfStr[1]);
}
});
});
</script>
<script>
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: "postvaccode3.php",
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});
return false;
});
</script>
postvaccode3.php
<?php
include('dbconnection.php');
if(isset($_POST['comid'])) {
$comid = $_POST['comid'];
$cato = $_POST['cato'];
$loc = $_POST['loc'];
$qul = $_POST['qul'];
$indate = $_POST['indate'];
$expdate = $_POST['expdate'];
$gpa = $_POST['gpa'];
$des = $_POST['des'];
$title = $_POST['title'];
$comname = $_POST['comname'];
$email=$_POST['email'];
$sql="insert into vacancy(companyid,catogary,location,qulification,indate,expectgpa,description,title,expdate,company_name,email,image) values($comid,'$cato','$loc','$qul','$indate','$gpa','$des','$title','$expdate','$comname','$email')";
if(mysqli_query($conn,$sql)){
echo "great";
}
else{
echo "not great";
}
}
?>
Am fairly new to Laravel and Ajax so you'll forgive me if am making a stupid mistake. Am trying to return a response from a controller, it works when i do all the logic in the routes file, but when i copied the code to my controller it doesnt work.
Heres my route
Route::post('student', 'StudentPDetailsController#store');
and the js file, called custom.js
(function($){
$(document).ready(function(){
$('#saveDetails').click(function(e){
e.preventDefault();
var dataString = {'studentphonenumber':$('#sphonenumber').val(),
'studentidnumber':$('#sidpp').val(),
'studentgender':$('#sgender').val(),
'studentbirthdate':$('#datepicker').val(),
'_token':$('#token').val()
}
$.ajax({
type: 'POST',
url: 'student',
data: dataString,
success: function(data){
console.log(data);
alert('Your personal details have been updated')
}
});
});
});
})(jQuery);
Heres the blade template:
<div class="panel panel-default panelshadow">
<div class="panel-heading"><h3 class="headinpanel">Personal Details</h3></div>
<div class="panel-body">
<form class="form-horizontal" id="studentPersonalDetails" role="form" method="POST" action="#">
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Phone Number</label>
<div class="col-md-6">
<input type="text" class="form-control" id="sphonenumber" name="phone" value="{{ old('phone') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">National ID/Passport</label>
<div class="col-md-6">
<input type="text" class="form-control" id="sidpp" name="idnumber" value="{{ old('idnumber') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Gender</label>
<div class="col-md-6">
<select name="gender" id="sgender" class="form-control">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Date of Birth</label>
<div class="col-md-6">
<input type="text" id="datepicker" class="form-control"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary saveBtn" id="saveDetails">
<h4>Save Details</h4>
</button>
</div>
</div>
</form>
</div>
</div>
and finally StudentPDetailsController
public function store()
{
if(Request::ajax()){
return Response::json(Request::all());
}
}
You need to pass Response object to the controller:
public function store(Request $request)
...
$request->all()
I submit my form with JQuery Ajax in php as this :
$("#addForm").submit( function() {
//var valid = jQuery("#addForm").validationEngine('validate');
var valid = true;
if(valid==true) {
// recuperer toutes les informations
var name = $("#name").val();
var type = $('select#type option:selected').val();
var region = $('select#region option:selected').val();
var level = $('select#level option:selected').val();
var nbPers = $("#number_person").val();
var tempsPre = $("#time_pre").val();
var tempsC = $("#time_c").val();
var ings = $("#ing_hide").val();
var etapes = $("#preparation").val();
$.ajax({
type: "POST",
url: "php/add.php",
data: "name="+name+"&type="+type+"®ion="+region+"&level="+level+"&nbPers="+nbPers+"&tempsPre="+tempsPre+"&tempsCui="+tempsC+"&ings="+ings+"&etapes="+etapes,
error : function(request, error) {
alert("Erreur : responseText: "+request.responseText);
},
success: function(msg){ // si l'appel a bien fonctionné
alert('ok');
}
});
return false;
} else {
alert('error');
return false;
}
});
when I debug using firebug, all my variables were correct
but in php files , the var_dump show : undefined for var_dump($_POST['name']); and correct value for var_dump($_POST['ings']);
Note: I dont use isset because I'm only testing that the variable contains value in first
Why I've undefined ?
My HTML form :
<div class="row field_text">
<label class="label_title">Nom :</label>
<input type="text" class="inputtext" name="name" id="name"/>
</div>
<div class="row field_select">
<label class="label_title">Type :</label>
<select class="select_styled" name="type" id="type">
<option value="1">E</option>
<option value="2">P</option>
<option value="3">D</option>
<option value="4">B</option>
</select>
</div>
<div class="row">
<label>Region</label>
<select class="select_styled" name="region" id="region">
<option value="1">Ma</option>
<option value="2">Eu</option>
<option value="3">Af</option>
<option value="4">Mo</option>
<option value="5">As</option>
</select>
</div>
<div class="row field_select">
<label class="label_title">Difficulté :</label>
<select class="select_styled" name="level" id="level">
<option value="1">F</option>
<option value="2">M</option>
<option value="3">D</option>
</select>
</div>
<div class="row field_text">
<label class="label_title">Nombre Personne :</label>
<input type="text" class="inputtext" name="number_person" id="number_person"/>
</div>
<div class="row field_text">
<label class="label_title">Temps pré :</label>
<input type="text" class="inputtext" name="time_pre" id="time_pre"/>
</div>
<div class="row field_text">
<label class="label_title">Temps cui :</label>
<input type="text" class="inputtext" name="time_cook" id="time_cook"/>
</div>
<div class="clear"></div>
<div class="ings_div">
<div class="row field_select">
<label for="ingredient" class="label_title">Ingrédient :</label>
<select id="ingredient" name="basic-combo" size="1" class="select_styled">
<?php include('php\liste-ingredients.php');?>
</select>
</div>
<div class="row field_text">
<label class="label_title">Quantité :</label>
<input id="quantite" type="text" class="inputtext" name="recette_quantite"/>
</div>
<div class="row field_select">
<label class="label_title">Unité :</label>
<select id="unite" class="select_styled" name="unite">
<?php include('php/liste-unites.php'); ?>
</select>
</div>
<div class="row rowSubmit">
<input id="btn_add_ing" type="submit" value="+">
</div>
<div class="rowIng">
<label class="label_title">Liste Ingrédients :</label>
<textarea id="recette_ingredient" class="textareaField required" name="recette_ingredient" cols="5" rows="4"></textarea>
</div>
<div class="rowIngHide">
<label class="label_title">Liste Ingrédients :</label>
<textarea id="recette_ingredient_hide" class="textareaField required" name="recette_ingredient_hide" cols="5" rows="4"></textarea>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="preparation_div">
<div class="">
<label class="label_title">Préparation :</label>
<textarea class="textareaField required" name="preparation" id="preparation" cols="8" rows="4"></textarea>
</div>
</div>
<div class="clear"></div>
<div class="row rowSubmit">
<span class="btn btn_search"><input type="submit" value="Ajouter"></span>
</div>
</form>