0

attempting to implement PHP post back to my form. I'm unsure on how to do this properly and any pointers would be greatly appreciated as to remove errors. As you can tell, I'm an amateur! I understand that the line if (isset($_REQUEST["submit-button"])) needs to be implemented, where exactly, I've been directed to the top. Any recommendations or answers to make my code better is greatly appreciated. Thank you in advance!

<?php
if ( isset($_POST["submit-button"]) && $_POST["submit-button"]=="submit") {
  $fname    = preg_replace("/[^a-zA-Z]/",              "", $_POST['firstname']);
  $email    = preg_replace("/[^a-zA-Z0-9\/:@\.\+-s]/", "", $_POST['email']);
  $address  = preg_replace("/[^a-zA-Z0-9 \n\r#.]/",    "", $_POST['postaddr']);
  $sport    = preg_replace("/[^a-zA-Z0-9]/",           "", $_POST['favsport']);
  $elist    = preg_replace("/[^a-zA-Z0-9\/:@\.\,+-s]/","", $_POST['emaillist']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Week 7 Exercise 3 Form</title>
  <style>
  :root{
    --width: 600px;
    --label: 200px;
  }
  *{
    font-size:16px;
    font-family:Arial,sans-serif;
  }
  h1  { 
    color: red;
    font-size: 2.4em;
  }
  p {
    /* display: table-row; */
    font-size: 1em; 
    color: blue;
    width: VAR(--width);
    border: 1px solid #eee;
    background-color: #eee;
    padding: 3px;
  }
  label {
    vertical-align: top;
    display: inline-block;
    padding: 3px;
    width: VAR(--label);
  }
  select,
  input,
  textarea {
    position: relative;
    width: CALC(VAR(--width) - VAR(--label) - 20px);
    border: 1px solid #eee;
    padding: 3px;
    right: 0px;
  }
  select{
    width: 8em;
  }
  input#list{
    width: 1em;
  }
  input#submit{
    position: relative;
    /* display: inline-block; */
    width: 5em;
    margin-left:auto;
    margin-right:0;
    border: 1px solid #000;
  }
  </style>  
</head>

<body>

<h1>Week 7 Exercise 3 PHP form demo</h1>

<form id="userinfo" action="<?=htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="application/x-www-form-urlencoded">
  <p>
    Please fill in the following form. All fields are mandatory.
  </p>

  <p>
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="firstname" value=<?=(($fname)?$fname:"")?>> 
  </p>

  <p>
    <label for="email">Email Address:</label>
    <input type="text" id="email" name="email" value=<?=(($email)?$email:"")?>>
  </p>

  <p>
    <label for="addr">Postal Address:</label>
    <textarea id="addr" name="postaddr"><?=(($address)?$address:"")?></textarea>
  </p>

  <p>
    <label for="sport">Favourite sport: </label>
    <select id="sport" name="favsport[]" size="4" multiple="multiple">
      <option value="soccer" <?=((in_array("soccer",$sport))?'selected':'')?>>Soccer</option>
      <option value="cricket" <?=((in_array("cricket",$sport))?'selected':'')?>>Cricket</option>
      <option value="squash" <?=((in_array("squash",$sport))?'selected':'')?>>Squash</option>
      <option value="golf" <?=((in_array("golf",$sport))?'selected':'')?>>Golf</option>
      <option value="tennis" <?=((in_array("tennis",$sport))?'selected':'')?>>Tennis</option>
      <option value="basketball" <?=((in_array("basketball",$sport))?'selected':'')?>>Basketball</option>
      <option value="baseball" <?=((in_array("baseball",$sport))?'selected':'')?>>Baseball</option>
    </select>
  </p>

  <p>
    <label for="list">Add me to the mailing list</label>
    <input type="checkbox" id="list" name="emaillist" value="Yes" <?=(($elist)?' checked ':"")?>>
  </p>

  <p>
    <input type="submit" id="submit" value="submit" name="submit-button">
  </p>
</form>

<section id="output">
  <h2>The following information was received from the form:</h2>
  <p><strong>First Name:</strong> <?=$fname;?></p>
  <p><strong>Email Address:</strong> <?=$email;?></p>
  <p><strong>Postal Address:</strong> <?=$address;?></p>
  <p><strong>Favourite sport = </strong> <?=ucwords(implode(", ",$sport));?></p>
  <p><strong>Email list = </strong> <?=$elist;?></p>
</section>
</body>
</html>

2 Answers2

0

Keeping your existing HTML the same, you can modify the PHP snippet like so in order to get your posted data:

<?php

if ( $_POST ) {

    $fname = $_POST['firstname'];
    $email = $_POST['email'];
    $address = $_POST['postaddr'];
    $sport = $_POST['favsport'];
    $elist = $_POST['emaillist'];

}

?>

This simply checks to see if anything has been posted to the page, ie. the $_POST array is not empty.

DannyXCII
  • 888
  • 4
  • 12
0

Write your code like below:

<?php

$fname=""; $email=""; $address=""; $sport=""; $elist="";
if (isset($_POST['submit-button'])) {
    $fname = $_POST['firstname'];
    $email = $_POST['email'];
    $address = $_POST['postaddr'];
    $sport = $_POST['favsport'];
    $elist = $_POST['emaillist'];
}

?>
Arash Younesi
  • 1,671
  • 1
  • 14
  • 23