Jump to content

Why does this work?


11Tami

Recommended Posts

Hello, this cool code someone gave me. If you save this in notepad and change the email under "uncomment the following line" to your own email address. Then enter items into the fields and send. For some reason it automatically inserts your own email account into the subject field. Can anyone tell me how this cool form code does this? I need to know how it works this way so I can modify accordingly. Thanks a lot.

<?php
  require_once 'thisform.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Forms</title>
</head>
<body>
<?php
  if (isset($_POST['Submit']) &&
      isset($_POST['fieldname']) &&
      trim($_POST['fieldname']) != '' &&
      $_POST['groupname'] != '') {
    echo '<h1>Thank you for filling out this form!</h1>';
    $text = '';
    foreach ($_POST as $name => $value) {
      if (is_array($value)) {
        $text .= sprintf("%s: %s\n", $name, join(' ', $value));
      } else {
        $text .= sprintf("%s: %s\n", $name, $value);
      }
    }
// UNCOMMENT THE FOLLOWING LINE TO USE ON YOUR SITE
  mail('youremailaddress', 'Form data', $text);
  } else {
?>
  <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);  ?>">
    <input type="text" name="fieldname"
      value="<?php
  echo (isset($_POST['fieldname'])) ? htmlspecialchars($_POST['fieldname']) : '';
      ?>" /><br />
    <input type="radio" name="groupname" value="php3" <?php
  if (isset($_POST['groupname']) && $_POST['groupname'] == 'php3') {
    echo 'checked="checked" ';
  }
    ?>/>PHP 3
    <input type="radio" name="groupname" value="php4" <?php
  if (isset($_POST['groupname']) && $_POST['groupname'] == 'php4') {
    echo 'checked="checked" ';
  }
    ?>/>PHP 4
    <input type="radio" name="groupname" value="php5" <?php
  if (isset($_POST['groupname']) && $_POST['groupname'] == 'php5') {
    echo 'checked="checked" ';
  }
    ?>/>PHP 5<br />
    <input type="submit" name="Submit" />
  </form>
<?php
  }
?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.