首页 \ 问答 \ 如何将我的PHP验证表单重定向到另一个页面(How to redirect my php validation form to another page)

如何将我的PHP验证表单重定向到另一个页面(How to redirect my php validation form to another page)

我真的在这里挣扎! 我有一个联系表单,我将包括使用jquery插件,并使用PHP作为备份。 这种形式还没有完全定制,但我现在遇到麻烦了几天。 当我使用MAMP并单击提交时,验证工作完美! 在php和jquery中。 然后重定向到谷歌(最终一些其他网站)。 当我将它上传到TEST文件夹下的公司网站时。 测试它我得到验证,一切都很完美。 当我在jquery中修复错误后提交时,它会按照它应该推送谷歌。 当我拿走jquery来测试php时,验证工作然后当我点击提交时它就停留在同一个index.php页面上! 我一直在试着尝试这么多不同的接触验证表格,这个似乎是最好的。 我是一名平面设计师,有足够的网络经验让我遇到麻烦!:)有人可以帮忙提一些建议吗?

这是我的index.php

<?php include('header_contact.php') ?>


<?php include('testval.php');?>




<div id="section1">

CONTACT FORM

<div id="contact-form">




<!--<form id="contactform" name="contactform" action="<!--?php echo $_SERVER['PHP_SELF']; ?>" method="post" >-->
<form id="contactform" name="contactform" action="index.php" method="post" >



    <!--?php
    if($sent === true) {
        echo "<h2 class='success'>Thanks, your message has been sent successfully</h2>";
    } elseif($hasError === true) {
        echo '<ul class="errorlist">';
        foreach($errorArray as $key => $val) {
            echo "<li>" . ucfirst($key) . " field error - $val</li>";
        }
        echo '</ul>';
    }
    ?>
    -->

    <input name="Name" type="text" id="Name" placeholder="NAME" value="<?php echo (isset($Name) ? $Name : ""); ?>"><span class='text_error'><?php echo $nameErr;?></span>

    <input name="Company" type="text" id="Company" placeholder="COMPANY" value=''>

    <input name="Phone" type="tel" id="Phone" placeholder="PHONE" value='<?php echo htmlentities($Phone);?>'>
    <span class='text_error'><?php echo $phoneErr;?></span>

    <input name="Email" type="Email" placeholder="EMAIL" value="<?php echo (isset($Email) ? $Email : ""); ?>">
    <span class='text_error'><?php echo $emailErr;?></span>

    <input name="webdesign" type="checkbox" value="Yes" /><label>Web Design: </label><br />

    <input name="webhosting" type="checkbox" value="Yes" /><label>Web Hosting: </label><br />

    <input name="wordpressdesign" type="checkbox" value="Wordpress Design" /><labeel>Wordpress Design: </labeel><br />

    <textarea id="Describe" class="mess" name="Describe" type="text" placeholder="DESCRIBE WHAT YOU NEED"><?php echo isset($_POST['Describe'])? $_POST['Describe'] : ''; ?></textarea>
    <span class='text_error'><?php echo $describeErr;?></span>

    <!--?php if(in_array('Describe', $validation)): ?><span class="error"><!--?php echo $error_messages['Describe']; ?></span><!--?php endif; ?>
    <span class='text_error'>
    -->

    <input class="input-upload" type="file" name="Upload File">

    <input name="Human" placeholder="5 + 3 = ?" value="<?php echo isset($_POST['Human'])? 
    $_POST['Human'] : ''; ?>">
    <span class='text_error'><?php echo $humanErr;?></span>

    <!--
    <!--?php if(in_array('Human', $validation)): ?><span class="error"><--?php echo $error_messages['Human']; ?></span><--?php endif; ?>
    -->

    <input id="submit" name="submit" type="submit" value="submit">




</form>






</div><!--END CONTACT FORM-->

</div><!--END SECTION1-->    







<?php include('footer.php') ?>

这是我的testval.php

<?php

