Banjerr
Banjerr

Reputation: 121

Where should I put PHP code for a HTML form?

I can't seem to find an answer to this particular question, so I apologize if it's already been answered and I have yet to see it.

I have a HTML form that takes visitors name, e-mail, and a message; styled with CSS of course. I have the PHP code prepared, but I'm not exactly sure where to place it. Currently I have it on it's own page and the html is pointing to it with "action", but when I test the page and submit the form it just goes to a blank page. Here's a sample of my HTML code.. and PHP code... The page can be seen here... http://wayhigh.we.bs/contact.html

<form method="post" action="index.php" class="form" id="form1">

  <p class="name">
    <input name="name" type="text"            class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name"  id="name" />
  </p>

  <p class="email">
    <input name="email" type="text" class="validate[required,custom[email]] feedback- input" id="email" placeholder="Email" />
  </p>

  <p class="text">
    <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
  </p>


  <div class="submit">
    <input type="submit" value="SEND" id="button-blue"/>
    <div class="ease"></div>
  </div>
</form>

and PHP...

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
$from = 'From: J. Michaels'; 
$to = 'mailto:[email protected]'; 
$subject = 'Hello from a visitor';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
/* Anything that goes in here is only performed if the form is submitted */
}

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

Upvotes: 0

Views: 3666

Answers (2)

lozadaOmr
lozadaOmr

Reputation: 2655

Note that I added the isset in if (isset($_POST['submit']))

PHP code before the HTML form

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
$from = 'From: J. Michaels'; 
$to = 'mailto:[email protected]'; 
$subject = 'Hello from a visitor';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if (isset($_POST['submit'])) {
/* Anything that goes in here is only performed if the form is submitted */
}

if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

<form method="post" action="index.php" class="form" id="form1">

  <p class="name">
    <input name="name" type="text"            class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name"  id="name" />
  </p>

  <p class="email">
    <input name="email" type="text" class="validate[required,custom[email]] feedback- input" id="email" placeholder="Email" />
  </p>

  <p class="text">
    <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
  </p>


  <div class="submit">
    <input type="submit" value="SEND" id="button-blue"  name="submit" />
    <div class="ease"></div>
  </div>
</form>

Upvotes: 0

Barmar
Barmar

Reputation: 781131

You need to add name="submit" to the submit button:

<input type="submit" name="submit" value="SEND" id="button-blue"/>

Otherwise, if ($_POST['submit']) won't succeed.

Upvotes: 1

Related Questions