1

坚持使用 PHP,我知道这很简单,但不确定使用 jquery 或 php 的正确方法。

我有一个联系表单,当它提交表单时,我希望某些字段的结果显示在结果中,谢谢 paeg 说:

感谢您输入YOURNAME。等等等等等等

这是否可以通过简单的 php 行实现,还是需要通过 jquery 调用。

谢谢,只是php的新手,所以有些东西还是有点混乱

   <?php
    define('is_freetrial-celebrity', 1);
    include_once('includes/header.inc.php');
    ?>
    <div role="main" id="main">
        <article id="mainFreetrial" class="greyBlock twoColumnsLayout">
            <header>
                <h1>Get Started</h1>
            </header>
            <div>
                <form method="get" action="/forms_validation/freetrial.php" class="trialForm ajaxForm">
                    <div class="column">

                        <p>
                            <label for="firstNameTrial">First name<sup class="red">*</sup></label><input type="text" id="firstNameTrial" name="firstNameTrial" value="" required/>
                        </p>
                        <p>
                            <label for="lastNameTrial">Last name<sup class="red">*</sup></label><input type="text" id="lastNameTrial" name="lastNameTrial" value="" required/>
                        </p>
                        <p>
                            <label for="ageTrial">Age</label><input type="text" id="ageTrial" name="ageTrial" value=""/>
                        </p>

                        <p>
                            <label for="celebrityTrial" style="display: block; width: auto; margin-bottom: 5px;">Name the celebrity you would most like to meet, and why?<sup class="red">*</sup></label>
                            <textarea id="celebrityTrial" name="celebrityWhyTrial" style="width: 430px; height: 3em;" required></textarea>
                        </p>

                        <p class="ajaxFormBeforeValid">
                            <input type="submit" value="Submit now" class="redButton"/><span class="ajaxFormWait"></span><span class="ajaxFormError error"></span>
                        </p>
                        <div class="ajaxFormValid">
                            <p>
                                Thank you! Your local consultant will contact you soon. 'Like' us while you wait for all the latest VIP offers and promotions!
                            </p>
                        </div>
                        <p>
                            <small>
                                <sup class="red">*</sup>These are mandatory fields.
                            </small>

                        </p>

                    </div>
                </form>
            </div>
        </article>
    </div>
    <?php include_once('includes/footer.inc.php'); ?>

这是jquery部分

