Search code examples
phpformsdrop-down-menutextboxsubmit-button

Check if form is filled before submitting


I have a form called choose_dates.php that submits to a file called process.php. The form consists of a textbox, a dropdown list and a submit button. I have set it up so that you can submit either one value, or the other, or both at the same time. I would like to have it such that if the user has put a value in the textbox AND the dropdown list, then a prompt will ask if that is what he/she really wants to do. The code below doesn't seem to do that when the submit button is pressed. The rest of my code (that I have not placed on here) works fine, this is more of a user interface issue.

<form name="dates" action="process.php" method="POST">
<input type="text" name="submitDate">
<select name="removeException">
<option value="some-value">display dropdown stuff</option>
.
.
</select>
     <input type="submit" value="submit" 
        <?php
        if($_POST['submitDate'] != "" and $_POST['removeException'] != "")
        {
        echo " onclick=\"return confirm('Are you sure you want to submit both values at the same time?')\" ";
        }
        ?>
        tabindex="2">   
</form>

And of course, please ask any questions if what I said isn't clear enough. Regards.


Solution

  • You need to do that on the client side using javascript ( preferably ). The post data will be submitted when the form is submitted. Try adding this function as your form's onsubmit event

    function func(){
        var a = document.getElementsByName('removeException'),
            b = document.getElementsByName('submitDate');
        if(a[0].value!=null && b[0].value!=null){
            var c = confirm('Are you sure you want to submit both values at the same time?')
            if(c){
                return true;
            }else{
                return false;
            }
        }
    }
    

    Then

    <form name="dates" action="process.php" method="POST" onSubmit='return func()'>