session_start();
    // define variables and set to empty values
    $nameErr = $emailErr = $phoneErr = $describeErr = $humanErr = "";
    $Name = $Email = $Phone = $Describe = $Human = "";

    $hasError = false;
    $sent = false;

    if(isset($_POST['submit'])) {
        $Name            = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES));
        $Email           = trim($_POST['Email']);
        $Describe        = trim(htmlspecialchars($_POST['Describe'], ENT_QUOTES));
        $Phone           = trim($_POST['Phone']);
        $Email           = trim($_POST['Email']);
        $webdesign       = trim($_POST['webdesign']);
        $webhosting      = trim($_POST['webhosting']);
        $wordpressdesign = trim($_POST['wordpressdesign']);
        $Human           = trim($_POST['Human']);
        $Number          = 8;

        $fieldsArray = array(
            'Name' => $Name,
            'Email' => $Email,
            'Describe' => $Describe,
            'Phone' => $Phone,
            'Human' => $Human
        );

        $errorArray = array();

        foreach($fieldsArray as $key => $val) {
            switch ($key) {
                case 'Name':
                    if(empty($val)) {
                        $hasError = true;
                        $nameErr = "Please enter your name.";
                    }
                case 'Name':
                    if (!preg_match("/^[a-zA-Z ]*$/", ($val))) {
                        $hasError = true;
                        $nameErr = "The value must be alphanumeric."; 
                    }
                    break;
                case 'Phone':
                    if (!preg_match("/^[0-9]+$/", ($val))) {
                        $hasError = true;
                        $phoneErr = "Only numbers and white space allowed.";
                    }
                case 'Phone':
                    if(empty($val)) {
                        $hasError = true;
                        $phoneErr = "Phone is required.";
                    }
                    break;
                case 'Email':
                    if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
                        $hasError = true;
                        $emailErr = "Email is required.";
                    } else {
                        $Email = filter_var($Email, FILTER_SANITIZE_EMAIL);
                    }
                    break;
                case 'Describe' :
                    if (strlen($val) < 25) {
                        $hasError = true;
                        $describeErr = "Please enter at least 25 characters. This way I know more specifically what you need!";
                    }
                case 'Describe' :
                    if(empty($val)) {
                        $hasError = true;
                        $describeErr = "Message is required. This way I know what you need!";
                    }
                    break;
                case 'Human':
                    if (!preg_match("/[^\d]?8[^\d]?/", ($val))) {
                        $hasError = true;
                        $humanErr = "Not the right answer";
                    }
                case 'Human':
                    if (!preg_match("/^[0-9]+$/", ($val))) {
                        $hasError = true;
                        $humanErr = "Must be a number";
                    }
                case 'Human':
                    if(empty($val)) {
                        $hasError = true;
                        $humanErr = "Are you human?";
                    }
                    break;
            }
        }

        //CHECK BOX WRITE UP
        if (isset($_POST['webdesign'])) {
            $checkBoxValue = "yes";
             //is checked
        } else {
            $checkBoxValue = "no";
             //is unchecked
        }

        if (isset($_POST['webhosting'])) {
        $checkBoxValue = "yes";
            //is checked
         } else {
            $checkBoxValue = "no";
            //is unchecked
        }

        if (isset($_POST['wordpressdesign'])) {
        $checkBoxValue = "yes";
            //is checked
        } else {
            $checkBoxValue = "no";
            //is unchecked
        }

        //Validation Success!
        //Do form processing like email, database etc here
        if($hasError !== true) {
            $priority = $_POST['priority'];
            $type = $_POST['type'];
            $message = $_POST['message'];
            $formcontent=" From: $Name \n Company: $Company \n Email: $Email \n Phone: $Phone \n Describe: $Describe \n Web Design: $webdesign \n Web Hosting: $webhosting \n Wordpress Design: $wordpressdesign ";
            $recipient = "creative@griffard.com";
            $subject = "Contact form test with Jquery and PHP";
            $mailheader = "From: $Email \r\n";
            mail($recipient, $subject, $formcontent, $mailheader);
            header("Location:http://www.google.com");
            exit();       
        }
    }

?><!--END PHP-->

我现在拿出jquery来测试php以使其正常工作,但是当它在index.php页面中时它正好在下面

这是代码。

