Page 1 of 1

How can I make a field not required

Posted: Mon Mar 19, 2007 10:38 am
by davanderbilt
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.

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">&nbsp; </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]

Posted: Mon Mar 19, 2007 10:43 am
by Chris Corbyn
Anything look funny about your code under our syntax highlighter? :)

Funny code

Posted: Mon Mar 19, 2007 10:56 am
by davanderbilt
It must be a Monday because I'm still missing it. I wrote this in Zend Studio and it didn't pick up any debug errors. If I comment out the gender function and the option on the page to select whether male or female, all the other values push through to the database just fine.

Posted: Mon Mar 19, 2007 4:20 pm
by davanderbilt
They team decided to just put in a dummy variable for males so I guess it is a moot point (except for my curiosity).

Posted: Mon Mar 19, 2007 4:26 pm
by Kieran Huggins
notice how the second half of your code is mostly red? Take a look at where that starts.


.
.
.
.
.
.


still need help? this is invalid:

Code: Select all

id="first_day" ;"

Posted: Mon Mar 19, 2007 4:37 pm
by RobertGonzalez
Another question to ask yourself is, where in the code is the requirement set? Then unset it.

Posted: Mon Mar 19, 2007 4:40 pm
by davanderbilt
I caught that ;" and fixed it, but that still didn't solve the problem.

Posted: Mon Mar 19, 2007 5:04 pm
by RobertGonzalez
So where in the code is the requirement actually enforced?

Posted: Tue Mar 20, 2007 8:14 am
by davanderbilt

Code: Select all

//this loop requires all the variables in the form to be filled in
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/>';
 }
} 
if($temp==NULL && $key!="submit" && $key!="action_h")
 {
   $error_rep=1;
   echo $key,' has not been filled <br/>';
  } 
}

Posted: Wed Apr 04, 2007 8:30 am
by davanderbilt
I have removed the code that requires all the fields to be filled and it works for any field except a field that is designated as date/time in Access.

If I type something in for each date field, the data does go into the database, but not all of these fields are required for every person. This error must be related to date fields, because if I leave the text fields blank, it goes into the DB just fine, but when I don't have a value for a field that is specified in Access as date/time it won't go in.

I get this 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\hh_add.php on line 269
There has been error while submitting your survey.
Line 269 is:

Code: Select all

$result = odbc_exec($conn,$insert_statement);
Here's one of my post statements for a date field:

Code: Select all

$first_day=isset($HTTP_POST_VARS['first_day']) ? $HTTP_POST_VARS['first_day'] :'00/00/0000';
I even tried taking out the isset function, but it still gives the same error.

Posted: Wed Apr 04, 2007 10:31 am
by RobertGonzalez
This is a SQL error, nothing else. How are dates entered into Acess? I know some DB's require wrapping date strings in #, like '#2007-12-31#'. Another thing to look at is the format of the date in the Access table. If you are entering one thing and the format is another thing, the DB will reject it.