3

I have a form written in HTML and a PHP file that should be able to generate the input into a textfile in the same dir. I tested it out before with a different simple HTML file and it worked, now I'm trying to apply it to a bigger HTML file and somehow it wont work and I cant find whats wrong with it.

feedback.html:

<html>
<head>
        <script>
        function myFunction()
        {
            alert("Your feedback has been sent! Thank you");
        }   
        </script>
</head>
<body>
<div class="Linksediv">
    <h3>Feedback</h3>
    <p>Please enter your name and give us some feedback</p>
        <form action="form1.php" method="post">
            Name<input id="Email" size="30" type="text" name="field1"/><br/>
                <textarea id="pass" type="text" name="field2" rows="4" cols="24">Please give us some feedback
                    </textarea>
             <br/>
                <input type="button" name="submit" value="submit" id="buttonOk" onclick="myFunction()" value="Show alert box" />
        </form>

</div>
</body>
</html>

Form1.php

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {                    //Check if fields are there in HTML file
    $data = $_POST['field1'] . ' - ' . $_POST['field2'] . "\n";             //output
    $ret = file_put_contents('data1.txt', $data, FILE_APPEND | LOCK_EX);    //file dir for data1.txt
    if($ret === false) {                                                    //check if anything was written down
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file"."\n";
    }
}
?>

If I press submit on my form then I'll only get a popup with the alert, and the textfile wont be written

4
  • 2
    Have you checked your error log? What errors do you get? What steps have you taken to troubleshoot this? Commented Feb 21, 2013 at 14:33
  • Does Form1.php echo anything? Commented Feb 21, 2013 at 14:35
  • there is nothing in my error log, and as I said before I tested this out on a smaller, simpler form and then it worked, now it wont Commented Feb 21, 2013 at 14:35
  • @jimmy form1.php has a link towards the textfile where the input should go, on my previous example it worked, but now it wont even write anything in the textfile Commented Feb 21, 2013 at 14:37

2 Answers 2

4

The following line

<input type="button" name="submit" value="submit" id="buttonOk" onclick="myFunction()" value="Show alert box" />

Creates an input type="button", not type="submit", so the form is not actually submitted.

Also, consider presenting the success message after the post and not on click, just echo it on the form1.php script when the file has been written successfully

Sign up to request clarification or add additional context in comments.

Comments

1

You may also need to add a "return true" statement to your onclick handler after you change the input type from button to submit. Returning false prevents the submit button from executing its default action, which is to submit the form. The behavior may be the same if you don't have a return value.

function myFunction()
{
   alert("Your feedback has been sent! Thank you");
   return true;
} 

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.