<script type="text/javascript">
jQuery(document).ready(function($) {
    $("#contactform").validate({
        rules: {
            Name: {
                required: true,
                minlength: 4
            },
            Email: {
                required: true,
                email: true
            },
            Describe: {
                required: true,
                minlength: 25
            },
            Phone: {
                required: true,
                number: true,
                minlength: 7,
                maxlength: 15
            },
            Human: {
                required: true,
                number: true,
                maxlength: 1,
                range: [8,8]
            }
        },
        messages: {
            Name: {
                required: "Please enter your name",
                minlength: "Your name seems a bit short doesn't it?"
            },
            Email: {
                required: "We need your email address to contact you",
                email: "Please enter a valid email address"
            },
            Describe: {
                required: "Please enter your message",
                minlength: "Please enter at least 25 characters. This way I know more specifically what you need!"
            },
            Phone: {
                required: "Please enter your phone number",
                number: "Phone has to be numbers",
                minlength: "Your phone number should be longer",
                maxlength: "Number must be less!"
            },
            Human: {
                required: "Are you human?",
                number: "has to be a number",
                maxlength: "That's to long!",
                range: "Please enter the correct number"
            }
        },
        errorElement: "div", //TAKE CONTROL OF ERROR MESSAGE IN CSS

        //IF I CHOOSE TO ADD CSS AND WRAP ERROR WITH BOX

        wrapper: "div", //WRAPPER AROUND THE ERROR MESSAGE
        errorPlacement: function(error, element) {
            error.insertAfter(element);
            error.addClass('message'); //ADD A CLASS TO THE WRAPPER
            /*
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
            */
    }
    });
});
</script>

很感谢任何形式的帮助!


I'm really struggling here! I have a contact form which I will include that uses the jquery plugin, and uses php as a backup. This form is not all the way customized yet, but I am having trouble for a couple of days now. When I use MAMP and click submit the validation works perfectly! in both php and jquery. Then redirects me to google(eventually some other website). When I upload it to my companies site under a TEST folder. to test it I get the validation and everything working perfectly. When I submit after fixing errors in jquery it pushes to google like it is supposed to. When I take away the jquery to test the php, the validation works then when I hit submit it just stays right there on the same index.php page! I have been pulling my hair out trying so many different contact validation forms and this one seems to be working the best. I am a graphic designer with enough web experience to get me in trouble with PHP!:) Can anybody help with some advice?

Here is my index.php

<?php include('header_contact.php') ?>


<?php include('testval.php');?>




<div id="section1">

CONTACT FORM

<div id="contact-form">




<!--<form id="contactform" name="contactform" action="<!--?php echo $_SERVER['PHP_SELF']; ?>" method="post" >-->
<form id="contactform" name="contactform" action="index.php" method="post" >



    <!--?php
    if($sent === true) {
        echo "<h2 class='success'>Thanks, your message has been sent successfully</h2>";
    } elseif($hasError === true) {
        echo '<ul class="errorlist">';
        foreach($errorArray as $key => $val) {
            echo "<li>" . ucfirst($key) . " field error - $val</li>";
        }
        echo '</ul>';
    }
    ?>
    -->

    <input name="Name" type="text" id="Name" placeholder="NAME" value="<?php echo (isset($Name) ? $Name : ""); ?>"><span class='text_error'><?php echo $nameErr;?></span>

    <input name="Company" type="text" id="Company" placeholder="COMPANY" value=''>

    <input name="Phone" type="tel" id="Phone" placeholder="PHONE" value='<?php echo htmlentities($Phone);?>'>
    <span class='text_error'><?php echo $phoneErr;?></span>

    <input name="Email" type="Email" placeholder="EMAIL" value="<?php echo (isset($Email) ? $Email : ""); ?>">
    <span class='text_error'><?php echo $emailErr;?></span>

    <input name="webdesign" type="checkbox" value="Yes" /><label>Web Design: </label><br />

    <input name="webhosting" type="checkbox" value="Yes" /><label>Web Hosting: </label><br />

    <input name="wordpressdesign" type="checkbox" value="Wordpress Design" /><labeel>Wordpress Design: </labeel><br />

    <textarea id="Describe" class="mess" name="Describe" type="text" placeholder="DESCRIBE WHAT YOU NEED"><?php echo isset($_POST['Describe'])? $_POST['Describe'] : ''; ?></textarea>
    <span class='text_error'><?php echo $describeErr;?></span>

    <!--?php if(in_array('Describe', $validation)): ?><span class="error"><!--?php echo $error_messages['Describe']; ?></span><!--?php endif; ?>
    <span class='text_error'>
    -->

    <input class="input-upload" type="file" name="Upload File">

    <input name="Human" placeholder="5 + 3 = ?" value="<?php echo isset($_POST['Human'])? 
    $_POST['Human'] : ''; ?>">
    <span class='text_error'><?php echo $humanErr;?></span>

    <!--
    <!--?php if(in_array('Human', $validation)): ?><span class="error"><--?php echo $error_messages['Human']; ?></span><--?php endif; ?>
    -->

    <input id="submit" name="submit" type="submit" value="submit">