/*************************
plugin to manage ajax forms
*************************/
(function( $ ){

    var methods = {
        init : function( options ) {

            return this.each(function(){

                var $this = $(this),
                    data = $this.data('ajaxForm'),
                    ajaxForm = $('<div />', {
                        text : $this.attr('title')
                    });

                // If the plugin hasn't been initialized yet
                if ( ! data ) {

                    $(this).data('ajaxForm', {
                        target : $this,
                        ajaxForm : ajaxForm
                    });

                    //get the spinner, the valid box and the error box
                    var mySpinner = $this.find('.ajaxFormWait');
                    var myValid = $this.find('.ajaxFormValid');
                    var myError = $this.find('.ajaxFormError');
                    var myBeforeValid = $this.find('.ajaxFormBeforeValid');

                    myError.hide();
                    mySpinner.hide();

                    //add an event to send the form via AJAX
                    $this.submit(function(){
                        // get all the inputs into an array.
                        var $inputs = $this.find(':input:not([type="submit"], [type="button"])');

                        // not sure if you wanted this, but I thought I'd add it.
                        // get an associative array of just the values.
                        var values = {};
                        $inputs.each(function() {
                            if (this.type == "radio" || this.type == "checkbox"){
                                if($(this).is(':checked')){
                                    if(typeof(values[this.name]) === 'undefined'){
                                        values[this.name] = $(this).val();
                                    }else{
                                        values[this.name] += ', '+($(this).val());
                                    }
                                }
                            } else
                                values[this.name] = $(this).val();
                        });

                        function defineTheInvalidsFields(fieldsList){
                            for(var i in fieldsList){
                                if(fieldsList[i] == 'closestStudio'){
                                    $this.find('[name="'+fieldsList[i]+'"]').parent().addClass('invalid');
                                }else{
                                    $this.find('[name="'+fieldsList[i]+'"]').addClass('invalid');
                                }
                            }
                        }


                        //send an AJAX request
                        $.ajax({
                            url: $this.attr('action'),
                            dataType: 'json',
                            data: values,
                            beforeSend: function(){
                                mySpinner.show();
                            },
                            success: function(result){
                                mySpinner.hide();
                                $this.find('.invalid').removeClass('invalid');

                                //error management
                                if(typeof(result.valid) === 'undefined'){

                                    if(result.multipleSend){ //if multiple send
                                        myError.html('Your request is already sent.');
                                    }else if(result.required){ //if fields are required
                                        defineTheInvalidsFields(result.required);
                                        myError.html('The fields in red are required.');
                                    }else if(result.format){ //if the forma is incorrect
                                        defineTheInvalidsFields(result.format);
                                        myError.html('The fields in red have invalid content.');
                                    }else if(result.loginInvalid){
                                        myError.html(result.loginInvalid);
                                    }else{
                                        myError.html('An unknown error occured.');
                                    }
                                    myValid.slideUp(300);
                                    myError.slideDown(300);
                                }else if(typeof(result.loginUrl) !== 'undefined'){
                                    window.location.href = result.loginUrl;
                                }else{
                                    if(result.valid || result.valid == 'true'){
                                        if($('#inputFreetrialFitnessFirst').length){
                                            myBeforeValid.slideUp(300);
                                            myError.slideUp(300);
                                            myValid.slideDown(300);
                                        }else{
                                            window.location = '/free-trial-thank-you/';
                                        }
                                    }else{
                                        myError.html('There was an error sending your details. Please try again.');
                                        myValid.slideUp(300);
                                        myError.slideDown(300);
                                    }
                                }
                            }
                        });

                        return false;
                    });

                    //special case for the heardAbout
                    $('#heardAbout').change(function(){
                        if($(this).find('option:selected').attr('value') == 'Other'){
                            $('#otherHeardAbout').slideDown(300);
                        }else{
                            $('#otherHeardAbout').slideUp(300);
                        }
                    });
                }
            });
        },
        destroy : function(){
            return this.each(function(){
                var $this = $(this),
                    data = $this.data('ajaxForm');

                // Namespacing FTW
                $(window).unbind('.ajaxForm');
                data.ajaxForm.remove();
                $this.removeData('ajaxForm');
            })
        }
    };

    $.fn.ajaxForm = function( method ) {

        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.ajaxForm' );
        }    

    };

})( jQuery );

表单被发送到另一个页面,有没有办法定位特定的 div 并添加带有名称的自定义消息。

if(result.valid || result.valid == 'true'){
     if($('#inputFreetrialFitnessFirst').length){
          myBeforeValid.slideUp(300);
          myError.slideUp(300);
          myValid.slideDown(300);
     }else{
          window.location = '/free-trial-thank-you/';
       }
}else{
    myError.html('There was an error sending your details. Please try again.');
    myValid.slideUp(300);
    myError.slideDown(300);
}
4

2 回答 2

0

One way that I can think of is using session to store the information.

  1. At freetrial.php store the form information in session like this:

    session_start();
    $_SESSION['firtNameTrial'] = $_GET['firstNameTrial'];
    
  2. On ajax success, redirect the page to the thank you page using javascript windows.location.replace:

      // similar behavior as an HTTP redirect
      window.location.replace("http://yourwebpageaddress.com/thankyou.php");
    
  3. Get the session on the thank you page. If you don't know, you may look at this example: Click here for session example

于 2013-08-09T08:02:24.817 回答
0

if You wand to fetch specific element div via ajax call You can do like this:

$.ajax({
url: 'page.html',
success: function(data) {
    item=$(data).filter('#specific_element');
    $(selector).html(item); 
 }
});

If You wand to redirect to other page using

window.location = '/free-trial-thank-you/';

and then show data on a different page You should pass needet parameters like

 window.location = '/free-trial-thank-you/page.php?name1=Daniel&name2=Pablo;

and on different site show parameters using _GET or _POST for example: echo $_GET['name1'] or echo $_POST['name1'] if You use PSOT method in Your Ajax request

于 2013-08-09T08:03:15.963 回答