Ajax multipart/form-data method gives error - php

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);
}
});
}));

Related

how can I get the result from second code?

I have modified my question. How can I get the result from second form? because I have create two form with similar code, however I can only get the result from the first form and when I press the second one, it still get the result from first form. Can someone help me out?
Code
<div>
<form id="fupForm" name="form1" method="post">
<div class="form-group">
<input type="hidden" class="form-control" id="gp_name" name="gp_name"
value="<?php echo $gp_name;?>">
<input type="hidden" class="form-control" id="date" name="date" value="<?
php
echo $deday;?>">
<input type="hidden" class="form-control" id="type" name="type"
value="Hotel">
</div>
<input type="button" name="save" class="btn btn-primary"
value="Add Hotel" id="butsave">
</form></div>
<div>
<form id="fupForm2" name="form2" method="post">
<div class="form-group">
<input type="hidden" class="form-control" id="gp_name" name="gp_name"
value="<?php echo $gp_name;?>">
<input type="hidden" class="form-control" id="date" name="date" value="<?
php
echo $deday;?>">
<input type="hidden" class="form-control" id="type" name="type"
value="Coach">
</div>
<input type="button" name="save" class="btn btn-primary"
value="Add Coach" id="butsave2">
</form></div>
<script>
$(document).ready(function() {
$('#butsave').on('click', function() {
$("#butsave").attr("disabled", "disabled");
var gp_name = $('#gp_name').val();
var date = $('#date').val();
var type = $('#type').val();
if(gp_name!="" && date!="" && type!=""){
$.ajax({
url: "save.php",
type: "POST",
data: {
gp_name: gp_name,
date: date,
type: type
},
cache: false,
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$("#butsave").removeAttr("disabled");
$('#fupForm').find('input:text').val('');
$("#success").show();
$('#success').html('Data added successfully !');
}
else if(dataResult.statusCode==201){
alert("Error occured !");
}
}
});
}
else{
alert('Please fill all the field !');
}
});
});
</script>
<script>
$(document).ready(function() {
$('#butsave2').on('click', function() {
$("#butsave2").attr("disabled", "disabled");
var gp_name = $('#gp_name').val();
var date = $('#date').val();
var type = $('#type').val();
if(gp_name!="" && date!="" && type!=""){
$.ajax({
url: "save.php",
type: "POST",
data: {
gp_name: gp_name,
date: date,
type: type
},
cache: false,
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$("#butsave2").removeAttr("disabled");
$('#fupForm2').find('input:text').val('');
$("#success").show();
$('#success').html('Data added successfully !');
}
else if(dataResult.statusCode==201){
alert("Error occured !");
}
}
});
}
else{
alert('Please fill all the field !');
}
});
});
</script>
I have try to find on the Google, but can't find the solution for my case.
Add form id before the input element.
Remember ids are always unique and can not be duplicate. So here I change id to class. Please check below code.
<div>
<form id="fupForm" name="form1" method="post">
<div class="form-group">
<input type="hidden" class="form-control gp_name" id="" name="gp_name"
value="<?php echo $gp_name;?>">
<input type="hidden" class="form-control date" id="" name="date" value="<?
php
echo $deday;?>">
<input type="hidden" class="form-control type" id="" name="type"
value="Hotel">
</div>
<input type="button" name="save" class="btn btn-primary"
value="Add Hotel" id="butsave">
</form></div>
<div>
<form id="fupForm2" name="form2" method="post">
<div class="form-group">
<input type="hidden" class="form-control gp_name" id="" name="gp_name"
value="<?php echo $gp_name;?>">
<input type="hidden" class="form-control date" id="" name="date" value="<?
php
echo $deday;?>">
<input type="hidden" class="form-control type" id="" name="type"
value="Coach">
</div>
<input type="button" name="save" class="btn btn-primary"
value="Add Coach" id="butsave2">
</form></div>
<script>
$(document).ready(function() {
$('#butsave').on('click', function() {
$("#butsave").attr("disabled", "disabled");
var gp_name = $('#fupForm .gp_name').val();
var date = $('#fupForm .date').val();
var type = $('#fupForm .type').val();
if(gp_name!="" && date!="" && type!=""){
$.ajax({
url: "save.php",
type: "POST",
data: {
gp_name: gp_name,
date: date,
type: type
},
cache: false,
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$("#butsave").removeAttr("disabled");
$('#fupForm').find('input:text').val('');
$("#success").show();
$('#success').html('Data added successfully !');
}
else if(dataResult.statusCode==201){
alert("Error occured !");
}
}
});
}
else{
alert('Please fill all the field !');
}
});
});
</script>
<script>
$(document).ready(function() {
$('#butsave2').on('click', function() {
$("#butsave2").attr("disabled", "disabled");
var gp_name = $('#fupForm2 .gp_name').val();
var date = $('#fupForm2 .date').val();
var type = $('#fupForm2 .type').val();
if(gp_name!="" && date!="" && type!=""){
$.ajax({
url: "save.php",
type: "POST",
data: {
gp_name: gp_name,
date: date,
type: type
},
cache: false,
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$("#butsave2").removeAttr("disabled");
$('#fupForm2').find('input:text').val('');
$("#success").show();
$('#success').html('Data added successfully !');
}
else if(dataResult.statusCode==201){
alert("Error occured !");
}
}
});
}
else{
alert('Please fill all the field !');
}
});
});
</script>
An id must be unique in a document. Use a validator.
You get the same data each time, because you are searching using an id selector and error recovery means that you will always get the first one.
Don't use IDs
Do use selectors relative to the form you are dealing with
For example:
$("form").on("submit", function(event) {
event.preventDefault();
const $form = $(this);
const $input = $form.find("[name=type]");
console.log($input.val());
});
form {
padding: 1em
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type=hidden name=type value=foo>
<button>Submit</button>
</form>
<form>
<input type=hidden name=type value=bar>
<button>Submit</button>
</form>
The function $( document ).ready( function() {} ) and $( function() {} ) are equal. This will overwrite the previous variant. If you merge the code, it should work.

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);
}
});
});

