I am using a form with a dropbox. The dropbox has 18 schools to select from. When the visitor submits the form I need a the info to be emailed to the correct school email address. I tried different things being new to PHP but I can’t quite get the email sent to the right place. I’ll post what I thought would work but the email is not going to the correct location. Any help would be appreciated.
<?php
include 'schools.php';
echo '<select name=school>' . PHP_EOL ;
foreach( $schools as $school){
// only pass the school id number back to your form handler
echo '<option value='.$school['id'].'>'.$school['name'].'</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL ;
Then use the array again in your form handler
handler.php
<?php
include 'schools.php';
if( isset($_POST['school']) && (int)$_POST['school'] > 0 ){
echo 'Send email to '. $schools[$_POST['school']]['email'];
}else{
// a valid school number was not submitted
header('Location : /');
exit();
}
Better than trying to write 18 if/else statements … what if you add another school? Best keep it in one place
The only thing Cups is that as a newbie to PHP your solution is confusing me. For now I can keep up with if statements better. Could you tell me the solution going in that direction for now.
You could ask questions about particular things you don’t understand about the solution I proposed.
Here’s the same with some more comments:
<?php
// include the array previously made, now it is available to this part of your application
// which is creating a drop-list of your schools
include 'schools.php';
// start your form
echo '<form method=POST action = handler.php>';
// start the select box
echo '<select name=school>' . PHP_EOL ;
foreach( $schools as $school){
// output each school,
// only pass the school id number back to your form handler
// add and End Of Line (return) to each option so your html source code can be read
// when you "view source code" in your browser
echo '<option value='.$school['id'].'>'.$school['name'].'</option>' . PHP_EOL;
}
// close the select box
echo '</select>' . PHP_EOL ;
// gives
//<select name=school>
//<option value=1>School One</option>
//<option value=2>School Two</option>
//<option value=3>School Three</option>
//</select>
// now get on with other elements of your form
echo '<input type = submit />';
echo '</form>';
Then imagine that the form was POST ed to this script, handler.php
<?php
// again include the schools array which essentially you maintain and keep up to date
// in one place -- but reuse it
include 'schools.php';
// check to see if a school was chosen, and that an expected number was sent
// not a string which could be an attack
// (you could go on and check the number was not greater than you expect too)
if( isset($_POST['school']) && (int)$_POST['school'] > 0 ){
echo 'Send email to '. $schools[$_POST['school']]['email'];
}else{
// a valid school number was not submitted
// so send the user away to start again, maybe?
header('Location : /index.php');
exit();
}
To answer your immediate question, if you intent on doing it the hard way then take a look at PHP switch instead then:
<?php
switch ($school) {
case "School One":
$email_to = "sch1@email.com";
break;
case "School Two":
$email_to = "sch2@email.com";
break;
}
?>
+1 for using a switch instead of if/else if you don’t want to go down the array route, but to answer the question of why your original script wasn’t working, you had this:
if ($school = "Ashville Elementary")
That sets the $school variable to “Ashville Elementary” so the if conditional is successful, as it has been able to set $school as you asked. What you need is this:
if ($school == "Ashville Elementary")
Which means if $school IS EQUAL TO “Ashville Elementary”, and that’ll either be true or false. The next line, which sets $email_to, is correct as it is.
Here is my form guys. As you can see in the last entry that only 3 schools will be used for the form now. To pass along the school info in the drop box should I use $school in my form email script to pass that info along?
Here is my form handler at password-form-thanks.php
<?php
if(isset($_POST[‘email’])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
if
($school == "Ashville Elementary")
$email_to = "martin1028@gmail.com";
$email_subject = "Password Information Needed";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['pname']) ||
!isset($_POST['cname']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['school'])) {
died('We are sorry, but there appears to be a problem with the form you submitted. Please try again');
}
$pname = $_POST['pname']; // required
$cname = $_POST['cname']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$school = $_POST['school']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= ‘The Email Address you entered does not appear to be valid.<br />’;
}
$string_exp = “/[1]+$/”;
if(!preg_match($string_exp,$pname)) {
$error_message .= ‘The Parent Name you entered does not appear to be valid.<br />’;
}
if(!preg_match($string_exp,$cname)) {
$error_message .= ‘The Child Name you entered does not appear to be valid.<br />’;
}
if(strlen($school) < 2) {
$error_message .= ‘The School you entered do not appear to be valid.<br />’;
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.
I realized I was out of my league on this so I wanted to start learning by using “case” and then move on to arrays. I created a html drop box like this.
I get no error messages but the results do not send. Can anyone help. I’ve studied this problem for two days. Obviously if I can get this worked out I need to start learning PHP from the very beginning.
Nearly there old bean, you’ve grasped the logic of the switch, what you are missing is this bit, the bit that sends the mail
// you did some working out to get $email_to to go
// to the correct recipient
$subject = "This is a notification";
$contents = "this is some content\
Thank you!\
Goodbye";
mail( $email_to, $subject, $contents);
This is where you now study PHPs mail() function [fphp]mail[/fphp]