Forum Moderators: coopster

Message Too Old, No Replies

single field validation

trying to get the id validated if empty not continue

         

togethercomms

10:51 am on Jul 20, 2009 (gmt 0)

10+ Year Member



Hi everyone,

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

mattclayb

11:13 am on Jul 20, 2009 (gmt 0)

10+ Year Member



As you currently have your code written, the data will always be 'sent', as you are checking the 'POSTED' variable of 'blog_id' for validation.

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.

mattclayb

11:19 am on Jul 20, 2009 (gmt 0)

10+ Year Member



...can I also add that in your current code the blog ID field will be red on entry to the page, BEFORE the form is submitted because blogID is not yet set. So you may want to change this, and check that the form has been submitted.

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>"; }
}
?>

togethercomms

11:43 am on Jul 20, 2009 (gmt 0)

10+ Year Member



where would i put this?

Would i put it into the "add-blog.php"?
or in the form itelf?

Many Thanks

mattclayb

11:55 am on Jul 20, 2009 (gmt 0)

10+ Year Member



Yes the handling of the data would be in the 'add-blog.php' script.

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");

}
?>

togethercomms

12:29 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



cheers, i managed to do it with some funky and easy to use javascript.

Very easy and works like a dream.

Many Thanks all

mattclayb

12:41 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



cool...but if the check is critical ensure you keep some server side validation as the javascript can be easily bypassed ;-)