带有javascript表单验证的PHP表单请求

时间:2017-04-24 03:32:12

标签: javascript php html forms

所以我有一个javascript,可以在

上执行简单的表单验证
<form method="get" name="formPG" onsubmit="return (validateForm());" action="filter.php">
<p>Noun <input id="iNoun" name="fNoun" type="text" /></p>

<p><br />
Pronoun <input id="iPronoun" name="fPronoun" type="text" /></p>

<p><br />
Verb <input id="iVerb" name="fVerb" type="text" /></p>

<p><br />
Adverb <input id="iAdverb" name="fAdverb" type="text" /></p>

<p><br />
Adjective <input id="iAdjective" name="fAdjective" type="text" /></p>

<p><br />
Silly Word <input id="iSillyWord" name="fSillyWord" type="text" /></p>

<p><br />
Magic Spell <input id="iMagic" name="fMagic" type="text" /></p>

<p><br />
<input type="submit" value="submit" /></p>
</form>

javascript说&#34;嘿,这是填写&#34;或者&#34;嘿,这还没有填写,请填写“#34;。

现在,我有一个PHP文件,用于收集HTML标记中的信息

<?php
    require "start.php"
?>

<div class="work"><</div>
<?php
if(!empty($_GET['fNoun']) && !empty($_GET['fPronoun']) && !empty($_GET['fVerb']) && !empty($_GET['fAdverb']) && !empty($_GET['fAdjective']) && !empty($_GET['fSillyWord']) && !empty($_GET['fMagic']))
{
    $noun = filter_input(INPUT_GET, 'fNoun', FILTER_SANITIZE_STRING);
    $pronoun = filter_input(INPUT_GET, 'fPronoun', FILTER_SANITIZE_STRING);
    $verb = filter_input(INPUT_GET, 'fVerb', FILTER_SANITIZE_STRING);
    $adverb = filter_input(INPUT_GET, 'fAdverb', FILTER_SANITIZE_STRING);
    $adjective = filter_input(INPUT_GET, 'fAdjective', FILTER_SANITIZE_STRING);
    $sillyword = filter_input(INPUT_GET, 'fSillyWord', FILTER_SANITIZE_STRING);
    $magic = filter_input(INPUT_GET, 'fMagic', FILTER_SANITIZE_STRING);
        echo "There was once a $adjective magician who roamed the wild terrains of $adverb <br>The magician $noun cast the mighty spell $magic around the $sillyword <br>$pronoun knew there was only one way to win the war - $verb";
}
else
{
    echo "parameters not provided."
}
?>

<?php
    require "end.php"
?>

我遇到的问题是它收集信息(您可以在URL中看到它)但是,它不会将信息回显到屏幕上。

这可能是与PHP和JavaScript的奇怪互动吗?

1 个答案:

答案 0 :(得分:0)

这是一个选项,您需要首先检查filter_input返回的内容,然后您可以将其分配给变量并显示它,下面是一个简单的示例,源自w3schools在此处提供的内容:https://www.w3schools.com/php/showphp.asp?filename=demo_func_filter_input。以下示例验证来自表单的电子邮件,如果过滤器输入未返回false,则会打印一条消息,说明“此电子邮件:是有效的电子邮件&#39;

示例:

<!DOCTYPE html>
<html>
<body>

<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
E-mail: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>

<?php
if (isset($_GET["email"])) {
    if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) === false) {
        $email = filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL);
        echo("This email: ".$email." is a valid Email");
    } else {
        echo("Email is not valid");
    }
}
?>

</body>
</html>

希望这会有所帮助。