[PHP]Validate form on same page?

Status
Not open for further replies.

Adil

DevBest CEO
May 28, 2011
1,278
716
Probably gonna sound nooby, but here goes >.<
What I need to do is validate a form, without a form action leading to another page. I've done this with JQuery, but I need stronger server-side validation as well. Any ideas?
HTML:
<form  id="regform" action=""  method="post">
      <label for="username">Username:</label><br/>
    <input type="text" name="username" id="username"  class="required"/><br/>
      <label for="password">Password:</label><br/>
      <input type="password" id="password" class="required"/><br/>
     <label for="email">Email:</label><br/>
     <input type="email" id="email" ></input><br/>
 
     <br/>
 
<input type="submit" value="Submit" class="regsub" onclick="validate();"/><input type="reset" value="Reset" class="regres"></input>
</form>
Here's my form, help is appreciated!
~Adil
 

Alam

shietttt
Jul 3, 2011
433
166
You can also use <form action=""> If you wan't the form to redirect to a php file , or some other text but you can also use the OnClick method.

If you wan't the action to occur on same page , you may want to use an isset if(isset($_POST['submit'])) {

}
 

Adil

DevBest CEO
May 28, 2011
1,278
716
You can also use <form action=""> If you wan't the form to redirect to a php file , or some other text but you can also use the OnClick method.

If you wan't the action to occur on same page , you may want to use an isset if(isset($_POST['submit'])) {

}
PHP:
   <?php 
                if(isset($_POST['password'])){
     
         echo "lol";    
     
                }?>
I tried with submit, didn't work.
Any help? :confused:
 

Alam

shietttt
Jul 3, 2011
433
166
Just use , this if you wan't ur data to be displayed on the same page of form.

Code:
<a href="javascript: submitform()"></a>
</form>
<script type="text/javascript">
function submitform()
{
  document.adil.submit();
}
</script>
 

Adil

DevBest CEO
May 28, 2011
1,278
716
Code:
$(document).ready(function(){
    $("#regform").validate();
 
  });
Client-side form validation, I need server-side.
 

Adil

DevBest CEO
May 28, 2011
1,278
716
Just a validation script that does:
  • Checks for data in the forms
  • preg_match on data
  • Queries database with data
  • Directs to portal.php
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
You can't do this server-side without refreshing, since when it validates it should send a request first. So you would need to use either JavaScript (jQuery) or Ajax.

Since you requested it, I just made a really quick submit page.

What this does is validating fields in the form. If these are password, then locate to Google's website.

PHP:
<?php
 
    if (isset($_POST['submit']))
    {
        if (!empty($_POST['username']) && strlen($_POST['username']) >= 5 && strlen($_POST['username']) <= 9)
        {
            if (!empty($_POST['password']))
            {
                // All fields was following the rules
                // Location: <your url or path>
                header("Location: http://google.com");
                exit;
            } else {
                echo 'Please type in a password.';
            }
           
        } else {
            echo 'Username must be larger than 5 and less than 10 characters.';
        }
       
    }
 
?>
 
<form action="#" method="POST">
    <input type="text" name="username" />
    <input type="text" name="password" />
 
    <input type="submit" name="submit" />
</form>

Note that this has absolutely no security at all. This is really basic and was only to show how you COULD do it. I do not recommend using this for your website as is.
 
Status
Not open for further replies.

Users who are viewing this thread

Top