Thank you message not showing when Ajax form goes through - php

I'm trying to hide the regular content and display a thank you message in it's place after a form is sent. However when the form sends, the fields reset but the thank you message won't appear. Please help. I'm not that great with jQuery.
I'm also using the foundation abide.js, don't know if that makes difference or not.
Here is the ajax code.
$(function() {
// Get the form.
var form = $('#form');
// Get the messages div.
var formMessages = $('#thanks');
// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}).done(function(response) {
$('#thanks').show();
$(form).hide();
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#number').val('');
})
});
});
This is the code for the form.
<div class="newsletter" id="contact">
<div class="row footer-top">
<div class="large-12 medium-12 columns updates" id="thanks" style="display: none;">
<h4>Thanks! We'll be in touch shortly.</h4>
</div>
<div class=" large-12 medium-12 column updates" id="normal">
<h4>Want more info? Contact us!</h4>
<p>Sign up to stay in touch with what's happening. Don't Worry, we don't spam.</p>
</div>
</div>
<form data-abide id="form" method="post" action="wp-content/themes/myles_custom/mailer.php">
<div class="row">
<div class="large-8 large-centered columns">
<label>
Name:
<input type="text" name="name" id="name" placeholder="Name" value="<?php if(isset($name)){echo htmlspecialchars($name);} ?>" required pattern="[a-zA-Z]+">
</label>
<small class="error">Name is required.</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Email:
<input type="email" id="email" name="email" placeholder="Email" value="<?php if(isset($email)){echo htmlspecialchars($email);} ?>" required>
</label>
<small class="error">Email is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Number:
<input type="tel" pattern="number" id="number" name="number" placeholder="Name" value="<?php if(isset($number)){echo htmlspecialchars($number);} ?>" required>
</label>
<small class="error">Number is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Campus:
<select id="campus" name="campus">
<option>FSU</option>
<option>FAMU</option>
<option>TCC</option>
</select>
</label>
<small class="error">Campus is required</small>
</div>
</div>
<div id="sucks" style="display: none;">
<label for="address" id="adress_label" style: "display: none;">Address</label>
<input name="address" id="address" type="text" style="display: none;">
</div>
<div class="row">
<div class="large-12 columns" style="text-align: center;">
<button id="submit" type="submit" value="Send">Submit</button>
</div>
</div>
</form>
</div>

You have a extra closing bracket in the code, and I also would not use $(form) with jquery instead use an id reference $("#form").
I made a quick working example at https://jsfiddle.net/mum1m1sc/
$("#myForm").submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
var formData = $("#myForm").serialize();
$.ajax({
type: 'POST',
url: $("#myForm").attr('action'),
data: formData
}).done(function(response) {
$('#thanks').show();
$("#myForm").hide();
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#number').val('');
});
});
<div class="newsletter" id="contact">
<div class="row footer-top">
<div class="large-12 medium-12 columns updates" id="thanks" style="display: none;">
<h4>Thanks! We'll be in touch shortly.</h4>
</div>
<div class=" large-12 medium-12 column updates" id="normal">
<h4>Want more info? Contact us!</h4>
<p>Sign up to stay in touch with what's happening. Don't Worry, we don't spam.</p>
</div>
</div>
<form data-abide id="myForm" method="post" action="">
<div class="row">
<div class="large-8 large-centered columns">
<label>
Name:
<input type="text" name="name" id="name" placeholder="Name" value="" required pattern="[a-zA-Z]+">
</label>
<small class="error">Name is required.</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Email:
<input type="email" id="email" name="email" placeholder="Email" value="" required>
</label>
<small class="error">Email is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Number:
<input type="tel" id="number" name="number" placeholder="Name" value="">
</label>
<small class="error">Number is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Campus:
<select id="campus" name="campus">
<option>FSU</option>
<option>FAMU</option>
<option>TCC</option>
</select>
</label>
<small class="error">Campus is required</small>
</div>
</div>
<div id="sucks" style="display: none;">
<label for="address" id="adress_label" style: "display: none;">Address</label>
<input name="address" id="address" type="text" style="display: none;">
</div>
<div class="row">
<div class="large-12 columns" style="text-align: center;">
<button id="submit" type="submit" value="Send">Submit</button>
</div>
</div>
</form>
<div id="thanks" style="display:none;">
<h3>Thanks</h3>
</div>
hope that helps.

Related

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!

Vue JS Auto Generate Password not working

