Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交表单后请保持在页面上_Php_Jquery_Ajax_Html_Form Submit - Fatal编程技术网

Php 提交表单后请保持在页面上

Php 提交表单后请保持在页面上,php,jquery,ajax,html,form-submit,Php,Jquery,Ajax,Html,Form Submit,我试图停留在提交表单的当前页面上。但不知怎么的,它不起作用了。我在互联网上找到了一些代码,并把它们放在一起 这是process.php文件: <?php // Get Data $name = strip_tags($_POST['name']); $email = strip_tags($_POST['email']); $phone = strip_tags($_POST['phone']); $subject = strip_tags($_POST['subject']);

我试图停留在提交表单的当前页面上。但不知怎么的,它不起作用了。我在互联网上找到了一些代码,并把它们放在一起

这是process.php文件:

<?php
// Get Data    
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$subject = strip_tags($_POST['subject']);
$message = strip_tags($_POST['message']);

// Send Message
mail( "email@domain.com", "Contact Form testttt",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms testtttttttt" );
?>

剩下的代码、html和Java脚本可以在JSFIDLE上找到:

$(函数(){
$(“#联系人”)。验证({
submitHandler:函数(表单){
$(表格).ajaxSubmit({
url:'process.php',
成功:函数(){
$(“#联系人”).hide();
$(“#联系方式”)。附加(

感谢测试。

) } }); } }); });

忘了说现在发生了什么。我被重定向到process.php页面。

这是一个让您开始的链接:

您需要使用JavaScript返回或发出请求,而不是通过提交表单(这称为Ajax,有许多与主题相关的教程从链接而来)。

使用函数提交表单而不刷新页面。您需要这样做:

test.php:

<script type="text/javascript" src="jquery-version.js"></script>
<script type="text/javascript" src="ajaxform.js"></script>

<form action='process.php' method='post' class='ajaxform'>
 <input type='text' name='txt' value='Test Text'>
 <input type='submit' value='submit'>
</form>
<?php
      // Get your form data here in $_POST
?>

使用表单时会发生什么情况?为什么要使用
strip\u标签
?数据没有在HTML上下文中使用,因此标记根本不是问题。@ThiefMaster,我只是从不同的页面复制了源代码。我甚至不知道那是什么,你不应该这样做。最好学会自己做事情,而不仅仅是复制粘贴。@ThiefMaster,我就是这样学习的。但是谢谢你的建议。你是test.php,表单中的所有内容都使用单引号,为什么?我也可以使用双引号吗?谢谢,你的代码成功了!不过我有一个小问题。当表单成功处理后,如何再次清除所有字段?@Yustme:您可以在ajax函数的success method中清除字段,例如
$('.ajaxform')[0].reset()他实际上是如何发送电子邮件的?你不需要使用php来实现吗?
<?php
      // Get your form data here in $_POST
?>
jQuery(document).ready(function(){

    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            data    : $(this).serialize(),
            success : function( data ) {
                         alert('Form is successfully submitted');       
                      },
            error   : function(){
                         alert('Something wrong');
                      }
        });

        return false;
    });

});