JeniF 0 Junior Poster in Training

Good day to all!
I am working on some error checking and I have run into a snag in my code. Here is what I want to do:
Upon Submit, error check field criteria. This code has to do with posting Internal Job Postitions and based on the dates entered, the postings will display on either our regular website or on our corporate bulletin board. All of that works fine, but upon add if the admin enters a BBPosting Date, they must enter certain other criteria.
ie - if they entered a BBPostingDate, they must enter a facility. that actually works fine in my code. The problem is with a text area. This text are is also calling javascript to copy data from the entry to a hidden field, and I think this may be where the problem lies. I do get the error that they must enter this field, and it it retaining the values on the form, but it is not recognized as being posted in the error checking. here is the error checking code:

$AddData="no";
$MM_flag="MM_insert";
if((isset($_POST["Submit"])) && ($_POST["MM_insert"] == "addPosting")) {
$AddData="yes";
$startWarning="no";
if (!$_POST['Position']	
|| !$_POST['Region']
|| !$_POST['Status']
|| !$_POST['PositionPostingNum']
|| !$_POST['PositionStatus'] 
||($_POST['BBPostingDate'] != NULL && $_POST['BBDeadLine']== NULL)
||($_POST['WSPostingDate'] != NULL && $_POST['WSDeadLine']== NULL) 
|| ($_POST['BBPostingDate'] != NULL && $_POST['facility'] == NULL)
 || ($_POST['BBPostingDate'] != NULL  && $_POST['InternalPositionQual']==NULL)
|| ($_POST['BBPostingDate'] != NULL  && $_POST['InternalPositionEdu'] == NULL)
|| ($_POST['BBPostingDate'] != NULL  && $_POST['InternalPositionResp'] == NULL)
)   
{ 		
$AddData = "no";
$startWarning="yes";
?>
<?
// if WSPosting Date is not NULL enter the WSDeadLine  - display error message
if ($_POST['WSPostingDate'] != NULL && $_POST['WSDeadLine'] == NULL)
{ 
?> 
<div class="warning">
<li>You must enter the Website Dead Line Date if you have entered a Website Posting Date!</li></div>
<?php } ?>		
<?
// if no  Position Posting Number  - display error message
if (!$_POST['PositionPostingNum']) 
{ ?> 
<div class="warning">
<li>You must enter the Posting Position Number!</li></div>
<?php } ?>
<?
// if no Region  - display error message
if (!$_POST['Region']) 
{ ?> 
<div class="warning">
<li>You must enter the Region!</li></div>
<?php } ?>
<?
// if no Job  Position  - display error message
if (!$_POST['Position']) 
{ ?> 
<div class="warning">
<li>You must enter the Job Position!</li></div>
<?php } ?>				
<?
// if no  Position Status  - display error message
if (!$_POST['PositionStatus']) 
{ ?> 
<div class="warning">
<li>You must enter the Employment Status!</li></div>
<?php } ?>
<?
// if no Class  - display error message
if (!$_POST['Class']) 
{ ?> 
<div class="warning">
<li>You must enter the Employment Classification!</li></div>
<?php } ?>
<?
// if no Facility  - display error message
if ($_POST['BBPostingDate'] != NULL && $_POST['facility'] == NULL) 
{ ?> 
<div class="warning">
<li>You must enter the Facility if you have posted a Bulletin Board Date!</li></div>
<?php } ?>
<?
// if no Service  - display error message
if (!$_POST['ServiceCenter']) 
{ ?> 
<div class="warning">
<li>You must enter the Service Center!</li></div>
<?php } ?>
<?
// if no Work Schedule  - display error message
if (!$_POST['WorkSched']) 
{ ?> 
<div class="warning">
<li>You must enter the Work Schedule!</li></div>
<?php } ?>
<?
// if not selected Internal/External  - display error message
if (!$_POST['InternalExternal']) 
{ ?> 
<div class="warning">
<li>Please tell us if the job information is the same for the Web Site Posting!</li></div>
<?php } ?>
///////////////////////
All works fine until here!
////////////////////////////
<?
// if no Internal Position Qualifications  - display error message
if ($_POST['BBPostingDate'] != NULL  &&  !$_POST['InternalPositionQual']==NULL) 
{ ?> 
<div class="warning">
<li>Because you have entered a Posting Date, you must enter the Qualifications!</li></div>
<?php } ?>
<?
// if no Internal Position Education  - display error message
if ($_POST['BBPostingDate'] != NULL  &&  !$_POST['InternalPositionEdu']==NULL) 
{ ?> 
<div class="warning">
<li>Because you have entered a Posting Date, you must enter the Educational Requirements!</li></div>
<?php } ?>
<?
// if no Internal Position Responsibilities  - display error message
if ($_POST['BBPostingDate'] != NULL && !$_POST['InternalPositionResp'] == NULL) 
{ ?> 
<div class="warning">
<li>Because you have entered a Posting Date, you must enter the Job Responsibilities!</li></div>
<?php } ?>
<?
// if no Status  - display error message
if (!$_POST['Status']) 
{ ?> 
<div class="warning">
<li>You must set the Posting Status to "Active" or "Inactive"!</li></div>
<?php } ?>				
<div class="warningend"></div>			
<?php
} 
else
} 
//////////////////////// 
Here is the textbox code  for the  1 of the 3 areas, but basically they are the same(InternalPositionQual, InternalPositionEdu, and InternalPositionResp):
<tr valign="top" id="ExternalPositionQual" style="display:none;" name="ExternalPositionQual"> 
<th width="234" scope="row"><divalign="left">Qualifications:</div></th>
<th width="503" scope="row"><div align="left">
<textarea name="InternalPositionQual" cols="50" rows="4" id="InternalPositionQual"  onChange="copyData(this,document.addPosting.InternalPositionQual2)"
    onKeyUp="copyData(this,document.addPosting.InternalPositionQual2)">
