Trying to create a empty fields function
Posted: Mon Dec 06, 2010 8:11 am
Hi. I am trying to create an empty field checking function which seems quite straight forward but it has put me into a trouble spin. Please help me to debug the codes below. These classes are to manage bookmarks in a website. The bookmark adding form would be in 'manageBookmarks.php' and it would send the a POST over to 'addBookmarks.php' which would check the fields first. If the fields any of the fields are empty, it would add a session attribute and redirect back to 'addBookmarks.php' which would put a '*' besides every empty field and would add in the previous values entered into the fields before the previous submission. The problem is that when a form that has empty values gets redirected back to 'addBookmarks.php', all the previous values before submission could not be added into the text fields and only the text area could sort of work.
manageBookmarks.php
addBookmarks.php
Thanks in advance for helping me to debug.
manageBookmarks.php
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<link href="css/default.css" rel="stylesheet" type="text/css"></head><body>
<?php
include("header.php");
session_start();
echo $_SESSION['sname']." sname_val: ".$_SESSION['sname_val']." ".$_SESSION['stype']." stype_val: ".$_SESSION['stype_val']." ".$_SESSION['saddy']." saddy_val: ".$_SESSION['saddy_val']." scomm: ".$_SESSION['scomm_val'];
?>
<p/>
<p><b>Manage Bookmarks</b>
<?php
if(isset($_SESSION['empty'])){
echo "<b><font color=\"red\"> - Incomplete fields. Please complete them.</font></b>";
unset($_SESSION['empty']);
}
?>
</p>
<form name="addBookmarksFrm" method="post" action="addBookmarks.php">
<p/>Add Bookmarks:
<p/>Site Name: <input type="text" name="sitename">
<?php
if(isset($_SESSION['sname_val'])){
$val = $_SESSION['sname_val'];
echo $val;
unset($_SESSION['sname_val']);
}
?>
</input>
<?php
if(isset($_SESSION['sname'])){
if($_SESSION['sname']){
echo "<b><font color=\"red\"> * </font></b>";
}
unset($_SESSION['sname']);
}
?>
<p/>Site Type: <input type="text" name="sitetype">
<?php
if(isset($_SESSION['stype_val'])){
$val = $_SESSION['stype_val'];
echo $val;
unset($_SESSION['stype_val']);
}
?>
</input>
<?php
if(isset($_SESSION['stype'])){
if($_SESSION['stype']){
echo "<b><font color=\"red\"> * </font></b>";
}
unset($_SESSION['stype']);
}
?>
<p/>Site Address: <input type="text" name="siteaddress">
<?php
if(isset($_SESSION['saddy_val'])){
$val = $_SESSION['saddy_val'];
echo $val;
unset($_SESSION['saddy_val']);
}
?>
</input>
<?php
if(isset($_SESSION['saddy'])){
if($_SESSION['saddy']){
echo "<b><font color=\"red\"> * </font></b>";
}
unset($_SESSION['saddy']);
}
?>
<p/>Comments: </br>
<textarea rows="7" cols=37" name="sitecomment">
<?php
if(isset($_SESSION['scomm_val'])){
$val = $_SESSION['scomm_val'];
echo $val;
unset($_SESSION['scomm_val']);
}
?>
</textarea>
<p/>
<?php session_destroy();?>
<input type="reset" value="Clear" /> <input type="submit" value="Submit" />
</form>
<?php include("footer.php"); ?>
</body></html>
Code: Select all
<?php
//include("debug.php");
session_start();
$sname = $_POST["sitename"];
$stype = $_POST["sitetype"];
$saddy = $_POST["siteaddress"];
$scomm = $_POST["sitecomment"];
if(!checkFields($sname,$stype,$saddy,$scomm)){
//echo "Fields checked";
$conn = mysql_connect("localhost:3306","root","faefolks");
//echo "Conn = ".$conn;
if(!$conn){
die("Could not connect: ".mysql_error());
} else {
mysql_select_db("bookmarks",$conn);
$rs = mysql_query("select TypeId from bookmarktypes where TypeName = '$stype'");
$row = mysql_fetch_array($rs);
if($row > 0 ){
//Data found, continue to add...
//echo "<html>Data found </html>";
echo 'row - '.$row['TypeId'];
addBookmarks($conn,$row['TypeId'],$sname,$saddy,$scomm);
} else {
//No data... insert a valid one
$rs = mysql_query("insert into bookmarktypes (TypeName) values ('$stype')");
if (!$rs){
die('Error, add book mark types: '.mysql_error());
} else {
$rs = mysql_query("select TypeId from bookmarktypes where TypeName = '$stype'");
$row = mysql_fetch_array($rs);
if($row > 0 ){
addBookmarks($conn,$row['TypeId'],$sname,$saddy,$scomm);
}
}
}
}
} else {
header("Location: manageBookmarks.php");
}
mysql_close($conn);
//Refresh page once
function addBookmarks($conn,$stype_id,$sname,$saddy,$scomment){
$sql = "insert into bookmarks.bookmarks (`SiteName`, `SiteType`, `SiteAddress`, `Comments`) values ('$sname','$stype_id','$saddy','$scomment')";
$rs = mysql_query($sql,$conn);
if (!$rs){
die('Error, addBookmarks(): '.mysql_error());
} else {
//echo "window.alert('Successfully added site: $sname')";
header("Location: bookmarks.php");
}
}
function checkFields($sname,$stype,$saddy,$scomm){
$isEmpty = false;
if(empty($sname)||$sname==""){
$_SESSION['sname_val']=$sname;
$_SESSION['sname']=true;
$isEmpty = true;
}
if(empty($stype)||$stype==""){
$_SESSION['stype_val']=$stype;
$_SESSION['stype']=true;
$isEmpty = true;
}
if(empty($saddy)||$saddy==""){
$_SESSION['saddy_val']=$saddy;
$_SESSION['saddy']=true;
$isEmpty = true;
}
if($isEmpty === true){
$_SESSION['empty']=true;
$_SESSION['scomm_val']=$scomm;
}
return $isEmpty;
}
?>