Jump to content

Archived

This topic is now archived and is closed to further replies.

AdRock

form validation

Recommended Posts

I am trying to validate my form to make usre that both the title field and the content field have something in before posting.  I have managed to get the form to show an error message if the title field is empty but if the content field is empty and the title field not empty the form will post.

How can I check to make usre both fields are not empty?

[code]$title = addslashes($_POST['title']);
$content = addslashes($_POST['content']);

if (!isset($_POST['title'])) {

?>



<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p class="style1"><label for="title">Please enter a title for the news item.</label>
<input style="width:400px;" type="text" size="50" name="title"></p>
<p class="style1"><label for="content">Please enter the content for the news item.</label>
<textarea style="width:400px;" name="content" cols="30" rows="10">
</textarea></p>
<input class="submit-button" style="margin-left:0"  type="Submit" value="Submit"><input class="submit-button" style="margin-left:2px" type="reset" value="Reset"></form>

<?php

}

elseif (empty($title) || empty($content)) {

    echo $empty_fields_message;

}

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.";
        exit;
    }

    // The URLs matched so send the email
    include_once("../includes/connection.php");
mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO news VALUES ('','$title', '$content','')";
mysql_query($query) or die( "Unable to execute query" );

mysql_close();

    // Display the thankyou message
    echo $thankyou_message;
   
}[/code]
I thought it would be to do with the [b]!isset[/b] but I don't know how to change it

Share this post


Link to post
Share on other sites
Guest docseussman
did you try (!isset($_POST['title']) || !isset($_POST['content']))  ?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.