本文介绍了如何在不重定向页面的情况下刷新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的PHP代码,
Here are is my php code,
<?php
$data= array();
$from = $_POST['email'];
$full_name = $_POST['fullname'];
$number=$_POST['number'];
$subject = "Contact Form submission";
$messages = $full_name . " " . " sent you this message:" . "\n\n" . $_POST['message']. "\nPlease contact ". $full_name ." via email ". $from . " or call them at ". $number;
$data['success'] = true;
$data['message'] = 'Thank you. We will get back to you shortly!';
// return all our data to an AJAX call
echo json_encode($data);
?>
这里是我的javascript文件
// magic.js
and here is my javascript file
// magic.js
$(document).ready(function() {
// process the form
$('form').submit(function(event) {
// get the form data
// there are many ways to get this data using jQuery (you can use the class or id also)
var formData = {
'email' : $('input[name=email]').val(),
'fullname' : $('input[name=fullname]').val(),
'number' : $('input[name=number]').val(),
'message' : $('input[name=message]').val()
};
// process the form
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'mail_processor.php', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
// using the done promise callback to handle successful ajax request
.done(function(data) {
// log data to the console so we can see
console.log(data);
$('form').append('<div class="alert alert-success">' + data.message + '</div>');
// usually after form submission, you'll want to redirect
// window.location = '/thank-you'; // redirect a user to another page
alert('success'); // for now we'll just alert the user
});
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
});
当我点击提交时,没有任何反应。按钮不会单击,我似乎无法弄清楚它有什么问题。请帮忙。
When I click submit, nothing happens. The button won't click and I can't seem to figure out whats wrong with it. Please help.
推荐答案
这篇关于如何在不重定向页面的情况下刷新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!