0

I have the following code in PHP:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <?php $value = $_POST['Field1'] ?>
    <input type="text" id="Field1" name="Field1" value="<?php echo $value ?>">
    <input type="button" value="ButtonValue" onclick="SubmitFunction()">
</form>

And some function in JavaScript:

<script>
    function SubmitFunction(){
        //... do some stuff here
        //But here I need to assign value to Field1:
        document.getElementById("Field1").setAttribute("value", "Some value");
        document.form.submit();
    }
</script>

When this code loads for the first time, it says there is undefined index Field1. It's absolutely right.

But when I set attribute value to this field using SubmitFunction(), page reloads and it still can't find Field1 ! This is my problem.

I noticed that in pure HTML code the initial form looks like:

<form>
    <input type="text" id="Field1" name="Field1" value>
    <input type="button" value="ButtonValue" onclick="SubmitFunction()">
</form>

Where is my problem (except brain)?

llaabbss
  • 1
  • 2
  • what's your browser i test it in FF26 working – saman khademi Jan 27 '14 at 11:23
  • Mine is Google Chrome 32 – llaabbss Jan 27 '14 at 11:29
  • AAAAHm... sorry I suck. In my last useless answer (i delete it) I told you how to prevent submit. But you nedd to fire it... my bad. However... I have paste your code locally and it works fine. Only one thing to submit the form I've need to change your `document.form.submit();` in `document.forms[0].submit();` but when the page reloads I find "Some value" like value. – Frogmouth Jan 27 '14 at 11:51
  • Yes, it works this way. But I found another issue. setAttribute("value", "Some value") sets value which is obtained with AJAX callback function. It simply returns this value. Nothing more. If in my project I simply submit form without filling this value, it works as expected. But if I set value through setAttribute - is fails. Well, I probably need to check my code and find this misbehaviour. – llaabbss Jan 27 '14 at 12:52

1 Answers1

0
 <script>
        function SubmitFunction(){
            //... do some stuff here
            //But here I need to assign value to Field1:
        document.getElementById("Field1").setAttribute("value", "Some value");
        document.form.submit(document.getElementById("Field1").getAttribute("value"));
        }
    </script>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <?php $value = $_POST['Field1'] ?>
        <input type="text" id="Field1" name="Field1" value="<?php echo $value?$value:''; ?>">
        <input type="button" value="ButtonValue" onclick="SubmitFunction()">
    </form>
  • document.form.submit(document.getElementById("Field1").getAttribute("value")); What if there is a plenty of fields I need to submit? Should I handle them all like this? – llaabbss Jan 27 '14 at 12:03
  • there is a function of jquery named serialize() which will return all element of the form. You have to just use the function and send serialize data to the server – Girish Kumar Sinha Jan 28 '14 at 05:57