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
https://forums.phpfreaks.com/topic/32835-why-does-this-work/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.