Hoe verstuur ik onderstaand forumulier nou naar een e-mailadres?
<<<<<< start of index.php >>>>>>
<?php
include "array.php";
// everything between these {} will be done when the form is submitted
if ($action == "sent")
{
// $options is the array that contains all the questions and their specifics. It's located in array.php
foreach ($options AS $number => $array)
{
if($array[type] == "radio" && $array[required] == "yes")
{
// this means none of the radiobuttons for this question have been checked
if(!$radio[$number])
{
$error[$number] = "<tr>\n<td colspan=\"3\">\n\t<font color=\"red\">You forgot to answer this question!</font>\n </td>\n</tr>\n";
}
}
if($array[type] == "check" && $array[required] == "yes")
{
// this means none of the checkboxes for this question have been checked
if (!$checkbox[$number])
{
$error[$number] = "<tr>\n<td colspan=\"3\">\n\t<font color=\"red\">You have to at least answer one of these options</font>\n </td>\n</tr>\n";
}
}
if ($array[type] == "text" && $array[required] == "yes")
{
// this means nothing has been entered into the text field
if (!$text[$number])
{
$error[$number] = "<tr>\n <td colspan=\"3\">\n\t<font color=\"red\">You forgot to answer this Question</font>\n </td>\n</tr>\n";
}
}
// you can add as many of these as you want to, if more values have to be check for their validity
if ($array[type] == "text" && $array[required] == "yes" && $array[special] == "email")
{
// this will check if the entered value is a valid email address
if (!preg_match("/(\w+\-*)\@{1}(\w+\-?\w+)(\.\w+\-?\w+)*(\.{1}\w{2,3})$/", $text[$number]))
{
$error[$number] = "<tr>\n <td colspan=\"3\">\n\t<font color=\"red\">The email address you entered is not a valid one</font>\n </td>\n</tr>\n";
}
}
}
if (!$error)
{
// if the parser got to this point, it means everything required was filled in
// and the values were valid ones. so do whatever you want to do here and then exit the script
exit();
}
}
?>
<html>
<body>
<form action="<?php echo $PHP_SELF; ?>" method="post" name="submit_form">
<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000" width="80%">
<tr>
<td>
<table border="0" cellpadding="5" cellspacing="0" bgcolor="#CACACA" width="100%">
<?php
foreach ($options AS $number => $array)
{
// just delete $number if you don't want it to be displayed. But let the <td> intact
echo $error[$number];
echo "<tr>\n <td valign=\"top\" width=\"1%\">\n\t$number.\n </td>\n";
echo " <td align=\"left\" valign=\"top\">\n\t$array[question]\n </td>\n";
if ($array[type] == "radio")
{
echo " <td align=\"left\">\n\t";
$count = 1;
foreach ($array[options] AS $option)
{
if ($radio[$number] == $count)
{
$check = " checked";
}
else
{
$check = "";
}
echo "<input type=\"radio\" name=\"radio[$number]\" value=\"$count\"$check>$option $array[break]\n";
$count++;
}
echo " </td>\n";
}
elseif ($array[type] == "check")
{
echo " <td align=\"left\">\n\t";
$count = 1;
foreach ($array[options] AS $option)
{
if ($checkbox[$number][$count] == "on")
{
$check = " checked";
}
else
{
$check = " ";
}
echo "<input type=\"checkbox\" name=\"checkbox[$number][$count]\"$check>$option $array[break]\n";
$count++;
}
echo " </td>\n";
}
elseif ($array[type] == "text")
{
echo " <td align=\"left\">\n\t<input type=\"text\" name=\"text[$number]\" value=\"$text[$number]\" size=\"$array[size]\">\n </td>\n";
}
}
?>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="5" cellspacing="0" bgcolor="#CACACA" width="100%">
<tr>
<td align="center" width="50%">
<input type="submit" value="Submit Form">
</td>
<td align="center" width="50%">
<input type="reset" value="Reset Form">
</td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="action" value="sent">
</form>
</body>
</html>
<<<<<<< end of index.php >>>>>>>
<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>
<<<<<< start of array.php >>>>>>
<?php
$options = array(
"1" => array(
"type" => "radio",
"question" => "hoe oud ben je?",
"options" => array("0-15","16-23","24-30","31-ouder"),
"break" => "<br>",
"required" => "yes"
),
"2" => array(
"type" => "check",
"question" => "waar programmeer je in?",
"options" => array("php","asp","geen van beide"),
"break" => " ",
"required" => "yes"
),
"3" => array(
"type" => "text",
"question" => "wat is je email adres",
"size" => "30",
"required" => "yes",
"special" => "email"
)
);
?>
<<<<<<< end of array.php >>>>>>>
1.160 views