Hi,
I have this form that has a list of topics that a user must select. If the user chooses "Other" from the Selection List then the Other Topic which is a textfield must be entered along with other required fields when a user presses the continue button. The PHP page posts to itself for field validation. I managed to make it work partly that is if the user selects Other and presses the continue button then the error to enter the Other Topic is shown. Now, if the user inputs something in the textbox, then the page is just redisplayed which it shouldn't be doing. It should go to another page. The PHP coding is a bit complex, I've taken it from the web and i've done some slight modifications to it. I would very much appreciate it if someone could help me with this. I've highlighted my added line regarding the Other Field validation
Here is my complete code:
<?PHP
session_register();
header("Cache-control: private");
function check()
{
if ($POST['process'] == 1)
{
$sentarray=array();
$tocheck=explode(',',$POST['required']);
$error[0]="Errors:";
foreach ($tocheck as $t)
{
if(!array_key_exists($t,$_POST))
{$error[]=$t;}
}
foreach (array_keys($_POST) as $p)
{
if(!preg_match('/initvalue/',$p) and !preg_match('/required/',$p))
{
if ($_POST[$p] == $_POST[$p.'initvalue']) {
$sentarray[$p] = '';
} else {
$sentarray[$p] = $_POST[$p];
}
//$sentarray[$p]=$_POST[$p]==$_POST[$p.'initvalue']?'':$_POST[$p];
}
foreach ($tocheck as $c)
{
if ($p==$c and $sentarray[$p]=='')
{
$error[]=$_POST[$p.'initvalue'];
}
}
}
[B]if ( ($_POST['Topic'] == "Other") and (empty($POST['Other_Topic']) ) )
$error[]="Other Topic";[/B]
return $error[1]==''?$sentarray:$error;
}
}
function add($f)
{
global $errorindicator,$errorclass,$Javascript;
$tocheck=explode(',',','.$_POST['required']);
preg_match('/id="(.*?)"/i',$f,$i);
preg_match('/name="(.*?)"/i',$f,$n);
preg_match('/type="(.*?)"/i',$f,$t);
preg_match('/value="(.*?)"/i',$f,$iv);
$n=$n[1];
$iv=$iv[1];
$i=str_replace('_',' ',$i[1]); // Retire le souligne de l'id
if(preg_match('/<textarea/',$f))
{
//$v=$_POST[$n]==''?$i:$_POST[$n];
if ($_POST[$n] == '') {
$v = $i;
} else {
$v = $_POST[$n];
}
$f=preg_replace('/<textarea(.*?)>(.*?)<\/textarea>/','<textarea\\1>'.stripslashes(htmlentities($v)).'</textarea>',$f);
if($Javascript)
{
$f=preg_replace('/<textarea/','<textarea onfocus="this.value=this.value==\''.$i.'\'?\'\':this.value"',$f);
}
}
if(preg_match('/<select/',$f))
{
preg_match('/<select.*?>/',$f,$st);
preg_match_all('/<option value="(.*?)">(.*?)<\/option>/',$f,$allopt);
foreach ($allopt[0] as $k=>$a)
{
if($_POST[$n]==$allopt[1][$k] || ($_POST[$n]=='' && $k==0))
{
$preg='/<option value="';
$preg.=$allopt[1][$k].'">'.$allopt[2][$k].'<\/option>/si';
$rep='<option selected="selected" value="';
$rep.=$allopt[1][$k].'">'.$allopt[2][$k].'</option>';
$f=preg_replace($preg,$rep,$f);
}
}
}else
{
switch ($t[1])
{
case 'text':
$v=$_POST[$n]==''?'value="'.$i.'"':'value="'.stripslashes(htmlentities($_POST[$n])).'"';
$f=preg_replace('/<input/','<input '.$v,$f);
if($Javascript){$f=preg_replace('/<input/','<input onfocus="this.value=this.value==\''.$i.'\'?\'\':this.value"',$f);}
break;
case 'checkbox':
$v=$_POST[$n]=='on'?' checked="checked"':'';
$f=preg_replace('/<input/','<input'.$v,$f);
break;
case 'radio':
$f=$_POST[$n]==''?$f:preg_replace('/checked.*?\s/','',$f);
//$f=preg_replace('/checked.*?\s/','',$f);
$v=$_POST[$n]==$iv?' checked="checked"':'';
$f=preg_replace('/<input/','<input'.$v,$f);
break;
}
}
$f.='<input type="hidden" name="'.$n.'initvalue" value="'.$i.'" />';
if (array_search($n,$tocheck) and ($_POST[$n]=='' or $_POST[$n]==$i)){
if($errorindicator!='')
{
$f=$errorindicator.$f;
}
if($errorclass!='')
{
$f=preg_replace('/name=/i','class="'.$errorclass.'" name=',$f);
}
}
return $f;
}
$errorindicator='<img src="alert.gif" width="20" height="20" alt="Alert" title="Indicator for missing form element" border="0" />';
$errorclass="error";
$Javascript=true;
if ($_POST['process'] == 1)
{ $results=check();
if ($results['Title']!='')
{
session_start();
$_SESSION['POST_VARS'] = base64_encode(serialize($_POST));
session_write_close();
header("location: abstract_page_2.php");
exit();
}
} ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title></title>
<link href="styles.css" type="text/css" rel="StyleSheet">
</head>
<body>
<table width="50%" border="0" cellspacing="1" cellpadding="0" bgcolor="#eb6093">
<tr>
<td>
<table width="50%" border="0" cellspacing="0" cellpadding="5" bgcolor="white">
<tr>
<td><br/>
<form action="abstract_page_1v3.php" method="post">
<div align="left">
<img src="images/i_abstract_information.gif" alt="" height="30" width="700" border="0"/><?PHP if($results[0]=='Errors:') {?><br/>
<h2 class="errorhead">There has been an error:</h2>
<p>You forgot to enter the following field(s)</p>
<?PHP
foreach ($results as $i=>$e)
{
if ($i>0)
{
echo "<li>$e</li>";
}
}
}
?>
<p><input type="hidden" name="required" value="Title,Topic" /><input type="hidden" name="process" value="1" /></p>
<table width="100%" border="0" cellspacing="0" cellpadding="5" bgcolor="white">
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="25%"><font color="red" face="Verdana">*</font><font face="Verdana"><b>Title:</b></font></td>
<td><?=add('<input type="text" name="Title" id="Title" />')?></td>
</tr>
<tr>
<td width="25%"></td>
<td></td>
</tr>
<tr>
<td width="25%"><font color="red" face="Verdana">*</font><font face="Verdana"><b>Topic:</b></font></td>
<td><?=add('
<select id="Topic" name="Topic" onchange="Enable_Other_Topic_Field(this); ">
<option value="">Select A Topic</option>
<option value="Angiogenesis">Angiogenesis</option>
<option value="Clinical Therapeutics">Clinical Therapeutics</option>
<option value="Genetics and Imaging">Genetics and Imaging</option>
<option value="Metastasis and Metastatic Disease">Metastasis and Metastatic Disease</option>
<option value="Pathology and Molecular Profiling">Pathology and Molecular Profiling</option>
<option value="Receptors and Hormones">Receptors and Hormones</option>
<option value="Signal Transduction">Signal Transduction</option>
<option value="Tumor Microenvironment and Cancer Stem Cells">Tumor Microenvironment and Cancer Stem Cells</option>
<option value="Other">Other</option>
</select>
')?></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="25%"><font face="Verdana"><b>Other topic<br/>
(please specify):</b></font></td>
<td><?=add('<input type="text" name="Other_Topic" id="Other_Topic" />')?></td>
</tr>
<tr>
<td width="25%"></td>
<td></td>
</tr>
<tr>
<td width="25%"><font color="red" face="Verdana">*</font><font face="Verdana"><b>Type of presentation:</b></font></td>
<td>
<table width="30%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><?=add('<input type="radio" name="Presentation_Type" id="Oral" checked="checked" value="Oral" />')?><font face="Verdana">Oral</font></td>
<td><?=add('<input type="radio" name="Presentation_Type" id="Poster" value="Poster" />')?><font face="Verdana">Poster</font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<table border="0" cellspacing="0" cellpadding="25">
<tr>
<td><input type="image" name="Submit" src="images/continue.gif" alt="" tabindex="7"/></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>