Hi all,
I’m new in PHP, and I’ve surf the web and found some php code which I’ve use it in my webpage ([URL]http://www.foolishman.net/testphp.htm
Now, I have some problems and want to seek for your kind help.
1/ how to validation selection box (the required field) to be selected (point 4 in the webpage);
2/ how to retain (pre-filled) the choice which the vistor has already selected after an error was found (say, if you only enter your NAME and select the Sex and then press ‘submit’, an error page is shown, but, the choice (I only know how to retain a textbox message) is missed);
3/ how to validate the data (point 6 to 9 in the webpage) need to be fill if ABC course (point 5 in the webpage) is selected, also the data (point 10) if DEF course (point 5 in the webpage) is selected;
4/ how can I get the all the [B]selected[B]last one
Below is the PHP code (testphp_process.php) which I used for the webpage.
As I just gather and merge the code from the web, I’m not sure whether it is something confusing or duplicate or outdated, so, I’m very happy if anyone of you can have a look on it and give me some advices on the above problems and the code below. Thanks~~!
[code=php]<?php
function getAddress()
{
if(!empty($_SERVER[‘HTTP_CLIENT_IP’]))
{
$ip_expl = explode(‘.’, $_SERVER[‘HTTP_CLIENT_IP’]);
$referer = explode(‘.’, $_SERVER[‘REMOTE_ADDR’]);
if($referer[0] != $ip_expl[0])
{
$ip = array_reverse($ip_expl);
$return = implode(‘.’, $ip);
}
else
{
$return = $client_ip;
}
}
elseif(!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’]))
{
if(strstr($_SERVER[‘HTTP_X_FORWARDED_FOR’], ‘,’))
{
$ip_expl = explode(‘,’, $_SERVER[‘HTTP_X_FORWARDED_FOR’]);
$return = end($ip_expl);
}
else
{
$return = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
}
}
else
{
$return = $_SERVER[‘REMOTE_ADDR’];
}
return $return;
}
extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
if ($action == “send”)
{
include(“testphp_config.php”);
$ip = getAddress();
$to = $ademail;
$name = $_POST[‘student_name_eng’];
$from = $_POST[‘sender_email’];
$sex = $_POST[‘sex’];
$coursestotake = $_POST[‘applycourse’];
$to = trim($to);
$name = trim($student_name_eng);
$from = trim($from);
$sex = trim($sex);
$coursestotake = trim($applycourse);
if (empty($to))
{
$ermessage = “ERROR¡JEmail address to can not be blank, Please enter your email address in the config file!”;
include(“testphp_error.htm”); exit();
}
if (empty($name))
{
$ermessage = “ERROR: Please enter your name.”;
include(“testphp_error.htm”); exit();
}
if (empty($sex))
{
$ermessage = “ERROR: Please enter your sex.”;
include(“testphp_error.htm”); exit();
}
if (empty($coursestotake))
{
$ermessage = “ERROR: Please select one or more courses.”;
include(“testphp_error.htm”); exit();
}
$body =
“Application Form Details¡Jn_______________________________________n”.
“nStudent Name¡J”.$name.
“nSex¡J”.$sex.
“nEmail¡J”.$from.
“nCourse Apply¡J”.$coursestotake.
“n”.
“n************************************************************”.
“nDate and Time¡J”.date(“Y-m-d D H:i:s”).
“nIP¡J”.$ip.
“n************************************************************”;
$bodycopy =
“n************************************************************”.
“nAutorespond”.
“n************************************************************”.
“n”.
“n____________________________________________________________n”.
“nStudent Name¡J”.$name.
“nSex¡J”.$sex.
“nEmail¡J”.$from.
“nCourse Apply¡J”.$coursestotake.
“n____________________________________________________________n”.
“n”.
“n”.
“nThank you for your interest in our courses”.
“n”.
“n”.
“n”;
$send = mail($to, “applicationform”, $body, “From: $name”);
if ($send)
{
include(“testphp_thankyou.htm”);
// send copy of email to sender
mail(“$name<$from>”, “¡iAutorespond¡jThank you for your application¡I”,
$bodycopy, “From: XXXXXXX <$to>”);
exit();
}
else
{
$ermessage = “ERROR¡Jinformation cannot be send, please try again.”;
include(“testphp_error.htm”); exit();
}
}
else
{
include(“testphp.htm”);
}
?>
Below is the error page (testphp_error.htm).
[code=html]………
<form name=”applicationform” method=”post” action=”testphp_process.php?action=send”>
………………
<td colspan=”4″ rowspan=”1″ valign=”undefined”><font color=”red[COLOR=Red]”><?php echo $ermessage; ?>[/COLOR]</font></td>
………….
<td style=”width: 436px;” align=”undefined” valign=”undefined”>
<input size=”30″ name=”student_name_eng” [COLOR=Red]value=”<?php echo $name; ?>”[/COLOR]></td>
………………..
<td align=”undefined” valign=”undefined”><input name=”sex” value=”male” type=”radio”>Male</td>
<td align=”undefined” valign=”undefined”><input name=”sex” value=”female” type=”radio”>Female</td>
……………
<td align=”undefined” valign=”undefined”>
<input size=”30″ name=”sender_email” [COLOR=Red]value=”<?php echo $from; ?>”[/COLOR]></td>
…………………..
<td align=”undefined” valign=”undefined”>Year
of Birth</td>
<td align=”undefined” valign=”undefined”>
<select name=”dob_year”>
<option>Please select</option>
<option>1987</option>
……………….
</select>
……………………
<td style=”width: 200px;” align=”undefined” valign=”undefined”>Course Apply<br>
(You can select one or more courses)</td>
………………………
<td><input value=”abccourse” name=”applycourse” type=”checkbox”>ABC course
(complete point 6 to 9)</td>
<td align=”undefined” valign=”undefined”><input value=”defcourse” name=”applycourse” type=”checkbox”>DEF
course (complete point 10)</td>
<td><input value=”ghicourse” name=”applycourse” type=”checkbox”>GHI course</td>
<td align=”undefined” valign=”undefined”><input value=”jklcourse” name=”applycourse” type=”checkbox”>JKL
course</td>
………………
<td style=”width: 200px; background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>”ABC course
details”</td>
……………….
<td style=”width: 200px; background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>Days a week</td>
………………
<td align=”undefined” valign=”undefined”><input value=”one” name=”abccourse_day_per_week” type=”radio”>One
day</td>
<td><input value=”two” name=”abccourse_day_per_week” type=”radio”>Two days</td>
<td align=”undefined” valign=”undefined”><input value=”three” name=”abccourse_day_per_week” type=”radio”>Three
days</td>
<td><input value=”four” name=”abccourse_day_per_week” type=”radio”>Four days</td>
<td align=”undefined” valign=”undefined”><input value=”five” name=”abccourse_day_per_week” type=”radio”>Five
days</td>
……………
<td style=”background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>Day</td>
<td align=”undefined” valign=”undefined”><input value=”Mon” name=”abcevery_[]” type=”checkbox”>Every
Monday<br>
</td>
<td><input value=”Tue” name=”abcevery_[]” type=”checkbox”>Every Tuesday<br>
</td>
<td><input value=”Wed” name=”abcevery_[]” type=”checkbox”>Every Wednesday</td>
<td align=”undefined” valign=”undefined”><input value=”Thur” name=”abcevery_[]” type=”checkbox”>Every
Thursday</td>
<td align=”undefined” valign=”undefined”><input value=”Fri” name=”abcevery_[]” type=”checkbox”>Every
Friday</td>
……………
<td style=”background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>Start Time</td>
<select name=”abccourse_starttime”>
<option selected=”selected”>Please select</option>
<option>09:30</option>
…………….
</select>
start </td>
………..
<td style=”width: 200px; background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>Course Start Date</td>
<td style=”width: 436px; background-color: rgb(255, 255, 153);” align=”undefined” valign=”undefined”>
<td>
<select name=”abccourse_startdate_month”>
<option selected=”selected”>Please select</option>
<option>01</option>
…………………..
</select>
month</td>
<td align=”undefined” valign=”undefined”>
<select name=”abccourse_startdate_day”>
<option selected=”selected”>Please select</option>
<option>01</option>
…………………
</select>
day</td>
………………..
<td style=”background-color: rgb(255, 204, 204);” align=”undefined” valign=”undefined”>”DEF course
details”</td>
…………….
<td style=”background-color: rgb(255, 204, 204);” align=”undefined” valign=”undefined”>Main Subject<br>
(You can select one or all courses)</td>
…………………
input value=”English” name=”defcourse_subject[]” type=”checkbox”>English</td>
<td><input value=”Mathematics” name=”defcourse_subject[]” type=”checkbox”>Mathematics</td>
………………
<td style=”width: 436px;” align=”undefined” valign=”undefined”>
<input value=”Submit” name=”submit” type=”submit”>
<input value=”Reset” name=”reset” type=”reset”></td>