PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

Martina Carrington
Martina Carrington
15,754 Points

Using A Third-Party Library

I was following the steps on the video but when i preview it i got a couple of error message on suggest.php .

<?php if($_SERVER["REQUEST_METHOD"] == "POST"){ $name = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_STRING)); $email = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_EMAIL)); $details = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_CHARS));

if ($name == ""|| $email == "" || $details == ""){ echo "Please fill in the required field: Name, Email and Details"; exit; } require("inc/phpmailer/class.phpmailer.php");

echo "<pre>"; $email_body = ""; $email_body .= "Name " . $name . "\n"; $email_body .= "Email " . $email . "\n"; $email_body .= "Details " . $details . "\n"; echo $email_body; echo "</pre>";

//To Do: Send Email header("location:suggest.php?status=thanks"); }

$pageTitle = "Suggest a Media Item"; $section = "suggest";

include("inc/header.php"); ?>

<div class="section page"> <div class="wrapper"> <h1>Suggest a Media Item</h1> <?php if (isset($_GET["status"]) && $_GET['status'] == "thanks") { echo "<p>Thanks for the email! I’ll check out your suggestion shortly!</p>"; } else { ?> <p>If you think there is something I’m missing, let me know! Complete the form to send me an email.</p> <form method="post" action="suggest.php"> <table> <tr> <th><label for="name">Name</label></th> <td><input type="text" id="name" name="name" /></td> </tr> <tr> <th><label for="email">Email</label></th> <td><input type="text" id="email" name="email" /></td> </tr> <tr> <th><label for="name">Suggest Item Details</label></th> <td><textarea name="details" id="details"></textarea></td> </tr> </table> <input type="submit" value="Send" /> </form> <?php } ?> </div> </div>

<?php include("inc/footer.php"); ?>

Simon Coates
Simon Coates
28,678 Points

People are going to have an easier time reading your code if you use the syntax for displaying code. For treehouse, that seems to be the use of three backticks (on the key with the tilde on most keyboards) before and after code. On the line with the opening backticks, you can also follow these immediately with the language you are using, so that highlighting is applied. some operations (such as posting a comment) typically display with a link to a Markdown Cheatsheet that explains the syntax.

5 Answers

Simon Coates
Simon Coates
28,678 Points
<input type="text" id="email" name="email" />

means that the value will show up as $_POST['email']. However,

$email = trim(filterinput(INPUT_POST,"name",Filter_SANITIZE_EMAIL));

looks for the value on $POST['name']. The first parameter for filterinput indicates whether you are using POST or GET, while the second indicates where to find the value you want. So you should probably use

$email = trim(filterinput(INPUT_POST,"email",Filter_SANITIZE_EMAIL));

which tries to get the value for $_POST['email']

Martina Carrington
Martina Carrington
15,754 Points

Thanks Simon, :D Simon , how did you put the code in that format

Simon Coates
Simon Coates
28,678 Points

use three backticks ` (on the key with the ~) before the code and after the code. try it out.

Simon Coates
Simon Coates
28,678 Points

for starters, there's a problem with your use of filter_input. You've fed in 'name' on every variable.

Martina Carrington
Martina Carrington
15,754 Points

Hey Simon Coates , i don't know how to put the codes that i posted in a different format

Martina Carrington
Martina Carrington
15,754 Points
if($_SERVER["REQUEST_METHOD"] == "POST"){
  $name = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_STRING));
  $email = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_EMAIL));
  $details = trim(filter_input(INPUT_POST,"name",Filter_SANITIZE_CHARS));

  if ($name == ""|| $email == "" || $details == ""){
    echo "Please fill in the required field: Name, Email and Details";
    exit;
  }
  require("inc/phpmailer/class.phpmailer.php");

  echo "<pre>";
  $email_body = "";
Simon Coates
Simon Coates
28,678 Points

At the section that says, "add an answer" can you see a link that says 'Markdown cheatsheet'?

Simon Coates
Simon Coates
28,678 Points

that link should cover the basic syntax. I'd also note that php displays best if you include the opening <?php.

<?php
//this is a comment
echo "this is some php code";

should include some syntax highlighting.

coskun olcucu
coskun olcucu
5,340 Points
$pageTitle = "Suggest a Media Item";
$section = "suggest";