</form>






</div><!--END CONTACT FORM-->

</div><!--END SECTION1-->    







<?php include('footer.php') ?>

Here is my testval.php

<?php

session_start();
    // define variables and set to empty values
    $nameErr = $emailErr = $phoneErr = $describeErr = $humanErr = "";
    $Name = $Email = $Phone = $Describe = $Human = "";

    $hasError = false;
    $sent = false;

    if(isset($_POST['submit'])) {
        $Name            = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES));
        $Email           = trim($_POST['Email']);
        $Describe        = trim(htmlspecialchars($_POST['Describe'], ENT_QUOTES));
        $Phone           = trim($_POST['Phone']);
        $Email           = trim($_POST['Email']);
        $webdesign       = trim($_POST['webdesign']);
        $webhosting      = trim($_POST['webhosting']);
        $wordpressdesign = trim($_POST['wordpressdesign']);
        $Human           = trim($_POST['Human']);
        $Number          = 8;

        $fieldsArray = array(
            'Name' => $Name,
            'Email' => $Email,
            'Describe' => $Describe,
            'Phone' => $Phone,
            'Human' => $Human
        );

        $errorArray = array();

        foreach($fieldsArray as $key => $val) {
            switch ($key) {
                case 'Name':
                    if(empty($val)) {
                        $hasError = true;
                        $nameErr = "Please enter your name.";
                    }
                case 'Name':
                    if (!preg_match("/^[a-zA-Z ]*$/", ($val))) {
                        $hasError = true;
                        $nameErr = "The value must be alphanumeric."; 
                    }
                    break;
                case 'Phone':
                    if (!preg_match("/^[0-9]+$/", ($val))) {
                        $hasError = true;
                        $phoneErr = "Only numbers and white space allowed.";
                    }
                case 'Phone':
                    if(empty($val)) {
                        $hasError = true;
                        $phoneErr = "Phone is required.";
                    }
                    break;
                case 'Email':
                    if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
                        $hasError = true;
                        $emailErr = "Email is required.";
                    } else {
                        $Email = filter_var($Email, FILTER_SANITIZE_EMAIL);
                    }
                    break;
                case 'Describe' :
                    if (strlen($val) < 25) {
                        $hasError = true;
                        $describeErr = "Please enter at least 25 characters. This way I know more specifically what you need!";
                    }
                case 'Describe' :
                    if(empty($val)) {
                        $hasError = true;
                        $describeErr = "Message is required. This way I know what you need!";
                    }
                    break;
                case 'Human':
                    if (!preg_match("/[^\d]?8[^\d]?/", ($val))) {
                        $hasError = true;
                        $humanErr = "Not the right answer";
                    }
                case 'Human':
                    if (!preg_match("/^[0-9]+$/", ($val))) {
                        $hasError = true;
                        $humanErr = "Must be a number";
                    }
                case 'Human':
                    if(empty($val)) {
                        $hasError = true;
                        $humanErr = "Are you human?";
                    }
                    break;
            }
        }

        //CHECK BOX WRITE UP
        if (isset($_POST['webdesign'])) {
            $checkBoxValue = "yes";
             //is checked
        } else {
            $checkBoxValue = "no";
             //is unchecked
        }

        if (isset($_POST['webhosting'])) {
        $checkBoxValue = "yes";
            //is checked
         } else {
            $checkBoxValue = "no";
            //is unchecked
        }

        if (isset($_POST['wordpressdesign'])) {
        $checkBoxValue = "yes";
            //is checked
        } else {
            $checkBoxValue = "no";
            //is unchecked
        }

        //Validation Success!
        //Do form processing like email, database etc here
        if($hasError !== true) {
            $priority = $_POST['priority'];
            $type = $_POST['type'];
            $message = $_POST['message'];
            $formcontent=" From: $Name \n Company: $Company \n Email: $Email \n Phone: $Phone \n Describe: $Describe \n Web Design: $webdesign \n Web Hosting: $webhosting \n Wordpress Design: $wordpressdesign ";
            $recipient = "creative@griffard.com";
            $subject = "Contact form test with Jquery and PHP";
            $mailheader = "From: $Email \r\n";
            mail($recipient, $subject, $formcontent, $mailheader);
            header("Location:http://www.google.com");
            exit();       
        }
    }