I want to generate a password for users when creating an account using vue components. However when I check the box it doesn't disabled the password input field.
Here is my code:
create.blade.php
#extends('layouts.manage')
#section('content')
<div class="flex-container">
<div class="columns m-t-10">
<div class="column">
<h1 class="title">Create New Users User</h1>
</div>
</div>
<hr class="m-t-0">
<div class="columns">
<div class="column">
<form action="{{ route('users.store') }}" method="POST">
<div class="field">
<label for="name" class="label">Name</label>
<p class="control">
<input type="text" class="input" name="name" id="name">
</p>
</div>
<div class="field">
<label for="email" class="label">Email</label>
<p class="control">
<input type="email" class="input" name="email" id="email">
</p>
</div>
<div class="field">
<label for="password" class="label">Password</label>
<p class="control">
<input type="password" class="input" name="password" id="password" v-if="!auto_password" placeholder="Manually input a password">
<b-checkbox name="auto_generate" class="m-t-15" v-model="auto_password">Auto Generate Password</b-checkbox>
</p>
</div>
<button class="button is-success">Create User</button>
</form>
</div>
</div>
</div>
#endsection
#section('scripts')
<script>
var app = new Vue({
el: '#app',
data: {
auto_password: true
}
});
</script>
#endsection
looking for help.
Thanks in advance.

php not receives data from ajax FormData

I'm trying to send multiple data and files by jquery ajax (using formData) to a php file but i don't recive anything in the php file neither $_POST nor $_FILE data, I receive NULL in both.
Here is my ajax function.
$('#formulario').submit(function(e){
e.preventDefault();
$.ajax({
url:"../phpfile.php",
type:"POST",
dataType:"JSON",
data: new FormData(this),
processData: false,
contentType: false
}).always(function(respuesta){
//code
});
});
Here is my HTML
<form enctype="multipart/form-data" method="POST">
<div class='row'>
<div class='col-xs-12'>
<h4>Destinatarios</h4>
<select class='form-control' required name='destinatarios[]' title='Selecciona cuantos quieras' multiple data-selected-text-format="count>2" data-live-search="true">
{%for familiar in familia%}
<option value='{{familiar.ID_PERSONA}}'>{{familiar.NOMBRE}} {{familiar.APELLIDOS}}</option>
{%endfor%}
</select>
</div>
<div class='col-xs-6 hidden'>
<h4>Tipo de entrega</h4>
<select class='form-control' id='entrega' name='entrega'>
<option value='1' selected></option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class='col-xs-4 text-center'>
<h4>Despues de mi partida</h4>
<input type="radio" value="muerte" name="tipoE" onchange="cambioR(this)">
</div>
<div class='col-xs-4 text-center'>
<h4>En fecha determinada</h4>
<input type="radio" value="fecha" name="tipoE" onchange="cambioR(this)">
</div>
<div class='col-xs-4 text-center'>
<h4>En mi ceremonia de despedida</h4>
<input type="radio" value="ceremonia" name="tipoE" onchange="cambioR(this)">
</div>
</div>
<br>
<div class="row">
<div id="f" class="hidden">
<div class="col-xs-12">
<div class="input-group date" id="sandbox-container">
<span class="input-group-addon">Fecha</span>
<input type="text" class="form-control date" placeholder="dd/mm/yyyy" name="fecha" id="fecha">
</div>
<br>
</div>
</div>
<div id="m" class="hidden">
<div class="col-xs-12">
<div class="input-group">
<span class="input-group-addon">Días después de mi partida</span>
<input class="form-control" id="muerte" name="muerte">
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h4>Mensaje</h4>
<input class="hidden" id="nombre_mensaje" name="nombre_mensaje">
<textarea class="form-control" rows="10" style="resize: none" id='mensaje' name='mensaje' required></textarea>
<br>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h4>Imágenes / Video / Audio / Archivos</h4>
<div class="grid" id="muestras" dc>
<div class="grid-item col-xs-3 deseo" onclick="novoFile(this);">
<img src="../Recursos/imagenes/plus.jpg" width="100%;">
<input class="hidden" type="file" onchange="cambio(this);" name="files[]">
<input class="hidden datas" name="data_multimedia[]" value="0">
</div>
</div>
</div>
</div>
<input class="hidden" id="id_mensaje" name="id_mensaje">
<input type="reset" id="reiniciar" hidden>
</form>
The user can upload as many files as he want, and the html input file is added dinamicly using jquery.
Try to replace:
new FormData(this);
with:
$('#formulario').serialize();

how will i passed the json data into php

