PHP Validate Form

Hi all, having a few issues validating my form, just for empty fields that’s all. Problem is that I am using PHP_SELF to process the form and the script is executing even though the form hasn’t been submitted so I have now added an isset but now nothing is working! Any help would be greatly appreciated as always!

<?php
$title = $_POST['articleTitle'];
$content = $_POST['articleContent'];
$tags = $_POST['articleTags'];
$msg = "";

if(isset($_POST['submit'])) { 
	if (!$title) {
	$msg = "Article Title contains empty data!";
	}
	if ($content) {
	$msgBod = "Body contains empty data!";
	}
	if ($tags) {
	$msgTags = "Tags contains empty data!";
	}
}	

?>

Do you have an attribute called name=“submit” on your submit button?

Hi SgtLegend, yes I do, but can’t work out what I am doing wrong, I’m sure it’s probably something very simple. Please see my entire page source below.

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;?php include('tpl_includes/defines.php'); ?&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
	&lt;head&gt;
	&lt;title&gt;The Blog | &lt;?php echo BUSINESS_NAME." - ".BUSINESS_SLOGAN ?&gt;&lt;/title&gt;
	&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
	&lt;meta name="author" content="Kristopher Kerr" /&gt;
	&lt;meta name="copyright" content="test 2010 - &lt;?php echo date('Y'); ?&gt;" /&gt;
	&lt;meta name="email" content="hello@test.com" /&gt;
	&lt;meta name="robots" content="all" /&gt;
	&lt;meta name="distribution" content="global" /&gt;
	&lt;meta name="rating" content="general" /&gt;
	&lt;link href="http://www.test.com/blog/css/stylesheet.css" rel="stylesheet" type="text/css" /&gt;
	&lt;/head&gt;
&lt;body&gt;
&lt;div id="wrapper"&gt;
&lt;?php include('tpl_includes/header.php');?&gt;
&lt;div class="postMaster" style="margin-top: 5px;"&gt;
&lt;?php
$title = $_POST['articleTitle'];
$content = $_POST['articleContent'];
$tags = $_POST['articleTags'];
$msg = "";

if(isset($_POST['submit'])) { 
	if (!$title) {
	$msg = "Article Title contains empty data!";
	}
	if ($content) {
	$msgBod = "Body contains empty data!";
	}
	if ($tags) {
	$msgTags = "Tags contains empty data!";
	}
}	

?&gt;
&lt;div class="postTitle"&gt;Post entry to Web Log&lt;/div&gt;
&lt;form id="formadd" action="&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;" method="post" name="addPostForm"&gt;
&lt;label class="labelForm"&gt;Article Title &lt;span class="invalid"&gt;&lt;?php echo $msg ?&gt;&lt;/span&gt;&lt;/label&gt;
&lt;input class="formText bold" type="text" name="articleTitle" value="&lt;?php echo $title; ?&gt;" /&gt;
&lt;label class="labelForm" style="margin-top: 15px;"&gt;Article Body &lt;span class="invalid"&gt;&lt;?php echo $msgBod; ?&gt;&lt;/span&gt;&lt;/label&gt;
&lt;textarea class="formText" name="articleContent" rows="7"&gt;&lt;?php echo $content; ?&gt;&lt;/textarea&gt;
&lt;label class="labelForm" style="margin-top: 15px;"&gt;Article Tags (separate with comma) &lt;span class="invalid"&gt;&lt;?php echo $msgTags ?&gt;&lt;/span&gt;&lt;/label&gt;
&lt;input class="formText" type="text" name="articleTags" value="&lt;?php echo $tags; ?&gt;" /&gt;
&lt;input type="image" src="http://www.test.com/blog/images/submit-article.gif" name="submit" width="81" height="20" class="img" style="margin-top: 10px;" alt="Submit Article" &gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;!-- BOF Filters --&gt;
&lt;div id="filters"&gt;
&lt;div id="filterTags"&gt;
&lt;div class="fontGen voicesBox"&gt;&lt;a href="http://www.test.com/blog/default/shobrook/"&gt;shobrook&lt;/a&gt;, &lt;a href="default.php?tag=amet"&gt;amet&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="filterVoices"&gt;
&lt;div class="fontGen voicesBox"&gt;&lt;a href="http://www.test.com/blog/default/kriss"&gt;Show only posts by Kriss&lt;/a&gt;&lt;br/&gt; &lt;a href="http://www.test.com/blog/default/gina"&gt;Show only posts by Gina&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="recentPosts"&gt;
&lt;div class="fontGen recentPostsContain"&gt;
&lt;?php
$related = mysql_query("SELECT * FROM posts ORDER BY `posts`.`date` DESC LIMIT 5");
	while ($res = mysql_fetch_array($related)) {
	echo '&lt;div class="relHold"&gt;';
	echo '&lt;b&gt;'.$res['article_title'].'&lt;/b&gt;&lt;br/&gt;';
	echo '&lt;span style="font-size: 9px;"&gt;Posted '.$res['date'].'&lt;/span&gt;';
	echo '&lt;/div&gt;';
	}
?&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- EOF Filters --&gt;
&lt;?php
include('tpl_includes/right_ads.php');
include('tpl_includes/footer.php');
?&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

Hi, ok figured out the issue. Seems as though the submit tag I am using is a little dodgy, all seems to be working perfectly now. Thanks for your time albeit. Cheers dude :smiley:

if (isset($_POST['submit']))

is evaluating to false on every pass.

I’m not sure if it’s because your input type for the submit is an image. If I change it to a button type, the submit’s name attribute is sent along with the other form data.