Hi everyone,
I don’t know why but I can’t get my form to be checked if some of the fields are empty. With my code I get the “empty message” always. No matter if fields are filled or not. And if nothing is filled it’s still uploading information to db wich cannot be deleted from the pages.I tried with this
if ( !isset ($_POST['headline']) ||
!isset ($_POST['page']) ||
!isset ($_POST['story_text'])... )
{
echo'You must fill all fields !<a href="#'>Back</a>;
}
Another thing that I should add that this operation is executed in the file pointed in "action=’ ’ " in the form
Thank you in advance !
If you have defined a form - it will post the field on the submit ( provided you have a method=“post” in your form tag) even if these fields are not completed. The values will be blank - but isset will return true.
If you run the following and submit the form - you will notice the isset value = 1 regardless of any values entered.
echo "<textarea style='width:300;height:100'>";print_r($_POST);echo "</textarea>";
echo "<br>[".!isset ($_POST['headline'])."]";
echo "<form method='post'>";
echo "<input type='text' value='' name='somefield' id='somefield'>";
echo "<input type='submit'>";
echo "</form>";
Have you thought about Javascript validation before submitting the form ?
Something like this
<script language='javascript'>
function validate () {
if(document.getElementById('somefield').value=="") {
alert("You need to complete Somefield");
return false;
}
return true;
}
</script>
and then you need to change your form definition to read as follows
echo "<form method='post' onsubmit='return validate ()'>";
Sorry about the stupid question but I don’t know JS even if it’s similar.
Where’s the some field it should be the name of the field, right ?
And what about checking muliple fields ?
Have you thought about Javascript validation before submitting the form ?
You would still have to check again server side.
Like DidUSayScript pointed out, you’ll have to check the value of the fields as well:
if ( !isset ($_POST['headline']) ||
trim($_POST['headline'] == '' ||
!isset ($_POST['page']) ||
trim($_POST['page'] == '' ||
!isset ($_POST['story_text']) ||
trim($_POST['story_text'] == '' ... )
{
echo'You must fill all fields !<a href="#'>Back</a>;
}
Okay, I’ve managed to set up a JS code for validation. The problem is even when it alerts for empty field the php continue with query and add an empty one.
Here’s my js echoed in the php code.
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false;}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(headline,"The headline field is empty !")==false)
{email.focus();return false;}
}
}
</script>
From where are you executing the Javascript validation ?
If you do it in your form “onsubmit” event - you should do it as follows
onsubmit="return validate_form(thisform)"
otherwise it will submit the form regardless …
Well I found something suspishious 
the onsubmit was
onsubmit="return validate_form(this)"
but my function is (thisform)
function validate_form(thisform)
I rename it to (thisform) but the JS doesnt work. If it’s (this) JS runs and the query is executed EMPTY
Any ideas ? I think the problem is that JS is echoed in the html head. than means that the php starts the file runs some code find the echo , print it, and continue along. Here’s my whole php code. Please have a look.
<?php
include ('include_fns.php');
if (isset($_REQUEST['story'])) {
$story = get_story_record($_REQUEST['story']);
}
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<style type="text/css">
<!--
body {
background-color:#000033;
background-repeat:repeat-x;
font-family:"Courier New", Courier, monospace;
color:#FFFFFF;
font-size:10px;
font-weight:bold;
}
form{margin-left:300px;}
</style>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false;}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(headline,"The headline field is empty !")==false)
{email.focus();return false;}
}
}
</script>
</head>
<body>';
?>
<form action="story_submit.php" method="post" enctype="multipart/form-data" onsubmit="return validate_form(thisform)">
<input type="hidden" name="story" value="<?php echo $_REQUEST['story']; ?>">
<input type="hidden" name="destination"
value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
<table>
<tr>
<td>Заглавие
<td>
</tr>
<tr>
<td><input size="80" name="headline"
value="<?php echo $story['headline']; ?>"></td>
</tr>
<tr>
<td>Страница</td>
</tr>
<tr>
<td><?php
if (isset($_REQUEST['story'])) {
$query = "select p.code, p.description
from pages p, writer_permissions wp, stories s
where p.code = wp.page
and wp.writer = s.writer
and s.id =" . $_REQUEST['story'];
} else {
$query = "select p.code, p.description
from pages p, writer_permissions wp
where p.code = wp.page
and wp.writer = '{$_SESSION['auth_user']}'";
}
echo query_select('page', $query, $story['page']);
?>
</td>
</tr>
<tr>
<td>Текстът към статията (може да съдържа HTML тагове)</td>
</tr>
<tr>
<td><textarea cols="80" rows="7" name="story_text"
wrap="virtual"><?php echo $story['story_text']; ?></textarea>
</td>
</tr>
<tr>
<td>Снимка</td>
</tr>
<tr>
<td><input type="file" name="picture" size="40"></td>
</tr>
<?php
if ($story[picture]) {
$size = getImageSize('../' . $story['picture']);
$width = $size[0];
$height = $size[1];
?>
<tr>
<td><img src="<?php echo '../' . $story['picture']; ?>"
width="<?php echo $width; ?>" height="<?php echo $height; ?>"> </td>
</tr>
<?php
}
?>
<tr>
<td align="center"><input type="submit" value="Пусни" name="send"></td>
</tr>
<tr>
<td>* Статията подлежи на одобрение от Администатор</td>
</tr>
</script>
</table>
</form>
<?php echo '</body></html>'; ?>
I see various problems with this script, and also find some things quite peculiar. - Like for instance why do you echo the javascript and style bits but not the ‘form’ and ‘input’ bits ? Anyway it will still work - like I said - I just find it peculiar, and it will be a nightmare to debug or maintain by YOU in 6 months - let alone someone else.
Problems in the script
onsubmit=“return validate_form(thisform)”
and it should be
onsubmit=“return validate_form(this)”
and also ( probably because you used the sample from w3schools.com
)
email.focus
the field email does not exist in your form - so the javascript fails at this point, and without any further execution ( and thus NOT returning the ‘false’) - submits the form.