Page 1 of 1

validation of variables for submit form

Posted: Wed Mar 31, 2004 4:35 am
by preetgowda
Hi, i have a problem in trying to validate the data so that if a user does not submit a certain field then an error message will come up. I have started to do this( if empty....) but I am stuck on the coding.......

Code: Select all

<html>



<head><title>Submit form For Album Titles</title></head>

<body text="#000000" bgcolor="#feebcc"> 

<style type="text/css">
  body { margin-left: 10%; margin-right: 10%; }
</style>

<center><img src="unicrest.gif" width="154" height="79"
alt="University crest of Liverpool" longdesc="unicrest.html"></center>


<HR SIZE=3 style="background-color: blue">

<body>


<font face = "verdana">


<font size=4>
  
       <center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/UserLogin.php">
	   Home Page
	   </a>
	</center>   


<center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/ContactPage.html">
	   Contact Page
	   </a>
	</center>   


<center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/InfoPage.html">
	   Information Page
	   </a>
	</center>   
	
	</font size=4>



<?

$albumId=$HTTP_POST_VARS['albumId'];
       $albumTitle=$HTTP_POST_VARS['albumTitle'];
       $classicalTrack=$HTTP_POST_VARS['classicalTrack'];
       $cdNumber=$HTTP_POST_VARS['cdNumber'];
       $artistBandName=$HTTP_POST_VARS['artistBandName'];
       $genre=$HTTP_POST_VARS['genre'];
       $label=$HTTP_POST_VARS['label'];
       $year=$HTTP_POST_VARS['year'];
       $rating=$HTTP_POST_VARS['rating'];

       
  
      
         if (empty ($albumId) or empty ($albumTitle) or empty  ($artistBandName) or empty ($genre) or empty($year) or empty ($rating)) 
	   
	  
	   
     	
	$query = "INSERT INTO Album(albumId,AlbumTitle,classicalTrack,cdNumber,artistBandName,genre,label,year,rating)
                  VALUES('$albumId','$albumTitle','$classicalTrack','$cdNumber','$artistBandName','$genre','$label','$year', '$rating')";
		  
		
	  	  
	$result = mysql_query($query) or die ("Execution failed."); 
	
?>	
         
	  

	<form method="post"action="<?php echo $PHP_SELF?>">
	<table cellpadding=2 cellspacing=0 border=0>

	<td>Album Id    :</td><td>       		<input type="text" size="5" maxlength="10"                  name="albumId"  value="<?echo $albumId; ?>"></td><tr>

      <td>Album Title:</td><td>				<input type="text" size="30" maxlength="30"                      name="albumTitle" value="<?echo $albumTitle; ?>"></td></tr>

      <td>Classical Track:</td><td>			<input type="text" size="30" maxle2th="30"                      name="classicalTrack"value="<?echo $classicalTrack;?> "></td></tr>

	<td>Cd Number:(same as Album ID)</td><td>			        <input type="text" size="2" maxlength="2"                      name="cdNumber" value="<?echo $cdNumber;?>"></td></tr>

	<td>Artist / Band Name:</td><td>   			<input type="char" size="30" maxlength="30"                       name="artistBandName" value="<?echo $artistBandName;?>"> </td><tr>

	<td> Genre:(e.g pop, rock, R &B...) </td><td>       			<input type="char" size="20" maxlength="20"                       name="genre" value="<?echo $genre;?>"></td><tr>


        <td>Label:</td><td>                          <input type="char" size="20" maxlength="20"                         name="label" value="<?echo $label;?>"></td><tr>

	<td>Year  (e.g.yyyy-mm-dd):</td><td>  	        <input type="char" size="10" maxlength="10"                         name="year"value="<?echo $year;?>"></td><tr>

	<td>Rating:(e.g use- bad, ok, good, great)</td><td>  			        <input type="char" size="10" maxlength="10"                name="rating" value="<?echo $rating;?>"></td><tr>


	
	</table>
	<input type="Submit" name="class" value="Enter Album Title Info.">
	<input type="Reset">
	
	</form>

  
