e1065273
e1065273

Reputation: 1

Element changed by javascript is not submitting

I created a simple form with a text input and a button.

<form method="POST">
    <input type="text" name="Itm" id="Itm">
    <input type="submit" value="submit">
</form>

Then I replace the text input with a drop-down list by a JavaScript.

<script>
    document.getElementById('Itm').outerHTML="<select id='IItm'>";
     x = document.getElementById("IItm");
     var option = document.createElement("option");
     option.text = "Kiwi";
     x.add(option);
</script>

But, now by clicking the submit button the selected value is being not submitted.

Note: I used following code to get and check the values.

<?php 
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        print_r($_POST);
    }
 ?>

Upvotes: 0

Views: 92

Answers (1)

Joshua Rose
Joshua Rose

Reputation: 390

You must set a value for the select. The value is what is passed to in post.

<script>
    document.getElementById('Itm').outerHTML="<select id='IItm'>";
     x = document.getElementById("IItm");
     var option = document.createElement("option");
     option.text = "Kiwi";
     x.value = "Kiwi"
     x.add(option);
</script>

Upvotes: 0

Related Questions