3

I have two files, (file1 and file2). file1 includes file2 in a PHP include statement. File1 also contains a form and prints out all $_POST variables. File2 uses a Javascript button to dynamically change the value in an input field. The problem is that $_POST is empty after submit is pressed. Why is that and how do I fix it?

File1:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php include 'file2.php'; ?> 
<input type="submit" /></form>

<?php foreach ($_POST as $key => $val) { echo $key . " belongs to " . $val; } ?>

File2

<script type="text/javascript">
        var button = {
        counter : 0,

            count : function() {
            text = document.getElementById("text");
            this.counter++;
            text.setAttribute("value", this.counter);
        }
    };
    </script>

<button type="button" onclick="button.count()">CLICK ME!</button>
<input id="text" type="text" value="0" />
1
  • 3
    your input requires a name attribute to identify input when it's processed by PHP Commented May 20, 2011 at 14:17

3 Answers 3

5

You forgot to set name attribute:

<input id="text" name="text" type="text" value="0" />
Sign up to request clarification or add additional context in comments.

2 Comments

I feel very stupid for omitting this. I just tested it (my actual code) and adding name parameter solves the issue. Thanks so much.
Enjoy ! If you find any of the answer helpful, remember to accept it with a tick in the left side (I write about it because you have no answers accepted already).
2
text.setAttribute("value", this.counter);

This is better off as:

text.value = this.counter;

Also you need a name attribute on your element:

<input id="text" name="text" type="text" value="0" />

Comments

0

you didnt set the name attribute for the field "text".

Corrected code:

<input id="text" name="text" type="text" value="0" >

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.