Barnabas Nagy
Barnabas Nagy

Reputation: 15

How to add action tag to form?

I have a working php guestbook script. It's only 1 file. 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. 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');
?>

Upvotes: 0

Views: 1521

Answers (3)

eykanal
eykanal

Reputation: 27017

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.

By the way, you should ensure that your inputs are all cleaned; just using trim() before inserting them is asking for trouble. See here for more on this topic.

Upvotes: 0

Topher Fangio
Topher Fangio

Reputation: 20667

The action tag tells the form where to submit the data. If it is left blank, it will attempt to submit the data to the current php page. If it's giving you trouble, perhaps you need to specify it and point it to the php page that generates the form.

Upvotes: 0

aorcsik
aorcsik

Reputation: 15552

The action property specifies the link the form is sent to. If the form calls itself you can leave it blank:

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

Upvotes: 1

Related Questions