【问题标题】:How to call php validation file to html file when clicked on submit button单击提交按钮时如何将php验证文件调用到html文件
【发布时间】:2014-04-05 22:48:39
【问题描述】:

我正在尝试将 PHP 验证文件调用到 HTML 文件中。任何人都可以建议我单击提交按钮时如何调用该 php 文件。

示例 html 代码:

<head>
    <style>
        .error {color: #FF0000;}
    </style>
</head>
<body style="background-color:#F2F2F2">
    <form name="htmlform" method="post" onsubmit="\test_form.php">
        <table width="550px" table align="center" frame="box" height="80%" style="margin-top:25px;margin-bottom:25px; background-color:#ffffff;border-radius:5px;"></tr>
            <tr>
                <th colspan="2">
                    <h3  align="center">Write to Us</h3>
                    <hr>
                </th>
            </tr>
            <tr>
                <td valign="top">
                    <label for="name">Name </label>
                </td>
                <td valign="top">
                    <input  type="text" name="name" maxlength="50" size="30">
                    <span class="error">* <?php echo $nameErr;?></span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="email">Email Address </label>
                </td>
                <td valign="top">
                    <input  type="text" name="email" maxlength="80" size="30">
                    <span class="error">* <?php echo $emailErr;?></span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="telephone">Telephone Number</label>
                </td>
                <td valign="top">
                    <input  type="text" name="telephone1" maxlength="30" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="message">Message </label>
                </td>
                <td valign="top">
                    <textarea  name="message1" maxlength="1000" cols="25" rows="6"></textarea>
                    <span class="error">* <?php echo $messageErr;?></span>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center;">
                    <input type="submit" value="Submit" onclick="$.fancybox.close()">
                </td>
            </tr>
        </table>
    </form>
</body>

PHP代码如下:

<?php
    if (isset($_POST['submit']))
    {
        if (empty($_POST["name"]))
            {$nameErr = "Name is required";}
        else
            {$name =test_input($_POST["name"]);}

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

        if (empty($_POST["telephone1"]))
            {$telephone1 = "";}
        else
            {$telephone1 =test_input($_POST["telephone1"]);}

        if (empty($_POST["message1"]))
            {$messageErr = "Message is required";}
        else
            {$message1 = test_input($_POST["message1"]);}

        if(isset($name) && isset($email) && isset($message1))
        {
            echo "<h2>Your Input:</h2>";
            echo $name;
            echo "<br>";
            echo $email;
            echo "<br>";
            echo $telephone1;
            echo "<br>";
            echo $message1;
            $recipient = "pallavi.thota@pinovus.com"; ///  Your Email address
            if (isset($_POST['email']))
            {
            //Send Mail To Webmaster
                $email = $_POST['email'] ;
                $subject = 'Feedback Form';
                $message =  $name . ' has been subscribed to your website.';
                mail("$recipient", $subject, $message, "From:" . $recipient);
            }
        }
    }
    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>

当我将 php 代码保存到 html 文件中并将其保存为 php 并在 word press 中执行时,它不接受。

【问题讨论】:

  • test_form.php在哪里,和html在同一个目录吗?
  • 是的,它在同一个目录中,但不执行任何验证

标签: php html wordpress xampp


【解决方案1】:
<form method="post" onsubmit="\test_form.php">

改成:

<form method="post" action="test_form.php">

【讨论】:

  • 它被移动到 test_form.php 文件中,并且没有显示任何内容,只是空白页面,没有验证。请提出一些解决方案,例如检查指定字段的验证
  • @Pallavipradeep 所有文件都存放在同一个目录下?
  • 文件保存在同一目录中,但它只是显示该页面而无需验证
【解决方案2】:

第一页和第二页的路径是什么?

改用这个:http://jqueryvalidation.org/documentation/

$().ready(function() {
// validate form on keyup and submit
$("#htmlform").validate({
    rules: {
        name: {
            required: true,

        },
        email: {
            required: true,
            email:true
        }....

}
});

对于 WP 看看这个,it's might be usefull,你不需要写很多代码,只需在 WP 中配置

【讨论】:

  • 这样做有什么目的吗?如果您只想使用 php,请尝试检查您的目录,记住 ./ 转到一个目录前,然后 ../ 到根目录
  • 我想把这个 html 页面保存在 wordpress 中,它只接受 html 文件,PHP 被当作文本而不接受。它应该只能从后端工作,它可以被调用。
  • 您可以使用
  • Azrael 我可以有一段代码用 HTML 和 jQuery 代码调用该特定代码。提前致谢
  • 使用 jQuery 验证我已经通过使用此链接 runnable.com/Uaki4q9qiqlLAACJ/… 完成了表单验证,并删除了其中的 php 代码并要求导航到另一个页面,它工作正常。但想将此代码保留在 WP 中并检查。感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2018-09-08
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
相关资源
最近更新 更多