<?php if(isset($_POST['InternalPositionQual'])) { echo stripslashes($_POST['InternalPositionQual']); } ?></textarea>
<input name="InternalPositionQual2" type="hidden" value="" />
</div></th>
</tr>

//The actual form submission includes this:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addPosting")&& $AddData!="no") {
  $insertSQL = sprintf("INSERT INTO jobpostings .......

any help or suggestions would be greatly appreciated!

Dani AI

Generated

Likely causes: a server-side logic mistake combined with relying on client JS to populate a second field. Empty textarea values are posted as an empty string (""), not NULL, and expressions like !$var == NULL will not behave as intended because of operator precedence. Also, a client-side copy routine can fail to run before submit (or the field might be disabled/misnamed), so the server never sees the value you expect. A quick var_dump($_POST) (or check the request payload in the browser Network tab) will show exactly what is being submitted.

A safer, clearer approach is to do authoritative validation on the server and treat JavaScript as UI convenience only. Use the request method check, normalize values with trim(), and test with empty() or strict equality against the empty string. Collect error messages in an array and render them together so the logic is easier to follow and you avoid nested boolean spaghetti. Example pattern:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $errors = [];
  $qual = trim($_POST['InternalPositionQual'] ?? '');
  if ($bbDate !== '' && $qual === '') {
    $errors[] = 'Qualifications required when a bulletin-board date is set.';
  }
  if ($errors) { /* show errors and stop */ } else { /* process with prepared statements */ }
}

If you must keep the hidden-field copy, ensure the copy runs on the form onsubmit event so it always executes regardless of how the user changes the textarea. Verify the textarea has the correct name and is not disabled. Use browser devtools to confirm the POST body contains the expected fields.

Also review brace/else placement (syntax errors will stop validation from running). For security and future maintenance: escape output with htmlspecialchars() when reprinting inputs, and use prepared statements (PDO or mysqli) for DB inserts. For reference on PHP input checks see the manual for empty() (https://www.php.net/empty) and for reliably running code before submit see the form submit event docs (https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event). , these changes should make the server-side checks deterministic and stop false "missing field" errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.