Forum Moderators: coopster
I am trying to get a single field validation sorted, i need the blog_id to be filled in and not continue if it is empty.
<div class="content">
<br><br><br><form action="add-blog.php" method="POST">
Blog Ref: GH<input type="text" name="blog_id"> <?php if(empty($_POST['blog_id'])){ echo "<font color='red' size='2px'>The blog ID is missing or not valid<br><br></font>"; } ?>
Date: <input type="text" name="date"><br><br>
Title: <input type="text" name="title"><br><br>
Short Description:<br>
<textarea name="short" cols="40" rows="3">
Enter Short Article here...
</textarea><br><br>
Main Description:<br>
<textarea name="main" cols="40" rows="5">
Enter full Article here...
</textarea><br><br>
<input type="submit" value="Submit">
</form>
<br><br><br>
</div>
At the moment it will display the error but if i click send it will send the data regardless,
Any help greatly appreciated.
Many Thanks
ie, the validation check is on the data submitted from the form, so naturally the form has to send the data.
This is probably not the problem, I suspect the problem is in handling the data.
I would suggest that in your script you do a check on 'blog_id' like you have for the error, and only run the correct response if 'blog_id' exists.
eg -
<?PHP
$blogID = $_POST['blog_id'];
if(isset($blogID)){
//run your code because blogID is set
}else{
//show the form because blog ID is not set
<form action="add-blog.php" method="POST">
Blog Ref: GH<input type="text" name="blog_id"> <?php if(empty($_POST['blog_id'])){ echo "<font color='red' size='2px'>The blog ID is missing or not valid<br><br></font>"; } ?>
Date: <input type="text" name="date"><br><br>
Title: <input type="text" name="title"><br><br>
Short Description:<br>
<textarea name="short" cols="40" rows="3">
Enter Short Article here...
</textarea><br><br>
Main Description:<br>
<textarea name="main" cols="40" rows="5">
Enter full Article here...
</textarea><br><br>
<input type="submit" value="Submit">
</form>
<br><br><br>
</div>
<?
//end else
}
<?
The other option could be to run client side validation with Javascript, but this could lead to issues with how secure the validation is, so a combination of the two would be best in my opinion.
You could give your submit button a name and check that it has been suubmitted before doing the error check.
eg -
submit button -
<input type="submit" value="Submit" name="submit">
error check -
<?php
if($_POST['submit']=="Submit"){
if(empty($_POST['blog_id'])){ echo "<font color='red' size='2px'>The blog ID is missing or not valid<br><br></font>"; }
}
?>
So lets say that the form is in a script called 'form.php' you could check for the 'blog_id' in 'add-blog.php' and if it's not set redirect the user back to 'form.php' and send an error variable.
E.G. -
form.php
---------
<?PHP
//get the error variable
$error = $_GET['err'];
<form action="add-blog.php" method="POST">
Blog Ref: GH<input type="text" name="blog_id"> <?php if($error=="noblog"){ echo "<font color='red' size='2px'>The blog ID is missing or not valid<br><br></font>"; } ?>
Date: <input type="text" name="date"><br><br>
Title: <input type="text" name="title"><br><br>
Short Description:<br>
<textarea name="short" cols="40" rows="3">
Enter Short Article here...
</textarea><br><br>
Main Description:<br>
<textarea name="main" cols="40" rows="5">
Enter full Article here...
</textarea><br><br>
<input type="submit" value="Submit">
</form>
<br><br><br>
</div>
add-blog.php
---------
<?PHP
$blogID = $_POST['blog_id'];
if(isset($blogID)){
//run your code because blogID is set
}else{
//redirect back to form and set the error
header("Location: form.php?err=noblog");
}
?>