heav3n
heav3n

Reputation: 77

Form Select on submit run a function

    <form action="" method="post">  
    <select name="answer" onchange="this.form.submit();" >
        <option>Choose Answer</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    </form>

I want to cal a function after user chooses 1 answer. Function gets input the selected value. for example answered($option_here)

Upvotes: 0

Views: 413

Answers (3)

Will
Will

Reputation: 20235

Add the function call to the onchange attribute.

onchange="answered(this.value);this.form.submit();"

Edit:

Sorry, misunderstood your question. I though answered was a JavaScript function.

You have POST as your form method, so when it is submited you have to use $_POST to get the data, not $_GET. Try this instead:

answered( $userid, $answerid, $_POST["answer"] );

Upvotes: 1

Anthony
Anthony

Reputation: 3218

Try to do this one:

<script>
    function doSmTh(ths)
    {
        /*some actions*/

        document.forms['formName'].submit();
    }
</script>

     <form action="" id="formName" name="formName" method="post">  
        <select name="answer" onchange="doSmTh(this)" >
            <option>Choose Answer</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
     </form>

Upvotes: 0

Svish
Svish

Reputation: 158121

If you submit the form when the user selects anything, which it looks like you do, you can just handle it like you would any other form being submitted.

If you want to do it without reloading the page you have to use Javascript and AJAX. And if so, you might find some pointers in this sample code: http://samples.geekality.net/tail. This posts some data using jQuery Ajax to a php file which then calls a function and returns some result.

Upvotes: 0

Related Questions