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.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php include('tpl_includes/defines.php'); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Blog | <?php echo BUSINESS_NAME." - ".BUSINESS_SLOGAN ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Kristopher Kerr" />
<meta name="copyright" content="test 2010 - <?php echo date('Y'); ?>" />
<meta name="email" content="hello@test.com" />
<meta name="robots" content="all" />
<meta name="distribution" content="global" />
<meta name="rating" content="general" />
<link href="http://www.test.com/blog/css/stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<?php include('tpl_includes/header.php');?>
<div class="postMaster" style="margin-top: 5px;">
<?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!";
}
}
?>
<div class="postTitle">Post entry to Web Log</div>
<form id="formadd" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="addPostForm">
<label class="labelForm">Article Title <span class="invalid"><?php echo $msg ?></span></label>
<input class="formText bold" type="text" name="articleTitle" value="<?php echo $title; ?>" />
<label class="labelForm" style="margin-top: 15px;">Article Body <span class="invalid"><?php echo $msgBod; ?></span></label>
<textarea class="formText" name="articleContent" rows="7"><?php echo $content; ?></textarea>
<label class="labelForm" style="margin-top: 15px;">Article Tags (separate with comma) <span class="invalid"><?php echo $msgTags ?></span></label>
<input class="formText" type="text" name="articleTags" value="<?php echo $tags; ?>" />
<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" >
</form>
</div>
<!-- BOF Filters -->
<div id="filters">
<div id="filterTags">
<div class="fontGen voicesBox"><a href="http://www.test.com/blog/default/shobrook/">shobrook</a>, <a href="default.php?tag=amet">amet</a></div>
</div>
<div id="filterVoices">
<div class="fontGen voicesBox"><a href="http://www.test.com/blog/default/kriss">Show only posts by Kriss</a><br/> <a href="http://www.test.com/blog/default/gina">Show only posts by Gina</a></div>
</div>
<div id="recentPosts">
<div class="fontGen recentPostsContain">
<?php
$related = mysql_query("SELECT * FROM posts ORDER BY `posts`.`date` DESC LIMIT 5");
while ($res = mysql_fetch_array($related)) {
echo '<div class="relHold">';
echo '<b>'.$res['article_title'].'</b><br/>';
echo '<span style="font-size: 9px;">Posted '.$res['date'].'</span>';
echo '</div>';
}
?>
</div>
</div>
</div>
<!-- EOF Filters -->
<?php
include('tpl_includes/right_ads.php');
include('tpl_includes/footer.php');
?>
</div>
</body>
</html>
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
system
January 3, 2011, 11:17pm
5
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.