title: validating a HTML form using jquery with php?
body: <p>I have a simple HTML file with a form.</p>
  
  <pre><code>&lt;form action="register.php" method="get" id="submitform"&gt;
      First Name:
              &lt;input type="text" class="fb-input" name="fname" id="firstname" /&gt;
              &lt;br&gt;
      &lt;div class="fb-error"&gt;
      &lt;/div&gt;
  
      &lt;input type="button" name="signupBtn" id ="sub" value="Sign upw"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>When I press the submit button, this will be checked at the jquery if the field is empty.</p>
  
  <pre><code>$(document).ready(function(){
      $("#sub").click(function(){
         register();
      });
  });
  
  function register(){
      var firstname=$("#firstname").val();
  
      if(firstname==""){
          $(".fb-error").show().html("You must fill the fields.");
  
      }else{
          $(".fb-error").hide();
          $("#submitform").submit();
      }
  }
  </code></pre>
  
  <p>Then after being checked, it will be submitted to the php file to further check the characters inputted in the form, then will it will echo if an invalid character was found.</p>
  
  <p>What I want is, the checking that i am doing after page submission in the php file will be transferred inside the jquery. Example:</p>
  
  <pre><code>function register(){
      var firstname=$("#firstname").val();
  
      if(...){
           &lt;?php
              if(preg_match(....)){....}
           ?&gt;
      }else{
          .....
      }
  }
  </code></pre>
  
  <p>I tried this and it didn't work. I guess i cant put php inside the jquery. The reason i want to put the php inside the jquery is for me to be able to show an error message in the div part of the HTML because once I submit the page, It goes to another blank page where the error message of the php shows up. </p>
  
  <p>What im doing is like the regisration of facebook that once you submit it and you input wrong characters, an error message will show up at the bottom. We are required to use php, jquery is just optional.</p>
  
  <p>Could anyone help me with this? Or could suggest a better way to do this? </p>
  
  <hr>
  
  <p>Updated:</p>
  
  <p>Html code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Facebook Registration&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="register.php" method="get" id="submitform"&gt;
      First Name:
  
      &lt;?php $fname = isset($_POST['fname']) ? $_POST['fname'] : ''; ?&gt;
  
      &lt;input type="text" class="fb-input" name="fname" value="&lt;?php echo $fname;     ?&gt;"&gt;
          &lt;br&gt;
  
      &lt;div class="fb-error"&gt;
      &lt;/div&gt;
  
      &lt;input type="submit" name="signupBtn" id ="sub" value="Sign upw"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>Php file:</p>
  
  <pre><code>&lt;?php
  $fname=$_GET['fname'];
  
  if((preg_match("/^[a-zA-z]{1,}$/",$fname))){
      echo "registered";
  }
  else{
      var_dump($_POST);
      echo "&lt;form id='retForm' method='post' action='index.php'&gt;";
      echo "&lt;input type='hidden' name='fname' value='$_POST[fname]'&gt;";
      echo "&lt;script&gt;$(document).ready(function() { $('#retForm').submit() });&lt;/script&gt;";
  }
  
  ?&gt;
  </code></pre>