无论是否需要/错误处理尝试,PHP表单都会提交

时间:2014-02-15 15:38:56

标签: forms function required

新手在这里

我目前正在尝试构建一个简单的联系表单,以了解如何最好地构建一个表单。我试过要几个字段(名字,电子邮件,你喜欢垃圾邮件单选按钮)。所需的html5元素对我不起作用,或者我没有根据我在这里阅读的内容,谷歌和其他地方正确实现它。同样,如果有人试图提交消息,则不会显示错误/要求消息。相反,即使没有任何内容输入,表单也会继续提交。我昨天建了这个,现在已经处理了大约24小时的错误,我希望能在这里找到一些帮助。

我目前没有返回任何错误,表单会发送所有按预期输入的信息,但如果没有输入任何信息,它也会发送,这对我来说似乎不是最好的形式。我已经尝试构建一个函数来帮助实现所需的功能,但这似乎也没有用。

同样,我是新人,所以如果我的错误显而易见且愚蠢,请帮助他们理解他们,我们将不胜感激。

current example can be found here

我的代码:

<!-- For PoohPot -->
 <style TYPE="text/css">
input[type="text"]#website { display: none; }
input#website { display: none; }
.vSmall {font-size: 50%; text-align: center;}
 </style>

 <script>
$('form').submit(function(){
        if ($('input#website').val().length != 0) {
            return false;
        }
});
 </script>


 <?php
//template.php
include '_inc/config.php';//holds arrays, et al.
include '_inc/functions.php';//holds functions that call arrays, etc

include '_inc/head.php';//Begin Document

//include '_inc/header.php';//Begin Content
?>

 <article class="container box style3">
<header>
    <img src="_img/icons/icon_lg-max-o-matic2.png" alt="php icon">
    <h2><a href="index.php"><?=NAME_SITE?></a></h2>
    <p>Spam Survey</p>
</header>

<?php
 $to = "chezshire@gmail.com";
 $subject="Spam Survey";

//----- end conf ----- //

// define variables and set to empty values
 $nameErr = $emailErr = $spamErr = "";
 $userName = $userEmail = $userMessage = $website = "";


 if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  if (empty($_POST["userName"]))
    {$nameErr = "* Name is required";}
  else
    {$name = testInput($_POST["userName"]);}

  if (empty($_POST["userEmail"]))
    {$emailErr = "* Email is required";}
  else
    {$email = testInput($_POST["userEmail"]);}

  if (empty($_POST["spamErr"]))
    {$emailErr = "* You must select an option";}
  else
    {$email = testInput($_POST["userEmail"]);}

  if (empty($_POST["userMessage"]))
    {$comment = "";}
  else
    {$comment = testInput($_POST["userMessage"]);}
}


 if(isset($_POST['userName']))
{//data is submitted, show it
//echo $_POST['userName'];

$text = 'From: ' . $_POST['userName'] . PHP_EOL;
//$text .= $_POST['Comments']; //add on to text
$text .= process_post(); // uses proces_post to handle comments and stuff
$from = $_POST['userEmail'];

/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/

//domain spoofing controls
$headers = 'From: noreply@max-o-matic.com' . PHP_EOL . //from where formhandler sits
   'Reply-To: ' . $from . PHP_EOL . //where replies are to go to
   'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $text, $headers); //from becomes headers

echo '<div class="row">
            <div class="12u">
                <p>Thanxs!</p>

                <p>
                    <br /><a href="' .  THIS_PAGE . '">RESET</a>
                </p>
            </div>
        </div>
        </article>
        ';
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/

}else{//no data, show form

echo '
    <form action="' .  THIS_PAGE . '" method="post">
        <div class="row half">
            <div class="6u">
                <input type="text" class="text" name="userName" placeholder="Name?" required autofocus />
                <span class="error"><?php echo $nameErr;?></span>
            </div>
            <div class="6u">
                <input type="text" class="text" name="userEmail" placeholder="Email" required />
                <span class="error"><?php echo $emailErr;?></span>
            </div>
        </div>

        <div class="row half">
            <div class="6u">
                Do you like spam?<br />
                    <input type="radio" name="Please_Spam_Me" value="YES!" required value="1" />Yes<br />
                    <input type="radio" name="Please_Spam_Me" value="NO!" />No<br />
                    <span class="error"><?php echo $spamErr;?>
            </div>
            <div class="6u">
                How much Spam is too much?<br />
                <input type="checkbox" name="checkbox-group" class="group-required" id="checkbox-group-id" value="6+" >6 or more<br />
                <input type="checkbox" name="checkbox-group" value="2+">2 to 5<br />
                <input type="checkbox" name="checkbox-group" value="Any">ANY<br /><br />
            </div>
        </div>

        <!--For Winnie the Pooh -->
        <input id="website" name="website" type="text" value"Website" />

        <div class="row half">
            <div class="12u">
                <textarea name="userMessage" placeholder="What is your opinion on Spam?"></textarea>
            </div>
        </div>

        <div class="row">
            <div class="12u">
                <ul class="actions">
                    <li><a href="' .  THIS_PAGE . '" class="button form">SEND!</a></li>

                    <input type="submit" />
                </ul>

                <p class="vSmall">
                    * Please note, instead of a reCaptcha I build my own \'honeypot\' from scratch.
                </p>
            </div>
        </div>
    </form>
</article>
';

}

 function testInput($data)
{
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);
 return $data;
}

// this is a function
 function process_post()
{//loop through POST vars and return a single string
$myReturn = ''; //set to initial empty value

foreach($_POST as $varName=> $value)//$_POST is an array

{#loop POST vars to create JS array on the current page - include email
     $strippedVarName = str_replace("_"," ",$varName);#remove underscores
    if(is_array($_POST[$varName]))
     {#checkboxes are arrays, and we need to collapse the array to comma separated string!
         $myReturn .= $strippedVarName . ": " . implode(",",$_POST[$varName]) . PHP_EOL;
     }else{//not an array, create line
         $myReturn .= $strippedVarName . ": " . $value . PHP_EOL;
     }
}
return $myReturn;
}

 include "_inc/footer.php";//End Content/Document
?>

1 个答案:

答案 0 :(得分:0)

如果您尝试使用HTML5内容,则需要HTML5文档类型。将其放在输出的开头,看看会发生什么:

<!DOCTYPE html>

此外,拼写错误value"Website"应为value="Website"