1

I want to add an input to a form and cannot make it work. When loading the page, clicking "submit" and looping through the $_POST array, no input was added to the form.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
    $('#myOrder').append('<input type="text" name="test" value="product">');
    </script>
    <form id="myOrder" action="" method="post">
        <input type="submit" value="Submit">
    </form>
    <?php
    if ($_POST)
        {
        foreach($_POST as $key => $value)
            {
            echo $key . "<br>";
            echo $value;
            }
        }
    ?>
</body>
</html>

Help is highly appriciated!

4
  • where do you call the script? Commented Mar 15, 2015 at 17:19
  • Did you include JQuery? Commented Mar 15, 2015 at 21:39
  • @Jon Mark Perry. I put the append() in a function on an external file and call the function with addEventListener on load. Commented Mar 15, 2015 at 22:04
  • @Vucko. Not sure what you're on about. Commented Mar 15, 2015 at 22:05

2 Answers 2

1

Try to put your code inside $(document).ready():

$(document).ready(function(){
    $('#myOrder').append('<input type="text" name="test" value="product">');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Hamed. Shokran. Sorry for the late reply, I was grabbing a bite. I did not think append() had to be called as it was inside the body of the document.
You are welcome. It's better to put my code inside <head></head> section.
0

Hmm, You can try make inputs as array like this;

$('#myOrder').append('<input type="text" name="input[]" value="product">');

GL

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.