Jump to content


Photo

form validation


  • Please log in to reply
1 reply to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 29 July 2006 - 02:33 PM

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?

$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;
    
}
I thought it would be to do with the !isset but I don't know how to change it
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 Guest_docseussman_*

Guest_docseussman_*
  • Guests

Posted 29 July 2006 - 08:24 PM

did you try (!isset($_POST['title']) || !isset($_POST['content']))  ?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users