SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: Newsbie Help

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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";

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Do an echo of $school. Does it contain the value you were expecting?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 Code:
    <?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 Code:
    <?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 Code:
    <?php
    include 'schools.php';

    if( isset(
    $_POST['school']) && (int)$_POST['school'] > ){

    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

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido2004, if I do a test using only one if statement it does pass the expected info.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 Code:
    <?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 Code:
    <?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'] > ){

    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 Code:
    <?php
    switch ($school) {
        case 
    "School One":
            
    $email_to "sch1@email.com";
            break;
        case 
    "School Two":
            
    $email_to "sch2@email.com";
            break;
    }
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    +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:

    PHP Code:
    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:

    PHP Code:
    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.
    Martin.

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by martbean View Post
    +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:

    PHP Code:
    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:

    PHP Code:
    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:
    PHP Code:
    if ("Ashville Elementary" $school
    would throw an error.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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">

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 Code:
    <?php

    var_dump
    ($_POST);

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by OBEagle View Post
    To pass along the school info in the drop box should I use $school in my form email script to pass that info along?
    Hmm, what do you mean by that? Do you want to select the choice made by the user?

  13. #13
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido, yes I do.

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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?

  15. #15
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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 Code:
    <?php

    var_dump
    ($_POST);
    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.

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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">

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Ok, and the code from password-form-thanks.php?

  18. #18
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 = "/^[A-Za-z .'-]+$/";
    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.\n\n";

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

    $email_message .= "Parent: ".clean_string($pname)."\n";
    $email_message .= "Student: ".clean_string($cname)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "School: ".clean_string($school)."\n";


    // create email headers
    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" .
    '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
    }
    ?>

  19. #19
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  20. #20
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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

    PHP Code:

    // 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\nThank you!\nGoodbye";

    mail$email_to$subject$contents); 
    This is where you now study PHPs mail() function mail

  21. #21
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused Cups, I have used a mail handler at the end of the script above. It's

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


    I am using the script I posted above only adding Case to provide info to $email_to

    I'm going zonkers on this thing!

  22. #22
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    echo out all of the variables onto the page

    try it without the header

    remove the @ before mail() so that errors are reported

    check your error log file

    prove to yourself that the mail() function works from this script with some concrete values (i.e. send an email to yourself)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •