How can I make a field not required
Posted: Mon Mar 19, 2007 10:38 am
I have a php form that links up to an Access database where all of the fields are required and one of the fields asks for a date for females only. I can't figure out how to not require a value for this date field (first_day) if the person entering the form is male. I also don't want to have to put in a real date for a value if the person filling out the form is male, because then that means we have dummy data in the database. Here is some of the code I'm working with.
--------------------------------------------------
The first_day value needs to go into an Access database, where the field type is Date/Time. When I submit, I get the following error:
If I remove the part of the form where it asks if you are female and comment out the gender function, it still gives a data type mismatch if I manually type in 00/00/00 as the first_day date. Then if I manually type in 01/01/01 as the first_day date, the data goes into the database just fine. I have a lot of other fields on this page and all the others require values, and I am stuck trying to figure out how to not require a value for first_day if the person entering the form is male. I also don't want to have to put in a real date for a value if the person filling out the form is male. Any ideas?
d11wtq | Please use [syntax ],
Code: Select all
<script type="text/javascript" language="JavaScript">
function gender()
{
$val=document.dlw.Gender.options[document.dlw.Gender.selectedIndex].value;
if($val=="2")
{
document.dlw.first_day.value="00/00/00";
document.dlw.first_day.disabled=true;
}
if($val=="1")
{
document.dlw.first_day.disabled=false;
}
}
</script>
//I need to be able to make first_day a fillable field only for females, so I created this and have it referencing the gender function above
<p align="justify"><strong><span class="style24">Are You Female?</span>
<select name="Gender" id="Gender" onChange="gender()">
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</strong></p>
<p align="justify"><strong>Females only (Date of first day of menstrual cycle): </strong> (Format:01/01/07)
<input name="first_day" type="text" id="first_day" ;" size="10" maxlength="10">
</p>
//Insert statement to put first_day into the Access database
//Gender doesn't need to go into the database. I just need it to make the first_day field fillable for females only.
<?php
echo '<br><table><tr><td><b>';
foreach ($HTTP_POST_VARS as $key => $value) {
$temp = stripslashes($value);
$HTTP_POST_VARS[$key] = $temp;
if($temp==" ")
{
$error_rep=1;
echo $key,' has not been filled <br/>';
}
}
echo '</td></tr></table></b>';
$Gender=isset($HTTP_POST_VARS['Gender'])?$HTTP_POST_VARS['Gender']:'NA';
$first_day=isset($HTTP_POST_VARS['first_day']) ? $HTTP_POST_VARS['first_day']:'00/00/0000';
$conn = odbc_connect ('LTE_Master', '', '');
$id= odbc_exec($conn,$get_stat);
$insert_statement = "
insert into
[AccessTable]
values ('$first_day')";
if($error_rep==0)
{
$result = odbc_exec($conn,$insert_statement);
if(!$result)
{
//echo 'There has been error while submitting your survey. Contact...';
?>
</p>
<h1 align="center" class="style1"> </h1>
<p align="center" class="style1"><font color="#FF0000">There has been an ERROR submitting your Data.</font> </p>
<?php
}
elseif ($result)
{
?>The first_day value needs to go into an Access database, where the field type is Date/Time. When I submit, I get the following error:
Code: Select all
Warning: SQL error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression., SQL state 22005 in SQLExecDirect in c:\program files\apache group\apache\htdocs\lte\dlw_add.php on line 157
There has been an ERROR submitting your Data.If I remove the part of the form where it asks if you are female and comment out the gender function, it still gives a data type mismatch if I manually type in 00/00/00 as the first_day date. Then if I manually type in 01/01/01 as the first_day date, the data goes into the database just fine. I have a lot of other fields on this page and all the others require values, and I am stuck trying to figure out how to not require a value for first_day if the person entering the form is male. I also don't want to have to put in a real date for a value if the person filling out the form is male. Any ideas?
d11wtq | Please use [syntax ],
Code: Select all
or [code ] tags when posting code in the forum. Whichever is most appropriate.[/color]