AJAX Method not working

I have a form, when i click on submit i dont want the page to refresh, thats why i added AJAX to achieve this as you can see. The problem is that its not working.
<form id="formFooter" action="" method="post">
<h3>Select your trademark</h3>
<select class="form-control" name="trademark">
<option></option>
<option>©</option>
<option>™</option>
<option>®</option>
</select>
<h3>Your company name</h3>
<input class="form-control" type="text" name="companyName" placeholder="Your company name" />
<h3>Background Color</h3>
<input class="form-control" placeholder="(e.g. 00ff00)" type="text" name="backgroundColor">
<h3>Font Color</h3>
<input class="form-control" placeholder="(e.g. 00ff00)" type="text" name="fontColor">
<h3>Opacity</h3>
<input class="form-control" placeholder="(Pick a value between 0 and 1 e.g. 0.3)" type="text" name="opacity">
<br/>
<br/>
<button class="form-control" id="run" type="submit" name="submit">Generate footer</button>
</form>
<div id="showData"> </div>
<script type="text/javascript">
$('#run').on("click", function (e) {
var formData = new FormData($('#myForm')[0]);
$.ajax({
url: "script.php",
type: 'POST',
data: formData,
success: function (data) {
$('#showData').html(data);
},
cache: false,
contentType: false,
processData: false
});
return false;
});
</script>
Here is the script.php:
<?php
function footerPreview ()
{
echo "<h3>Preview:</h3>";
date_default_timezone_set('UTC');
$trademark = $_POST["trademark"];
$company = $_POST["companyName"];
$date = date("Y");
//style
$backgroundColor = $_POST['backgroundColor'];
$fontColor = $_POST['fontColor'];
$opacity = $_POST['opacity'];
echo "<div id='generated_footer_date' style='background-color:$backgroundColor; color:$fontColor; opacity: $opacity; ' >$trademark $date $company </div>";
}
// generate result for the head
function rawHead()
{
$head = htmlspecialchars('<head>
<meta charset="utf-8">
<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/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Raleway:200" rel="stylesheet">
</head>',ENT_QUOTES);
echo "<pre><h4>Put this code inside your head tags</h4>$head</pre>";
}
// generate result for the body
function rawBody ()
{
$body1of5 = htmlspecialchars('<div id="footer_date">',ENT_QUOTES);
$body2of5 = $_POST["trademark"];
$body3of5 = date("Y");
$body4of5 = $_POST["companyName"];
$body5of5 = htmlspecialchars('</div>',ENT_QUOTES);
echo "<pre><h4>Put this code inside your body tags</h4>$body1of5 $body2of5 $body3of5 $body4of5 $body5of5 </pre>";
}
// generate result for the CSS
function rawCSS ()
{
$opacity = $_POST['opacity'];
$backgroundColor = $_POST['backgroundColor'];
$fontColor = $_POST['fontColor'];
echo
"<pre>
<h4>Put this code in your websites stylesheet</h4>
color:$fontColor;
background-color:$backgroundColor;
opacity:$opacity;
width:100%;
text-align:center;
padding-top:15px;
height:50px;
font-family: 'Raleway', sans-serif;
right: 0;
bottom: 0;
left: 0;
position:fixed;
</pre>";
}
// Generate eveything by one click
if(isset($_POST['submit']))
{
footerPreview();
rawHead();
rawBody();
rawCSS();
}
?>
When i click on submit nothing happens. I want the script.php to be generate on the same page without refreshing.
You can make it very simple your Ajax Request as:
First of all no need to use FormDate here, because you don't have any file input in your <form>, so you can use serialize() data in your request as:
var formData = $("#myForm").serialize();
Second, you are just printing the HTML in your PHP, it means you just need to print html, so you can use dataType=HTML here as:
dataType: "html",
Third, one more thing will help you in debugging, add print_r($_POST) in your script.php file at top and check the console.
Modified Request:
$(document).ready(function(){
$("#run").click(function(){
var formData = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "script.php",
data: formData,
dataType: "html",
success: function(response)
{
$('#showData').html(response);
},
beforeSend: function()
{
//any loader
}
});
return false;
});
});
Update:
From your comment: yeah it shows after submit. It shows this : Array
( [trademark] => [companyName] => [backgroundColor] => [fontColor] =>
[opacity] => ) – Kevin Aartsen 6 mins ago
Look at this array, you don't have submit in the result of $_POST so you have two options to change this:
1) You can use count() function for checking if(count($_POST) > 0).
2) Or you can use <input type='submit' name='submit'> instead of <button type='submit' name='submit'>
$(document).ready(function() {
$('#run').on("click", function (e) {
e.preventDefault();
alert('inside ajax call');
var formData = new FormData($('#myForm')[0]);
$.ajax({
url: "script.php",
type: 'POST',
data: formData,
success: function (data) {
$('#showData').html(data);
alert('ajax call success');
},
cache: false,
contentType: false,
processData: false
});
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<form id="formFooter" action="" method="post">
<h3>Select your trademark</h3>
<select class="form-control" name="trademark">
<option></option>
<option>©</option>
<option>™</option>
<option>®</option>
</select>
<h3>Your company name</h3>
<input class="form-control" type="text" name="companyName" placeholder="Your company name" />
<h3>Background Color</h3>
<input class="form-control" placeholder="(e.g. 00ff00)" type="text" name="backgroundColor">
<h3>Font Color</h3>
<input class="form-control" placeholder="(e.g. 00ff00)" type="text" name="fontColor">
<h3>Opacity</h3>
<input class="form-control" placeholder="(Pick a value between 0 and 1 e.g. 0.3)" type="text" name="opacity">
<br/>
<br/>
<button class="form-control" id="run" type="submit" name="submit">Generate footer</button>
</form>
<div id="showData"> </div>
try above code and remove alert when it works for you :)

Input fields are empty after .submit jquery statement

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.).

hide div after inserting data in database

Hi would you like to help me. im a php newbie. I want to insert employment information in my database and hide da div where the form placed.
HTML:
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<form name="empform" method="post" action="profile.php" autofocus>
<input name="employ" type="text" id="employ" pattern="[A-Za-z ]{3,20}"
placeholder="Who is your employer?">
<input name="position" type="text" id="position" pattern="[A-Za-z ]{3,20}"
placeholder="What is your job description?">
<input name="empadd" type="text" id="empadd" pattern="[A-Za-z0-9##$% ]{5,30}"
placeholder="Where is your work address?">
<input name="empcont" type="text" id="empcont" pattern="[0-9]{11}" title="11-digit number"
placeholder="Contact number">
<input name="btncancel" type="button" class="btncancel" value="Cancel"
style="width:60px; border-radius:3px; float:right">
<input name="btndone" type="submit" class="btndone" value="Done" style="width:60px; border-radius:3px; float:right">
</form>
</div>
</div>
PHP:
if (isset($_POST['btndone'])) {
$employ = $_POST['employ'];
$position = $_POST['position'];
$empadd = $_POST['empadd'];
$empcont = $_POST['empcont'];
$empdate = $_POST['empdate'];
$empID = $alumniID;
$obj - > addEmployment($employ, $position, $empadd, $empcont, $empdate, $empID);
}
JS:
<script>
$(function () {
function runEffect() {
var selectedEffect = "highlight";
$(".toggler").show(selectedEffect);
};
function runDisplay() {
var selectedDisplay = "highlight";
$("#empdisplay").show(selectedDisplay);
};
$(".btncancel").click(function () {
$(".toggler").hide();
return false;
});
$(".btndone").click(function () {
runDisplay();
$(".toggler").hide();
return false;
});
}
</script>
Hi this is what I'll do
var request = $.ajax({
url: "profile.php",
type: "POST",
data: $('#form').serialize()
});
request.done(function(msg) {
$('#form').hide();
});
request.fail(function(jqXHR, textStatus) {
alert( "Form failed" );
});
If you have some doubts with Jquery's Ajax visit this link
If you don't understand what jqXHR is, I suggest you visit this link http://www.jquery4u.com/javascript/jqxhr-object/
Execute on click
$('#form').submit(function(){
var request = $.ajax({
url: "profile.php",
type: "POST",
data: $('#form').serialize()
});
request.done(function(msg) {
$('#form').hide();
});
request.fail(function(jqXHR, textStatus) {
alert( "Form failed" );
});
});
Try This
HTML
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<form id="empform" name="empform" method="post" action="profile.php" autofocus>
<input name="employ" type="text" id="employ" pattern="[A-Za-z ]{3,20}"
placeholder="Who is your employer?">
<input name="position" type="text" id="position" pattern="[A-Za-z ]{3,20}"
placeholder="What is your job description?">
<input name="empadd" type="text" id="empadd" pattern="[A-Za-z0-9##$% ]{5,30}"
placeholder="Where is your work address?">
<input name="empcont" type="text" id="empcont" pattern="[0-9]{11}" title="11-digit number"
placeholder="Contact number">
<input name="btncancel" type="button" class="btncancel" value="Cancel"
style="width:60px; border-radius:3px; float:right">
<input id="submit"name="btndone" type="submit" class="btndone" value="Done" style="width:60px; border-radius:3px; float:right">
</form>
</div>
</div>
<script>
$(document).ready(function() {
//$("#form").prev
$('#submit').click(function(event) {
//alert (dataString);return false;
event.preventDefault();
$.ajax({
type: "POST",
url: 'profile.php',
dataType:"html",
data: $("#empform").serialize(),
success: function(msg) {
alert("Form Submitted: " + msg);
//alert($('#form').serialize());
$('div.toggler').hide();
}
});
});
});
</script>
</html>
PHP
profile.php
<?php
if (isset($_POST)) {
$employ = $_POST['employ'];
$position = $_POST['position'];
$empadd = $_POST['empadd'];
$empcont = $_POST['empcont'];
$empdate = $_POST['empdate'];
$empID = $alumniID;
$obj - > addEmployment($employ, $position, $empadd, $empcont, $empdate, $empID);
}
?>
Iam not sure about your fields
echo $empdate = $_POST['empdate'];
$empID = $alumniID;
they are not in form but works!...
You should do an ajax call to save your data and then hide the div, someting like this :
$('form[name="empform"]').submit(function(e) {
e.preventDefault();
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$('div.toggler').hide();
});
});

Categories