0

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

27
  • 2
    Which <div> tags are you referring to? Commented Feb 27, 2014 at 18:47
  • So it is not about the PHP script but your HTML or maybe some JS? Commented Feb 27, 2014 at 18:48
  • @relentless , i mean all of them . i removed all of them and tried again and it worked fine. Commented Feb 27, 2014 at 18:50
  • Do you have more HTML around it that you are not showing? Sounds a bit odd that a few HTML tags stop your form from submitting. Commented Feb 27, 2014 at 18:51
  • 1
    @dg988 he has a submit button, it should submit if he presses enter while any input control has focus. Commented Feb 27, 2014 at 19:18

2 Answers 2

2
+50

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot . it works but when i run it , an alert shows "success" . and it mustn't show that at the first place. what should i do ?
Ah! OK give me a few minutes and i will fix it!
@ShaPesar if(isset($_POST['submit'])) { // form has been posted } else { // diplay form, no past has been made yet }
added a hidden field to detect the form being submitted. Used your CSS. The code appends entries into the comment.txt file.
0
<?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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.