<br> </br>

	<p>You Can view the updated General Music Collection with information that you have entered <a href = "http://www.csc.liv.ac.uk/~u1phg/selectAlbum.php">  - Here!</a></p>
	
	
	
	
	
	<br> </br>
	<center><p>To submit or query the other collections  choose from below:</p></center> 
	
	<center><p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectClassical.php">Classical Music</a></p>  
	<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectDVD.php">DVDs</a></p>  
	<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectBook.php">Books</a></p></center>  
	
	
	
<?

Posted: Wed Mar 31, 2004 5:19 am
by mudkicker
it should be that way (as an example) :

if(empty($foo) || empty($boo)) {
print "error!";
exit;
}
else
{
// mysql insert.
}

Posted: Wed Mar 31, 2004 5:26 am
by preetgowda
Thanks, for that I will try this and see if it works!

Posted: Wed Mar 31, 2004 9:51 am
by preetgowda
I tried doing this but it does not work.Could you please show me using my form of how to do this please?

Posted: Wed Mar 31, 2004 9:52 am
by lostboy
i tend to like

Code: Select all

if (isset($$HTTP_POST_VARS['albumId']){
   $albumId=$HTTP_POST_VARS['albumId'];
}else{
   $err_msg.="Album id not input";
}
then checking to see if the $err_msg is set to something other than and empty string...if it is set, then halt the process and show the form with the $err_msg notes, otherwise proceed with the db query....

Note that you can also set the DB fields to NOT NULL to prevent empty records from getting in...
Also: your EMPTY check does not cover spaces (" ") since that field is no longer empty....Regex is something that you may want to look at since one regex function can accomadate most of your inputs...

hth

Posted: Wed Mar 31, 2004 9:52 am
by magicrobotmonkey
use javascript and don't let them submit the form until its all filled in

Posted: Wed Mar 31, 2004 9:57 am
by JayBird
have you taken that code from the guy in this post?

viewtopic.php?t=20215

it looks awfully similar and VERY wrong!

What the hell is going on here

Code: Select all

if (empty ($albumId) or empty ($albumTitle) or empty  ($artistBandName) or empty ($genre) or empty($year) or empty ($rating))
Basically, you are saying, if nothing was enetered in the form, put it in the database, but nothing was entered in the form.

The code isn't even structured correclty

mark

Posted: Wed Mar 31, 2004 10:04 am
by JayBird
okay, give this a try.

Code: Select all

<html>
<head>
<title>Submit form For Album Titles</title>
</head>
<body text="#000000" bgcolor="#feebcc"> 
<style type="text/css"> 
  body { margin-left: 10%; margin-right: 10%; } 
</style> 
<center> 
  <img src="unicrest.gif" width="154" height="79" alt="University crest of Liverpool" longdesc="unicrest.html"> 
</center> 
<body> 
<font face = "verdana"> <font size=4> 
<center> 
  <a href = "http://www.csc.liv.ac.uk/~u1phg/UserLogin.php"> Home Page </a> 
</center> 
<center> 
  <a href = "http://www.csc.liv.ac.uk/~u1phg/ContactPage.html"> Contact Page </a> 
</center> 
<center> 
  <a href = "http://www.csc.liv.ac.uk/~u1phg/InfoPage.html"> Information Page </a> 
</center> 
</font size=4> 
<? 
	if (isset($_POST)) { // Only do this bit if the form has been submitted

		$albumId=$_POST['albumId']; 
		$albumTitle=$_POST['albumTitle']; 
		$classicalTrack=$_POST['classicalTrack']; 
		$cdNumber=$_POST['cdNumber']; 
		$artistBandName=$_POST['artistBandName']; 
		$genre=$_POST['genre']; 
		$label=$_POST['label']; 
		$year=$_POST['year']; 
		$rating=$_POST['rating'];     
  
	// If any of the following variables are empty, DO NOT insert in Database
	if (!empty($albumId) || !empty($albumTitle) || !empty($artistBandName) || !empty($genre) || !empty($year) || !empty($rating)) { 
		$query = "INSERT INTO Album(albumId,AlbumTitle,classicalTrack,cdNumber,artistBandName,genre,label,year,rating) 
                  VALUES('$albumId','$albumTitle','$classicalTrack','$cdNumber','$artistBandName','$genre','$label','$year', '$rating')"; 
        
		$result = mysql_query($query) or die ("Execution failed."); 
	} else {
		echo "Make sure you filled out all the required fields";
	}
}
    
?> 
<form method="post"action="<?php echo $PHP_SELF?>"> 
  <table cellpadding=2 cellspacing=0 border=0>  
      <td>Album Id :</td> 
      <td> <input type="text" size="5" maxlength="10"                  name="albumId"  value="<? echo $albumId; ?>"></td> 
    <tr> 
      <td>Album Title:</td> 
      <td> <input type="text" size="30" maxlength="30"                      name="albumTitle" value="<? echo $albumTitle; ?>"></td> 
    </tr> 
    <td>Classical Track:</td> 
      <td> <input type="text" size="30" maxle2th="30"                      name="classicalTrack"value="<? echo $classicalTrack; ?> "></td> 
    </tr> <td>Cd Number:(same as Album ID)</td> 
      <td> <input type="text" size="2" maxlength="2"                      name="cdNumber" value="<? echo $cdNumber;?>"></td> 
    </tr> <td>Artist / Band Name:</td> 
      <td> <input type="char" size="30" maxlength="30"                       name="artistBandName" value="<? echo $artistBandName; ?>"> </td> 
    <tr> 
      <td> Genre:(e.g pop, rock, R &B...) </td> 
      <td> <input type="char" size="20" maxlength="20"                       name="genre" value="<? echo $genre; ?>"></td> 
    <tr> 
      <td>Label:</td> 
      <td> <input type="char" size="20" maxlength="20"                         name="label" value="<?e cho $label;?>"></td> 
    <tr> 
      <td>Year (e.g.yyyy-mm-dd):</td> 
      <td> <input type="char" size="10" maxlength="10"                         name="year"value="<? echo $year; ?>"></td> 
    <tr> 
      <td>Rating:(e.g use- bad, ok, good, great)</td> 
      <td> <input type="char" size="10" maxlength="10"                name="rating" value="<? echo $rating; ?>"></td> 
    <tr>  
  </table> 
  <hr size=3 style="background-color: blue"> 
  <input type="Submit" name="class" value="Enter Album Title Info."> 
  <input type="Reset"> 
</form> 
<br> 
</br> 
<p>You Can view the updated General Music Collection with information that you
  have entered <a href = "http://www.csc.liv.ac.uk/~u1phg/selectAlbum.php"> -
  Here!</a></p> 
<br> 
</br> 
<center> 
  <p>To submit or query the other collections choose from below:</p> 
</center> 
<center> 
  <p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectClassical.php">Classical
      Music</a></p> 
  <p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectDVD.php">DVDs</a></p> 
  <p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectBook.php">Books</a></p> 
</center>
Mark

Posted: Wed Mar 31, 2004 10:09 am
by preetgowda
so could someone please tell me what is thre right way of structuring the code to check for empty fields- please, thanks

Posted: Wed Mar 31, 2004 10:12 am
by JayBird
I just did in the previous post

Mark

Posted: Wed Mar 31, 2004 11:07 am
by markl999
I prefer to use a method such as :

Code: Select all

<?php

if(!empty($_POST)){
  $required = array(
    'albumId', 'albumTitle', 'classicalTrack',
    'cdNumber', 'artistBandName', 'genre',
    'label', 'year', 'rating'
  );
  foreach($required as $req){
    if(empty($_POST[$req])){
      $missing[] = $req;
    } else {
      $values[] = $_POST[$req];
    }
  }
  if(empty($missing)){
    mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
    mysql_select_db('thedb') or die(mysql_error());
    $sql = "INSERT INTO Album (".join(',', $required).") VALUES (".
      join(',', $values).")";
    mysql_query($sql) or die(mysql_error());
  } else {
    echo 'The following fields need to be filled in : '.join(' ', $missing);
  }
}

?>
<!-- put the form here -->
The code above has a few errors.
if (isset($_POST)) { <-- $_POST is always set regardless of if the form was submitted or not.
$albumId=$_POST['albumId']; <-- You don't really need to created separate vars to hold the data, you already have them in $_POST. Also if the field wasn't posted (not filled in) then you'll generate an Undefined Index warning.