?><!--END PHP-->

I took out the jquery for now to test the php to get it working, but when it is in the index.php page it is right below

here is the code for that.

<script type="text/javascript">
jQuery(document).ready(function($) {
    $("#contactform").validate({
        rules: {
            Name: {
                required: true,
                minlength: 4
            },
            Email: {
                required: true,
                email: true
            },
            Describe: {
                required: true,
                minlength: 25
            },
            Phone: {
                required: true,
                number: true,
                minlength: 7,
                maxlength: 15
            },
            Human: {
                required: true,
                number: true,
                maxlength: 1,
                range: [8,8]
            }
        },
        messages: {
            Name: {
                required: "Please enter your name",
                minlength: "Your name seems a bit short doesn't it?"
            },
            Email: {
                required: "We need your email address to contact you",
                email: "Please enter a valid email address"
            },
            Describe: {
                required: "Please enter your message",
                minlength: "Please enter at least 25 characters. This way I know more specifically what you need!"
            },
            Phone: {
                required: "Please enter your phone number",
                number: "Phone has to be numbers",
                minlength: "Your phone number should be longer",
                maxlength: "Number must be less!"
            },
            Human: {
                required: "Are you human?",
                number: "has to be a number",
                maxlength: "That's to long!",
                range: "Please enter the correct number"
            }
        },
        errorElement: "div", //TAKE CONTROL OF ERROR MESSAGE IN CSS

        //IF I CHOOSE TO ADD CSS AND WRAP ERROR WITH BOX

        wrapper: "div", //WRAPPER AROUND THE ERROR MESSAGE
        errorPlacement: function(error, element) {
            error.insertAfter(element);
            error.addClass('message'); //ADD A CLASS TO THE WRAPPER
            /*
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
            */
    }
    });
});
</script>

Any help is very much appreciated!


原文:https://stackoverflow.com/questions/39645213
更新时间:2023-12-19 21:12

最满意答案

当然,你需要做的就是使用white-space: nowrap;

基本上,这会将元素的文本设置为不换行到新行,这会导致容器被推出到文本内容的宽度。 这样你可以摆脱你巨大的宽度设置,它会自动调整大小。

看例子:

http://jsfiddle.net/YD4z2/


Sure, all you need to do is use white-space: nowrap;

Basically, this sets an element's text not to wrap to a new line, which causes the container to be pushed out to the width of the text content. This way you can get rid of your huge width setting, and it will automatically be sized.

See example:

http://jsfiddle.net/YD4z2/

相关问答

更多
  • 可能是CSS的可能,但在我看来,你需要使用一些JavaScript来解决这个问题: 这是诀窍: var sidebar = $('.sidebar'); sidebar.scroll(function(e) { sidebar.find('.title').css({ marginLeft: sidebar.scrollLeft() }); }); 我们将在.title元素上添加一些margin-left等于水平滚动的像素数。 这将始终显示标题栏粘贴在可滚动元素的右侧。 $(functi ...
  • 这里有一个简单的解决方法,在哪里将.content显示为inline-block并使用margin而不是padding 这适用于Firefox,Edge,IE11,但不适用于Chrome。 幸运的是,只需添加一个填充,例如padding-right: 0.1px; ,以.main修复这个问题。 更新小提琴 堆栈片段 body, html { height: 100%; display: flex; flex-direction: column; flex: 1; paddin ...
  • 如果你删除浮动,它可以工作。 http://jsbin.com/izoca/2/edit 与浮动,它只有在有些内容例如  it works if you remove floating. http://jsbin.com/izoca/2/edit with floats it only works if theres some content e.g.  
  • 你可以在jQuery中使用filter 。 这样您就可以为“空”设置自己的定义。 $(function() { var empties = $('div').filter(function(){ return $(this).html().trim() == ''; }); alert(empties.length); empties.remove(); });