Jump to content

form validation


AdRock

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.