1

I have been looking for a way to set up what I thought would be a simple form and redirect using php. I have found similar ideas though none are answered or end up being too different to work for my situation. I am very new to this so excuse my ignorance on the subject. Any help, even a point in the right direction is greatly appreciated.

I want to set up a form with one text box (for them to enter their name) and one drop down box that will have a list of school names. The user should enter their name, choose their school and then click go or submit.

Once done I want the form to do two things. Send an email with the persons name to a predefined email address (will always be the same email address) and then redirect the page depending on the school chosen in the drop down box. Basically, email sent to me with persons name and school 1 would go to www.address1.com, school 2 would go to www.address2.com etc. By the time site is complete I may end up with 30-40 schools though will only end up with 10-12 address destinations as some schools will be redirected to the same site addresses.

Once again thank you for any help that you can offer.

4
  • 1
    Do you want to email only the name of the person without the school selected? Commented Aug 25, 2011 at 19:22
  • @Martin. I did post what I had done as well as mentioned that I did not know how to do this. I also said pointing me in the right direction would be helpful as well. This information will most likely help others in the future as well. Commented Aug 26, 2011 at 12:47
  • @Daniel. It will work without the school name as we can manually add the person name to school selected on site. If form sent school name and persons name that would be great as well. Commented Aug 26, 2011 at 12:52
  • I updated code so that the email include selected school and persons name. Thank you for the code that you provided me with. Commented Aug 26, 2011 at 14:12

2 Answers 2

2

Hope it helps:

<?php

if(isset($_POST['submit']))
{
    mail('[email protected]','Some subject',$_POST['name']);
    switch($_POST['school'])
    {
      case 'school1':
        header('Location: http://school1.com');
        break;
      case 'school2':
        header('Location: http://school2.com');
        break;
    }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="name">Name:</label> <input name="name" type="text" id="name" />
    <label for="school">School:</label>
    <select id="school" name="school">
        <option value="school1">School1</option>
        <option value="school2">School2</option>
    </select>
    <input type="submit" name="submit" value="Submit" />
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

I hope Keith sees the fun of maintaining this with >10 schools :)
It gets heavy, it's true. But this only needs an Apache and a Mail server to run.
Thank you very much Daniel. I appreciate the time and effort. I will let you know how it turns out.
0

First, put all your schools into a database, and build an automated script that makes you the form based on the database input. From what you've written, you need at least an ID, the school's name, and the school's http address.

Sending an email is trivial and works with the mail() function - or with something really fancy like MIMEMail.

Redirecting the browser works with the header('Location: http://schoolname1.com'); - make sure that there is no output before header (thanks to Travis for mentioning that)

5 Comments

Is there a way to do this without using a database? I will be predetermining the school names in the drop down box for them to choose from. Most likely will only have 4-5 to begin with until the program takes off. I can make the dropdown list and have it redirect to an address (seen here www.subwayeasy.com/schools.html) but am unable to add the functionality to send the email after obtaining the information from the input box (not yet on site previously linked to). I do not know how to use databases unfortunately. I am probably not very helpful so I apologize for that.
You could also do this with a flat text file - or something like XML. You should not put the school names within the HTML code, however, as that will proove to be a PITA maintaining later. If you need this finished soon, write me at [email protected]
Just make sure there is no output before header or it will give an error.
@Kieth the DB is probably the best method even now as it will grow. You can also hard code schools into a PHP file into an array using something like $school[] and $school_url[] Not to mention using MySQL is not too hard. You can find how-tos on the internet.
Thank you all very much for the help. I will play around with the code provided and see what I can come up with. I will post back to let you know if I got it to work for others in a similar situation in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.