提交表单后留页表单、后留页

2023-09-10 19:06:25 作者:余生浪子

我想呆的地方,从一种形式被提交当前页面上。但不知何故,它不工作。我发现,code在互联网上的一些撕成小块,并把它在一起。

I'm trying to stay on the current page from where a form gets submitted. But somehow it's not working. I found some peaces of code on the internet and put it together.

这是process.php文件:

This is the process.php file:

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

和的code休息,HTML和JavaScript的可以的jsfiddle找到: jsfiddled code

And the rest of the code, the html and javascripts can be found on jsfiddle: jsfiddled code

    $(function(){
        $('#contact').validate({
        submitHandler: function(form) {
                $(form).ajaxSubmit({
                url: 'process.php',
                success: function() {
                    $('#contact').hide();
                    $('#contact-form').append("<p class='thanks'>thanks test.</p>")
                    }
                });
            }
        });         
    });

忘了提,现在发生了什么。我重定向到process.php页。

Forgot to mention what happens now. I get redirected to process.php page.

推荐答案

使用 jQuery.ajax()功能而无需刷新页面提交表单。你需要做的是这样的:

Use jQuery.ajax() function to submit a form without refreshing a page. You need to do something like this:

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>

process.php:

<?php
      // Get your form data here in $_POST
?>

ajaxform.js

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

});