0

Let's say I have a simple form as follows that has only a text field and a submit button.

<form id="dataForm" name="dataForm" method="post">      

  <?php
    if(isset($_POST['btnSubmit']))
    {
        if(isset($_POST['txtName'])&&!is_null($_POST['txtName'])&&$_POST['txtName']!="")    
        {
            header("location:Home.php");    
        }
        else
        {
            echo "<font style='color:red'>Name is mandatory.</font>";   
        }
    }
 ?>

    <input type="text" id="txtName" name="txtName" /><br/>
    <input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />        
</form>

The page is redirected to Home.php, if some text is inputted in the given textfield txtName and the submit button is clicked. An error message is displayed, if the text field is left blank and the submit button is clicked.

But I don't want to redirect the page using header("lcation:Home.php"); (I don't want to use the GET method at all in this case. The method must be POST anyway). I just want to redirect using the form's action attribute but only after checking the (server-side) validation to see, if the text field is left blank. It should only be redirected, if it fulfills the given server-side validation. Is this possible?

<form id="dataForm" name="dataForm" method="post" action="Home.php">

    <input type="text" id="txtName" name="txtName" /><br/>
    <input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />        

</form>
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • No, it's not possible. you have to use jQuery or AJAX to validate data if you want to use form action attribute – GBD Oct 06 '12 at 18:07
  • I don't understand what do you mean by `GET` method here. You are submitting all the values to the server using `POST` already. What you can do is, maybe, do the validation in home.php and redirect back from home in case the validation fails ? – Madhur Oct 06 '12 at 18:10
  • You can have the server side validation in the target page and if it doesnt vslidate you send the user back and repopulate the form, but of course you will then need to make a redirection... – jtheman Oct 06 '12 at 18:11
  • I was going to do that and I have already Ajax/jQuery validations. I just wanted to know, if it's possible someway that might not have been known to me. Thank you. – Tiny Oct 06 '12 at 18:14

0 Answers0