I have four textboxes and when you click on the add one button it will add up another set of textboxes. My problem is how can i pass the json data to the action form. Here is my code:
$("#test").click(function(){
var array = $('.experience').map(function() {
var obj = {};
$(this).next().addBack().find('input:text').each(function() {
obj[this.id] = this.value;
});
return obj;
}).get();
$('#json').text(JSON.stringify(array, null, 2));
});
and my form action
<form action="<?php echo base_url().'resume/update'?>" method="post" id="send_form">
<!-- Experience Start -->
<div class="row">
<div class="col-sm-12">
<p> </p>
<h2>Experience</h2>
</div>
</div>
<div class="row experience">
<div class="col-sm-6">
<div class="form-group">
<label for="resume-employer">Employer</label>
<input type="text" class="form-control" name="resumeEmployer" id="resume-employer" value="<?php echo set_value('resumeEmployer'); ?>" id="resume-employer" placeholder="Company name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="resume-experience-dates">Start/End Date</label>
<input type="text" class="form-control" name="resumeExperienceDates" name="<?php echo set_value('resumeExperienceDates'); ?>" id="resume-experience-dates" placeholder="e.g. April 2010 - June 2013">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="resume-job-title">Job Title</label>
<input type="text" class="form-control" name="resumeJobTitle" id="resume-job-title" value="<?php echo set_value('resumeJobTitle'); ?>" placeholder="e.g. Web Designer">
</div>
</div>
<div class="col-sm-6">
<div class="form-group" id="resume-responsibilities-group">
<label for="resume-responsibilities">Responsibilities (Optional)</label>
<input type="text" class="form-control" name="resumeResponsibilities" id="resume-responsibilities" value="<?php echo set_value('resumeResponsibilities');?>" placeholder="e.g. Developing new websites">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<hr class="dashed">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p><a id="add-experience">+ Add Experience</a></p>
<hr>
</div>
</div>
<!-- Experience Start -->
<div class="row text-center">
<div class="col-sm-12">
<p> </p>
<input type="submit" id="test" class="btn btn-primary btn-lg" value="Submit" />
</div>
</div>
</form>
i added a id="test" in the input type="submit button
any help is muchly appreciated. TIA
Use json_encode and json_decode function example in ref link,
json_encode($arr);
var_dump(json_decode($json));
http://php.net/manual/en/function.json-encode.php
var_dump(json_decode($json, true));

Ajax response is getting but not posting on the PHP page

I want to retrive the data got from jquery to php via ajax.
My code to start up a modal window
<a href='javascript:void(0):' data-id={$row['customer_id']} class='btn small bg-blue-alt tooltip-button modal-customeredit' data-placement='top' title='Edit'><i class='glyph-icon icon-edit' ></i>
here am getting id from main page.and opening a modal pop up.
modal script as follows.
$( ".modal-customeredit" ).click(function() {
var myGroupId = $(this).attr('data-id'); // data-id
data = {'id':myGroupId };
$.ajax({
type: "POST",
url: "left.inc.php",
data: data, // data retrieve on server-side ($_POST['id'])
success:function(response){
$( "#modal-customeredit" ).dialog({
modal: true,
minWidth: 700,
minHeight: 200,
dialogClass: "modal-dialog",
show: "fadeIn"
});
alert('done');
alert(response); //do the rest of operations.
}
});
my form code is for modal:
<!-- Customer Edit -->
<div class="hide" id="modal-customeredit" title="Edit Customer Info">
<div class="pad10A">
<h3>Update Customer Info</h3>
<p class="font-gray-dark"> Fides Admin uses colors & styles from both the default theme color schemes and the included core color helpers. </p>
<div class="divider"></div>
<form id="customer-edit" action="" class="col-md-12 center-margin" method="">
<div class="form-row">
<div class="form-label col-md-3">
<label for="name">
Name:
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="name" name="name" placeholder="Name" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="form-row">
<div class="form-label col-md-3">
<label for="details">
Details :
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="details" placeholder="Details" name="details" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="form-row">
<div class="form-label col-md-3">
<label for="domain">
Domain :
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="domain" placeholder="Domain" name="domain" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="form-row">
<div class="form-label col-md-3">
<label for="vertical">
Vertical :
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="vertical" placeholder="Vertical" name="vertical" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="form-row">
<div class="form-label col-md-3">
<label for="taxanomy">
Taxanomy :
<span class="required">*</span>
</label>
</div>
<div class="form-input col-md-9">
<input id="taxanomy" placeholder="Taxanomy" name="taxanomy" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="divider"></div>
<div class="form-row">
<div class="form-input col-md-8 col-md-offset-3">
<span class="button-content">Submit</span>
</div>
</div>
</form>
</div>
</div>
<?php } elseif ($pfile=="customer-screen.php") { ?>
The problem i face is i got response from ajax.which display the exact id i want.
But when i try in php as
<?php $id=$_POST['id'];
echo $id; ?>
I was not able to echo the id in php file.But ajax display in the response.Hope i am conveying the question properly.If u help it will be really appreciated.If you have doubts about my question.Please comment let me expalin.
The data you're posting is in JSON format, not URL format. The data should be 'id=' + myGroupId.

Categories