Jump to content

Form Validation


Mr Chris

Recommended Posts

Hi All,

Here’s my script for editing a record in my database:

[code]
<?php

//Connect to DB
include("**********");

if(isset($_GET['story_id']))
{
    $result = mysql_query("Select * From cms_stories where story_id=".$_GET['story_id']);
    $row = mysql_fetch_array($result);
    $section = $row['section'];
    $added_by = $row['added_by'];
    $headline = $row['headline'];
    $byline_name = $row['byline_name'];
    $appeared = $row['appeared'];
    $published = $row['published'];
    $opening = $row['opening'];
    $body_text = $row['body_text'];
    $notes = $row['notes'];
}

if(isset($_POST['Submit']))
{
    $story_id = $_POST['story_id'];
    $section = $_POST['section'];
    $added_by = $_POST['added_by'];
    $headline = $_POST['headline'];
    $byline_name = $_POST['byline_name'];
    $appeared = $_POST['appeared'];
    $published = $_POST['published'];
    $opening = $_POST['opening'];
    $body_text = $_POST['body_text'];
    $notes = $_POST['notes'];

$error="";
       if (empty($section)){
        $error = "** You forgot to enter the section for the story! **<br>";
    }  else if (empty($added_by)){
        $error = "** Error: You forgot to who added the story! **<br>";
    }  else if (empty($headline)){
        $error = "** Error: You forgot to enter a headline for the story! **<br>";
    }  else if (empty($byline_name)){
        $error = "** Error: You forgot to enter a byline name for the story! **<br>";
    }  else if (empty($published)){
        $error = "** Error: You forgot to the date you wish to publish the story! **<br>";
    }  else if (empty($opening)){
        $error = "** Error: You forgot to the Opening Paragraph for the Indexes! **<br>";
    }  else if (empty($body_text)){
        $error = "** Error: You forgot to enter any body text! **<br>";};

if(strlen($error)>0){die($error);};

    $result = mysql_query("Update cms_stories set section='$section', added_by='$added_by', headline='$headline', byline_name='$byline_name', appeared='$appeared', published='$published', opening='$opening', body_text='$body_text', notes='$notes' where story_id=$story_id");
    header('Location: http://www.site.org/site/new_site/stories/confirm_edited_story.php?story_id='.$_POST['story_id']);  
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>test site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../login/ssheet.css">
<script language="JavaScript" src="scripts/calendar1.js"></script>
<script language="JavaScript" src="scripts/calendar2.js"></script>
<link rel="stylesheet" href="../include/css_table.css">
</head>

<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<FORM ACTION="" METHOD="post" NAME="frmAdd"> <TABLE WIDTH="100%" BORDER="0" ALIGN="center">
<TR> <TD HEIGHT="24"><?php echo $msg?> <INPUT TYPE='hidden' NAME='story_id' VALUE='<?php echo $_GET['story_id']; ?>'/>
</TD></TR> </TABLE><BR> <TABLE WIDTH="100%" BORDER="0"> <TR> <TD WIDTH="12%"><B>Section:
</B></TD><TD WIDTH="88%"> <SELECT NAME="section"> <OPTION><?php echo $section?></OPTION>
<OPTION VALUE="art_and_culture">Art & Culture</OPTION> <OPTION VALUE="business">Business</OPTION>
<OPTION VALUE="clubs_and_societies">Clubs & Societies</OPTION> <OPTION VALUE="information_services">Information
Services</OPTION> <OPTION VALUE="leisure_venues">Leisure Venues</OPTION> <OPTION VALUE="organisation_links">Orgaisation
Links</OPTION> <OPTION VALUE="religion_links">Religion Links</OPTION> <OPTION VALUE="residents_homepages">Residents
Homepages</OPTION> <OPTION VALUE="sports_clubs">Sports Clubs</OPTION> <OPTION VALUE="student_links">Student
Links</OPTION> </SELECT> </TD></TR> <TR> <TD WIDTH="12%"><B>Added by: </B></TD><TD WIDTH="88%">
<INPUT NAME="added_by" TYPE="text" ID="added_by" VALUE="<?php echo $added_by?>" SIZE="50">
</TD></TR><TR><TD WIDTH="12%" HEIGHT="13">Headline</TD><TD WIDTH="88%" HEIGHT="13"><INPUT NAME="headline" TYPE="text" ID="headline" VALUE="<?php echo $headline?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%">Byline
Name </TD><TD WIDTH="88%"><INPUT NAME="byline_name" TYPE="text" ID="byline_name" VALUE="<?php echo $byline_name?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%" HEIGHT="13">Appeared</TD><TD WIDTH="88%" HEIGHT="13"><INPUT NAME="appeared" TYPE="text" ID="appeared" VALUE="<?php echo $appeared?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%">Published</TD><TD WIDTH="88%"><INPUT NAME="published" TYPE="text" ID="published" VALUE="<?php echo $published?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%">Opening</TD><TD WIDTH="88%"><INPUT NAME="opening" TYPE="text" ID="opening" VALUE="<?php echo $opening?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%">body
text </TD><TD WIDTH="88%"><INPUT NAME="body_text" TYPE="text" ID="body_text" VALUE="<?php echo $body_text?>" SIZE="50"></TD></TR><TR><TD WIDTH="12%">Notes</TD><TD WIDTH="88%"><INPUT NAME="notes" TYPE="text" ID="notes" VALUE="<?php echo $notes?>" SIZE="50"></TD></TR>
</TABLE><P> <INPUT TYPE="Submit" NAME="Submit" VALUE="Submit"> <INPUT TYPE="reset" NAME="Reset" VALUE="Clear Story">
</P></FORM><P> <A HREF="edit.php">Back to Links Index</A> <P>
</body>
</html>

[/code]

Now if you kindly go to:

[a href=\"http://www.slougheaz.org/greenock/links/edit_news2.php?story_id=7178\" target=\"_blank\"]http://www.slougheaz.org/greenock/links/ed...p?story_id=7178[/a]

and delete something from any field and hit ‘submit’ a message for my required fields appears on a new screen. But…

The question is how do I get that message to appear on the first page in the variable named $msg IF all the fields are not filled in? IE Not on a new blank screen whilst still retaining the header information if the conditions are met?

Many Thanks

Chris
Link to comment
Share on other sites

[code]$error="";
       if (empty($section)){
        $error = "** You forgot to enter the section for the story! **<br>";
    }  else if (empty($added_by)){
        $error = "** Error: You forgot to who added the story! **<br>";
    }  else if (empty($headline)){
        $error = "** Error: You forgot to enter a headline for the story! **<br>";
    }  else if (empty($byline_name)){
        $error = "** Error: You forgot to enter a byline name for the story! **<br>";
    }  else if (empty($published)){
        $error = "** Error: You forgot to the date you wish to publish the story! **<br>";
    }  else if (empty($opening)){
        $error = "** Error: You forgot to the Opening Paragraph for the Indexes! **<br>";
    }  else if (empty($body_text)){
        $error = "** Error: You forgot to enter any body text! **<br>";}; [/code]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]how do I get that message to appear...................IF all the fields are not filled[/quote]

don't use else if
just if

and make $error an associative array
like so
[code]
$error = array();
    if (empty($section))
        $error['section'] = "** You forgot to enter the section for the story! **<br>";
    if (empty($added_by))
        $error['added_by'] = "** Error: You forgot to who added the story! **<br>";
    if (empty($headline))
        $error['headline'] = "** Error: You forgot to enter a headline for the story! **<br>";
    if (empty($byline_name))
        $error['byline_name'] = "** Error: You forgot to enter a byline name for the story! **<br>";
    [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.