简体   繁体   中英

how can i call a php function using form submit

i am making a registration form in which php code and html form are in the same page , and i have also have a java script which validate the fields, but if the validation is done, i need to post the data by calling php and submit in database

if(isset($_POST['Name'])){
$order = "INSERT INTO `reg_form`(`Name`) VALUES ('$_POST[Name]')";
$result = mysql_query($order, $db2);
 if (!$result) {
    echo("<br>Input is failed");
 } else {
    echo 'Regestered successfully !!!';
 }
}

<form name="register" id="register" class="b1 f_l" action="register.php"  method="post">
 <div class="input-group" style="padding-top: 20px;">
  <span class="label_1">Name </span>
  <input type="text" class="inp_t" placeholder="Your Name" name="Name" id="name" required>
 </div>

 <div style="padding-top: 20px;">
   <button type="submit" class="btn btn-primary register" value="submit"  >Submit</button>
 </div>
</form>

<script language=JavaScript>
$('#register').validator()
   .submit(function(e){
      var form = $(this);
      var valid = form.validator({messageClass: "alert alert-danger validate_error", position: 'center right'});
      if (valid.data("validator").checkValidity()){
          alert('not working');
      }// VALID
      e.preventDefault();
      return false;
});// SUBMIT    
</script>

So you only want to prevent the default action if the form is not validated properly:

if (valid.data("validator").checkValidity())
{
    // Assuming this means Validation failed
    alert('not working');
    e.preventDefault();
    return false;
 }
 else
 {
    // It's working, let the submission go through
    return true;
 }

Now you need to give your submit button a name so that PHP can know about it

<button type="submit" name='submit-btn' class="btn btn-primary register" value="submit"  >Submit</button>

And lastly you need to update your PHP script to handle the form submission. Put this script at the top of your register.php file since that's where your form is submitting to, and since you're submitting via POST, check the post array:

                            // Submit button value check here
if(isset($_POST['submit-btn'] && $_POST['submit-btn'] == "submit")
{
    // Handle your form submission here
    // Read the data from the $_POST array using the name you gave to the form elements
    // Create a database connection and store your data in the database
} 

You should write "return true" some where in your JS function for the form to submit. If the validation is successful then return true from your JS function

You have to call event.preventDefault() only when validation fails. So moving it to else clause.

<script language=JavaScript>
$('#register').validator()
   .submit(function(e){
      var form = $(this);
      var valid = form.validator({messageClass: "alert alert-danger validate_error", position: 'center right'});
      if (valid.data("validator").checkValidity()){
          alert('not working');
          return true;
      }// VALID
      else {
          e.preventDefault();
          return false;
      }
});// SUBMIT    
</script>

使用onClick="validate()"调用验证器函数,验证成功后,使用document.getElementById("register").submit();提交表单document.getElementById("register").submit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM