Mahdi Abdi
Mahdi Abdi

Reputation: 692

PHP Script doesn't work on my form

I have a HTML form, and a php script for saving the elements into a .txt file. Here's my form:

<div class="col-md-6">
    <h2>Send us a message</h2>
    <form id="contact-form" action="myscript.php" method="POST">
        <div class="row">
            <div class="col-md-6">
                <input name="name" id="name" type="text" placeholder="Name" />
                <input name="email" id="email" type="text" placeholder="E-Mail" />
                <input name="subject" id="subject" type="text" placeholder="Subject" />
            </div>

            <div class="col-md-6">
                <textarea name="comment" id="comment" placeholder="Message"></textarea>
                <input type="submit" id="submit_contact" value="Send message" />
                <div id="msg" class="message"></div>
            </div>
        </div>
    </form>                     
</div>

And my php script:

<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comment'])) {
    $data ='Name : ' . $_POST['name'] . "\n" . 'E-Mail : ' . $_POST['email'] . "\n" . 'Website : ' . $_POST['subject'] . "\n" . 'Comment : ' . $_POST['comment'] . "\n" . '----------------------------------------------------------------' . "\n";
    $ret = file_put_contents('comments.txt', $data, FILE_APPEND);
    if($ret === false) {
        echo "<script>alert('Failure!');</script>";
    }
    else {
        echo "<script>alert('Success!');</script>";
    }
    }
    else {
      echo "<script>alert('Fill in The Form Please !');</script>";
    }

When I fill the form and press submit, nothing happens actually. But when I remove the DIV tags it works.

Here are my full codes: My HTML code and my CSS code

Upvotes: 0

Views: 1780

Answers (2)

Ryan Vincent
Ryan Vincent

Reputation: 4513

Your code and the HTML as posted. Included your css.

You have an extra brace in the script.

This code shows the form and creates the 'comments.txt' file.

Added test to ensure all the fields are entered

PHP 5.3.18 on windows XP.

<?php

if (isset($_POST['myscript'])) { // the form was submitted...


    if(   isset($_POST['name'])    && !empty($_POST['name'])
       && isset($_POST['email'])   && !empty($_POST['email'])
       && isset($_POST['subject']) && !empty($_POST['subject'])
       && isset($_POST['comment']) && !empty($_POST['comment'])) {

        $data ='Name : ' . $_POST['name'] . "\n" . 'E-Mail : ' . $_POST['email'] . "\n" . 'Website : '
                . $_POST['subject'] . "\n" . 'Comment : ' . $_POST['comment'] . "\n"
                . '----------------------------------------------------------------' . "\n";
        $ret = file_put_contents('comments.txt', $data, FILE_APPEND);
        if($ret === false) {
            echo "<script>alert('Failure!');</script>";
        }
        else {
            echo "<script>alert('Success!');</script>";
        }
    }
    else {
      echo "<script>alert('Fill in The Form Please !');</script>";
    }
}
?>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Q22077564</title>
    <LINK REL=StyleSheet HREF="Q22077564.css" TYPE="text/css" MEDIA=screen>
  </head>

<body>
<div class="col-md-6">
    <h2>Send us a message</h2>
    <form id="contact-form" action="myscript.php" method="POST">
      <!-- add hidden field so that we know the form came in! -->
      <input type="hidden" name="myscript"  value="myscript" />;
      <div class="row">
            <div class="col-md-6">
                <input name="name" id="name" type="text" placeholder="Name" />
                <input name="email" id="email" type="text" placeholder="E-Mail" />
                <input name="subject" id="subject" type="text" placeholder="Subject" />
            </div>

            <div class="col-md-6">
                <textarea name="comment" id="comment" placeholder="Message"></textarea>
                <input type="submit" id="submit_contact" value="Send message" />
                <div id="msg" class="message"></div>
            </div>
        </div>
    </form>
</div>
</body>
</html>

Upvotes: 2

user2783998
user2783998

Reputation: 68

<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comment'])) {
$data ='Name : ' . $_POST['name'] . "\n" . 'E-Mail : ' . $_POST['email'] . "\n" . 'Website : ' . $_POST['subject'] . "\n" . 'Comment : ' . $_POST['comment'] . "\n" . '----------------------------------------------------------------' . "\n";
$ret = file_put_contents('comments.txt', $data, FILE_APPEND);
if($ret === false) {
    echo "<script>alert('Failure!');</script>";
}
else {
    echo "<script>alert('Success!');</script>";
}
}
else {
  echo "<script>alert('Fill in The Form Please !');</script>";
} **}**//not your answer but you should remove this one right

Upvotes: 0

Related Questions