简体   繁体   English

如何在表单中添加动作标签?

[英]How to add action tag to form?

I have a working php guestbook script. 我有一个工作的php留言簿脚本。 It's only 1 file. 只有1个文件。 I tried to validate it and there is only one error: 我试图验证它,只有一个错误:

Line 147, Column 36: required attribute "action" not specified
<form method="post" name="blogform">

Now the code is this and I'm sure I would need to break up the file to two so that I can create a file for the action tag but I just don't know how. 现在的代码是这样,我确定需要将文件分解为两个,以便可以为action标签创建文件,但是我不知道如何。 Any help is much appreciated. 任何帮助深表感谢。

    <?php
session_start();
include("../../4a/inc/opendb.inc.php");

if(isset($_POST['send'])) //checks if $_POST variable "is set"

if(isset($_SESSION["ellenorzo"]) && !empty($_SESSION["ellenorzo"]) && $_SESSION["ellenorzo"]==$_POST["code"]){    

    $name    = trim($_POST['name']); //eliminating whitespaces
    $email   = trim($_POST['email']);
    $message = addslashes( trim($_POST['message']));

    $query = "INSERT INTO blog (name, email, message, date) " .
             "VALUES ('$name', '$email', '$message', NOW())";
    mysql_query($query) or die('Hey, something is wrong!' . mysql_error());

    header('Location: ' . $_SERVER['REQUEST_URI']); 
    exit; 
} 
?>

<?php
include('../../4a/inc/head.inc.php');
?>

<body style="color: #ffffff;">
    <div class="mainblog">
        <div class="top">
            <div class="menu">
                <?php
                include('../menu.inc.php');
                ?>
            </div>
        </div>

<div class="middleblog">

<form method="post" name="blogform">
<input name="name" id="name" class="nameblog" type="text" />
<img src="../../4a/img/main/name.jpg" class="name" alt="Name" />
<input name="email" id="email" class="emailblog" type="text" />
<img src="../../4a/img/main/email.jpg" class="email" alt="Email" />
<textarea name="message" id="message" class="messageblog" rows="6" cols="6" onkeyup="return ismaxlength(this)">
</textarea>
<img src="../../4a/img/main/message.jpg" class="message" alt="Message" />
<input name="send" value="submit" id="send" class="sendblog" type="image" src="../../4a/img/main/send.jpg" onclick="return checkform();" />
<input type="hidden" name="send" value="submit" />
<div class="text_check_code">
<font class="text">
Enter the characters as they are shown below.
</font>
</div> 
<img src="../../4a/inc/secure.inc.php" class="img_check_code" alt="Nospam" />
<input name="code" class="input_check_code" />
</form>

<?php
                $rowsperpage = 10;
                $pagenumber = 1; 

                if(isset($_GET['page'])) 
                { 
                    $pagenumber = $_GET['page']; 
                } 
                $offset = ($pagenumber - 1) * $rowsperpage; 

                $query = "SELECT id, name, email, message, date ".
                         "FROM blog ".
                         "ORDER BY id DESC ".
                         "LIMIT $offset, $rowsperpage";  
                $result = mysql_query($query) or die('Hey, something is wrong!. ' . mysql_error()); 

                if(mysql_num_rows($result) == 0) 
                { 
                print("<br /><br /><br /><br /><br /><br /><br /><br />The blog is empty.");
                } 
                else 
                { 
                    while($row = mysql_fetch_array($result)) 
                    { 
                        list($id, $name, $email, $message, $date) = $row;


                                        $name    = htmlspecialchars($name);
                                        $email   = htmlspecialchars($email);
                        $message = htmlspecialchars($message);
                        $message = stripslashes(nl2br($message)); //real breaks as user hits enter

                ?> 

            <br />
            <div class="blogentries">
                    <b><a href="mailto:<?=$email?>" class="index"><?=$name?></a></b>
                    <br />
                    <?=$message?>
                    <br />
                    <i><?=$date?></i>
            </div>
            <br />

            <?php
                } //closing while statement 

            $query   = "SELECT COUNT(id) AS numrows FROM blog";
            $result  = mysql_query($query) or die('Hey, something is wrong!. ' . mysql_error()); 
            $row     = mysql_fetch_array($result, MYSQL_ASSOC); 
            $numrows = $row['numrows']; 

            $maxpage  = ceil($numrows/$rowsperpage); //rounding up any integer eg. 4,1=5 
            $nextlink = ''; 

            if($maxpage > 1) 
            { 
                $self     = $_SERVER['PHP_SELF']; 
                $nextlink = array();
                for($page = 1; $page <= $maxpage; $page++) 
                { 
                    $nextlink[] =  "<a href=\"$self?page=$page\">$page</a>";
                }  
                $nextlink = "Next: " . implode(' » ', $nextlink); //returns all elements of an array as a string
            } 
            include ("../../4a/inc/closedb.inc.php");
            ?>
            <br />
            <div class="nextlink">
            <?=$nextlink;?>
            </div>
        </div>
        <br />
        <br />
        <div class="bottomblog">
            <?php
            require_once('../../4a/inc/copyright.inc.php');
            ?>
        </div>
        <br />
        <br />
    </div>
    <?php //closing the else statement
    }
    ?>

<?php
include('../../4a/inc/footer.inc.php');
?>

The action property specifies the link the form is sent to. action属性指定表单发送到的链接。 If the form calls itself you can leave it blank: 如果表单自行调用,则可以将其留空:

<form action="" method="post" name="blogform">

The action tag tells the form where to submit the data. action标签告诉表单将​​数据提交到哪里。 If it is left blank, it will attempt to submit the data to the current php page. 如果留空,它将尝试将数据提交到当前的php页面。 If it's giving you trouble, perhaps you need to specify it and point it to the php page that generates the form. 如果给您带来麻烦,也许您需要指定它并将其指向生成表单的php页面。

In the code you provided, the bit of code which handles new inserts is at the top of this page, so you should set the action tag to be the name of the page. 在您提供的代码中,处理新插入的代码位在此页面的顶部,因此您应该将action标签设置为页面的名称。

By the way, you should ensure that your inputs are all cleaned; 顺便说一句,您应该确保所有输入都被清理干净了。 just using trim() before inserting them is asking for trouble. 只是在插入它们之前使用trim()会带来麻烦。 See here for more on this topic. 有关此主题的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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