submit both data and file(image) using jquery ajax - php

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";
}
}
?>

Related

Hide Post Parameters in jQuery AJAX Request

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>

Ajax not refreshing contact form

I'm working on a site and the goal was to submit a contact form without reloading the whole page. I tried different paths, but nothing seems to help. It looks like the "on submit" part doesn't work at all I'm still leaving site to contact_form.php. I would be very thankful for any help, thanks in advance
This is js part:
$("#contact-form").on('submit', function(){
var name = $("#form_name").val();
var lastname = $("#form_lastname").val();
var email = $("#form_email").val();
var subject = $("#form_subject").val();
var message = $("#form_message").val();
var dataString = 'name=' + name + '&lastname=' + lastname + '&email=' + email +'&subject'+subject + '&message=' + message;
$.ajax({
type: "POST",
url: "contact_form.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
return false;
});
and here html:
<form id="contact-form" method="post" action="contact_form.php" role="form">
<div class="messages"></div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class ="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Last name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_email" type="text" name="mail" class="form-control" placeholder="Email" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Subject" required="required" data-invalid-message="Please enter your name">
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-invalid-message="Please enter your name"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success" value="Send message">
</div>
</div>
</form>
You can start by passing the event and stopping the default behaviour, which would be to hit the action of the form.
$("#contact-form").on('submit', function(e){
e.preventDefault();
// rest of code... ( do not return true in the end of this handler though )

Update Page without reload AJAX/PHP

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!

insert data to mysql table using jquery ajax [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
i want to insert data to mysql table called vacancy using ajax.ajax successfully function alert run.but data don't save in table.and errors are occer.i am new for php and ajax
html code
<form class="form-horizontal" action="postvaccode2.php" type="post">
<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="IR">IT</option>
<option value="USA">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="vacpost" name="vacpost" type="submit" class="form-control input-md">
</div>
</div>
</fieldset>
</form>
script
</script>
<script>
$('#vacpost').click(function () {
$.ajax({
type: "POST",
url: "postvaccode2.php",
data:{id:$('#comid').val(),cato:$('#cato').val(),loc:$('#loc').val(),quli:$('#qul').val(),indate:$('#indate').val(),expdate:$('#expdate').val(),gpa:$('#gpa').val(),des:$('#des').val(),title:$('#title').val(),comname:$('#comname').val()},
success:function () {
alert('Sccessfuly Post the Job vacency');
location.reload(true);
}
});
});
</script>
postvaccode2.php
<?php
$conn=mysqli_connect("localhost","root","","internship");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id=$_POST['id'];
$cato=$_POST['cato'];
$loc=$_POST['loc'];
$quli=$_POST['quli'];
$indate=$_POST['indate'];
$expdate=$_POST['expdate'];
$gpa=$_POST['gpa'];
$des=$_POST['des'];
$title=$_POST['title'];
$comname=$_POST['comname'];
$sql="insert into vacancy(companyid,catogary,location,qulification,indate,expectgpa,description,title,expdate,companyname) values('$id','$cato','$loc','$quli','$indate','$gpa','$des','$title','$expdate','$comname',)";
$conn->query($sql);
?>

Ajax form submit not working in PHP

When im doing a ajax call to the update.php from profiles.php it go a blank page update.php but it does make the changes if i go back to the profile.php
idk what im doing wrong what im missing??
profile.php
<?php include_once("includes/config.php");
if (isset($_GET['id']))
{
$Nome = $_GET['id'];
$sql = "SELECT * FROM utilizadores WHERE id='$Nome'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
echo'
<br>
<form action="update.php" method="post" id="go" class="go" accept-charset="utf-8">
<div class="content" >
<div class="container-fluid">
div class="row">
<div class="col-md-8">
<div class="card">
<div class="header">
<h4 class="title">Perfil do paciente</h4>
</div>
<div class="content">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Nome</label>
<input type="text" class="form-control" disabled placeholder="Nome" value="'.$row["Nome"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Apelido</label>
input type="text" class="form-control" disabled placeholder="Apelido" value="'.$row["Apelido"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" name="Email "disabled placeholder="Email" value="'.$row["Email"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Idade</label>
<input type="text" class="form-control" disabled placeholder="Idade" value="'.$row["Idade"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Sexo</label>
<input type="text" class="form-control" disabled placeholder="Sexo" value="'.$row["Sexo"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Telefone</label>
<input type="text" class="form-control" disabled placeholder="Telefone" value="'.$row["Telefone"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Peso</label>
<input type="text" class="form-control" disabled placeholder="Peso" value="'.$row["Peso"].'Kg">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Altura</label>
<input type="text" class="form-control" disabled placeholder="Altura" value="'.$row["Altura"].'Cm">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Perimetro Abdominal</label>
<input type="text" class="form-control" disabled placeholder="Perimetro abdominal" value="'.$row["Perimetro_abdominal"].'Cm">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Perimetro Anca </label>
<input type="text" class="form-control" disabled placeholder="Perimetro Anca" value="'.$row["Perimetro_anca"].'Cm">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Medicação:</label>
<textarea rows="3" class="form-control" name="Medicacao" placeholder="Medicação" id="Medicacao">'.$row["Medicacao"].'</textarea>
</div>
<input type=hidden name=hidden value="'.$row["id"].' ">
<input type="submit" name="med" class="btn btn-info btn-fill pull-right" value="Atualizar" id="med"></div>
<div class="col-md-6">
<div class="form-group">
<label>Observações:</label>
<textarea rows="3" name="Obs" class="form-control" placeholder="Anotar observações" id="Obs">'.$row["Obs"].' </textarea>
</div>
<input type="submit" name="obs" class="btn btn-info btn-fill pull-right" value="Atualizar">
</div>
</div></form>';
}
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($conn);
?>
<script type="text/javascript">
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
</script>
update.php
include_once("includes/config.php");
if(isset($_POST['med'])) {
$UpdateQuery = "UPDATE utilizadores SET Medicacao='$_POST[Medicacao]' WHERE id='$_POST[hidden]' ";
$res = mysqli_query($conn,$UpdateQuery);
if($res == 1)
{
//header('Location: ' . $_SERVER['HTTP_REFERER'],true, 301 );
}
?>
You are not cancelling the default submit event of your submit button when you handle the click event. That causes the form to be submitted in the normal way as well by going to the url that is set in the action attribute of the form.
Apart from that, you also need to add the data to the ajax request.
This:
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
should be:
$( "#med" ).click(function(event) {
// cancel default submit event
event.preventDefault();
$.ajax({
type: "POST",
// add data
data: $('form#go').serialize(),
url: "update.php",
success: function(){
// Do whatever you need to here
console.log('campo atualizado com sucesso');
}
});
});

Categories