A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: submit forms

  1. #1
    Junior Member
    Join Date
    Jan 2002
    Posts
    11

    submit forms

    can someone tell me how sumbit buttons work? I dont know how that works because when I look at html codes with forms it tells me nothing of sending it to an email address. For instance if I want someone to sign-up for a newsletter where is that information sent...please someone help me out, I always write on this thing and get no replies 98% of the time.
    Fariida Yasin

    http://www.syn3.org

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    For dealing with things like newsletter sign-ups you really need a server side script to handle things.

    The form in your HTML collects the information, then clicking submit gets the browser to send the information entered into the form to your script. The script can then do what it wants - save the information, send you and email or whatever.

    Here's a very quick example (without any real error checking or anything) that would create a form where a user entered their name and email address and when this is submitted the information is sent to the site admin - first some HTML:

    Code:
    <form method="post" action="send.php">
      <fieldset>
        <label for="name">Name:</label> <input type="text" name="name" id="name" value="">
        <label for="email">Email: </label> <input type="text" name="email" id="email" value=""> 
        <input type="submit" value="Send">
      </fieldset>
    </form>
    The <form method="post" action="send.php"> line is the most important one, it tells the browser that when the form is submitted it should send the content of the form (using the HTTP POST method) to a file called send.php - the form also contains a couple of input fields where the user can type in their name and email address. When the form is submitted what is entered here will be what gets sent to send.php

    So now onto send.php (assuming you're running on a server that supports PHP)

    Code:
    <?php
    
    $name = !empty($_POST['name']) ? $_POST['name'] : 'Name not entered';
    $email = !empty($_POST['email']) ? $_POST['email'] : 'Email not entered';
    
    $message = 'Hello! ' . $name . ' (email address: ' . $email . ') says hi!';
    
    mail('admin@myserver.com', 'Message from the website', $message);
    
    echo '<p>Thanks for submitting the form!</p>';
    
    ?>
    The <?php and ?> are tags that tell the server that the content contained between them is PHP code and needs to be processed - rather than plain old HTML that is sent directly to the browser.

    Remember the form was setup to send the content of the form using POST? well PHP takes all this POST content and stores it in $_POST. If you know the names of the variables that were sent (name and email - the name attributes from the form input fields) you can get the information that was sent to the script. The lines,

    Code:
    $name = !empty($_POST['name']) ? $_POST['name'] : 'Name not entered';
    $email = !empty($_POST['email']) ? $_POST['email'] : 'Email not entered';
    do this. First they check that $_POST['name'] and $_POST['email'] have a value - that is they are !empty. If there is a value supplied, then that value is used. Otherwise a message saying the user didn't supply that information is set.

    Right, now we have the information we need to do something with it. The line,

    Code:
    $message = 'Hello! ' . $name . ' (email address: ' . $email . ') says hi!';
    joins it all together to create a cheery greeting. But a greeting isn't much use if no one sees it, so

    Code:
    mail('admin@myserver.com', 'Message from the website', $message);
    sends the greeting (stored in $message) as an email to admin@myserver.com with the subject line message from the website.

    Finally a message is written back to the browser thanking the user.
    Last edited by catbert303; 01-17-2007 at 05:05 AM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center