简体   繁体   中英

How to validate a php form with java script, that is on everypage of my site

The problem i'm having is Im trying to get the contact.php to validate a contact form that is now in the bottom of every page in my site. It worked great with just validating one page and having a hard URL to take them back to. the troubleing code is this.

/* In case of no valid input go back  */
if ($p1==""||$p2==""||$p3==""||$p5==""||$n0!=0||$f0<7||$NL==0||$p7=="") {
    header($_SERVER['HTTP_REFERER'].$report); 
}else{$report='noerror';}

Ive also tryed this:

/* In case of no valid input go back  */
if ($p1==""||$p2==""||$p3==""||$p5==""||$n0!=0||$f0<7||$NL==0||$p7=="") {
    header($_SERVER['PHP_SELF'].$report); 
}else{$report='noerror';}

This is the original code that worked for one page:

/* In case of no valid input go back  */
if ($p1==""||$p2==""||$p3==""||$p5==""||$n0!=0||$f0<7||$NL==0||$p7=="") {
   header('Location:estimate_page.php?'.$report);
}else{$report='noerror';}

I could just make a new "contact.php" page for every page that has the form in the footer, but thats stupid if i can just make this work. Any ideas

You are thinking in the right direction with javascript/jQuery -- it will allow you to catch the invalid fields before submitting / changing pages. Much better user experience. Only submit when fields are validated.

Here is a link for how to do simple field validation testing with jQuery:

jsFiddle Demo

HTML:

One: <input type="text" id="f1"><br />
Two: <input type="text" id="f2"><br />
Three: <input type="text" id="f3"><br />
Four: <input type="text" id="f4"><br />
<br />
<input type="button" id="mybutt" value="Go" />

javascript/jQuery:

var chkFld, arrAll = {'One':'f1','Two':'f2','Three':'f3','Four':'f4'};

$('#mybutt').click(function() {
    var errMsg='', badFlds='', firstBad='';
    for(var key in arrAll){
        chkFld = '#'+arrAll[key];
        $(chkFld).removeClass('error');
        if ($(chkFld).val() ==''){
            $(chkFld).addClass('error');
            //alert('Please complete field ' + arrAll[key] );
            errMsg += '*' + key + '\n';
            if (firstBad=='') firstBad=chkFld;
        }
    }
    if (errMsg != '') {
        alert(errMsg);
        $(firstBad).focus();
    }
}); //END mybutt.click

Note that the above example uses jQuery, so you must reference the jQ libraries (usually inside the head tags, something like this:)

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

EDIT:

Here is what the full HTML would look like:

<?php
    //Any initial PHP code goes here
?>

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

        <script type="text/javascript">
            //javascript
        var chkFld, arrAll = {'One':'f1','Two':'f2','Three':'f3','Four':'f4'};
        $(document).ready(function() {

                $('#mybutt').click(function() {
                    var errMsg='', badFlds='', firstBad='';
                    for(var key in arrAll){
                        chkFld = '#'+arrAll[key];
                        $(chkFld).removeClass('error');
                        if ($(chkFld).val() ==''){
                            $(chkFld).addClass('error');
                            //alert('Please complete field ' + arrAll[key] );
                            errMsg += '*' + key + '\n';
                            if (firstBad=='') firstBad=chkFld;
                        }
                    }
                    if (errMsg != '') {
                        alert(errMsg);
                        $(firstBad).focus();
                        return false;
                    }

                    //If it gets to here, it passed validation
                        $('#yourForm').submit();
                }); //END mybutt.click()

            }); //END $(document).ready()

        </script>
    </head>
<body>

    <form id="yourForm">
        One: <input type="text" id="f1" /><br />
        Two: <input type="text" id="f2" /><br />
        Three: <input type="text" id="f3" /><br />
        Four: <input type="text" id="f4" /><br />
    </form>
    <input type="button" id="mybutt" value="Click Me">

</body>
</html>

Note that, using the above scenario, no further PHP code would execute until the form is submitted, using the manual $('#yourForm').submit(); statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM