Newsbie Help

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.

if
($school = "Ashville Elementary")
$email_to = "name1@domain.org";
elseif
($school = "Ashville Middle")
$email_to = "name2@domain.org";
elseif
($school = "Ashville Middle")
$email_to = "name3@domain.org";
else
$email_to = "name4@domain.org";

Do an echo of $school. Does it contain the value you were expecting?

So, 18 schools and 18 different emails, correct?

I’d really consider maintaining a single array in a file which you include as it is needed.

schools.php


<?php

$schools = array(

array(
'id'=>1,
'name'=>'School One',
'email'=>'sch1@mail.com'),

array(
'id'=>2,
'name'=>'School Two',
'email'=>'sch2@mail.com'),

array(
'id'=>3,
'name'=>'School Three',
'email'=>'sch3@mail.com'),

);

a) to create your html form elements as needed

website page html


<?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 :wink:

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.

guido2004, if I do a test using only one if statement it does pass the expected info.

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.

Good call Martin. I completely missed that last night. That’s why some prefer turning things around:

if ("Ashville Elementary" = $school)

would throw an error.

Thanks so much guys for your help. Things are making more sense now. I’ll go to work on this and get back with you if needed. You guys are great!

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?

<form id=“form1” method=“post” action=“password-form-thanks.php”>
<p>Parent Name<br />
<input type=“text” name=“pname” size=“30” />
</p>
<p>Child’s Name<br />
<input type=“text” name=“cname” size=“30” />
</p>
<p>Email<br />
<input type=“text” name=“email” size=“30” />
</p>
<p>Contact Information for Parent – Phone Number<br />
<input type=“text” name=“telephone” size=“30” />
</p>
<p>School<br />
<select name=“school”>
<option>Ashville Elementary</option>
<option>Ashville Middle</option>
<option>Ashville High</option>
</select>
</p>
<p>
<input type=“submit” value=“Submit” name=“submit”>

Your form is using the method POST so you access the school chosen from $_POST[‘school’];

When starting out always use this in your postback form handlers temporarily till you dig what is going on:


<?php

var_dump($_POST);

Hmm, what do you mean by that? Do you want to select the choice made by the user?

guido, yes I do.

Ok, so the form is sent to this script: password-form-thanks.php

Is this the same script as the form? Or a different one?

Do you mean the code should be the first code mentioned?

Yepper, I am starting to feel I am over my head on a simple form.

Here is my form

<form id=“form1” method=“post” action=“password-form-thanks.php”>
<p>Parent Name<br />
<input type=“text” name=“pname” size=“30” />
</p>
<p>Child’s Name<br />
<input type=“text” name=“cname” size=“30” />
</p>
<p>Email<br />
<input type=“text” name=“email” size=“30” />
</p>
<p>Contact Information for Parent – Phone Number<br />
<input type=“text” name=“telephone” size=“30” />
</p>
<p>School<br />
<select name=“school”>
<option>Ashville Elementary</option>
<option>Ashville Middle</option>
<option>Ashville High</option>
<option>John Pope Eden</option>
<option>Margaret Elementary</option>
<option>Moody Elementary</option>
<option>Moody Middle</option>
<option>Moody Jr High</option>
<option>Moody High</option>
<option>Odenville Elementary</option>
<option>Odenville Intermediate</option>
<option>Odenville Middle</option>
<option>Ragland High</option>
<option>Reuben Yancey </option>
<option>Springville Elementary</option>
<option>Springville Middle</option>
<option>Springville High</option>
<option>St Clair County High</option>
</select>
</p>
<p>
<input type=“submit” value=“Submit” name=“submit”>

Ok, and the code from password-form-thanks.php?

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.&lt;br /&gt;&lt;br /&gt;";
    echo $error."&lt;br /&gt;&lt;br /&gt;";
    echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
    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.

";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
 
$email_message .= "Parent: ".clean_string($pname)."\

";
$email_message .= "Student: “.clean_string($cname).”
";
$email_message .= "Email: “.clean_string($email).”
";
$email_message .= "Telephone: “.clean_string($telephone).”
";
$email_message .= "School: “.clean_string($school).”
";

// create email headers
$headers = 'From: '.$email."\r
“.
'Reply-To: '.$email.”\r
" .
‘X-Mailer: PHP/’ . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here –>

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>


  1. A-Za-z .'- ↩︎

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.

<select name=“school”>
<option name=“School One”>Ashville Elementary</option>
<option name=“School Two”>Ashville Middle</option>
</select>

Using the form handler above I used this PHP code to try to send the information.

switch ($school) {
case “School One”:
$email_to = “school1@domain.com”;
break;
case “School Two”:
$email_to = “school2@domain.net”;
break;
}

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 :slight_smile:



// 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]