0

i'm working with signup page using PHP and i validate my Signup form using JQuery...When i call my PHP function within the code of JQuery i cannot get my output.... i want to do that when user filled all the fields with proper format then the php signup function call which insert the data in the database

HERE IS MY CODE FOR PHP AND JQUERY

<?php
    function signup(){
        $name=$_POST["name"];
        $lastname=$_POST["lastname"];
        $mail=$_POST["mail"];
        $pass=$_POST["pass"];
        $repass=$_POST["repass"];
        $country=$_POST["country"];
        $city=$_POST["city"];
        $day=$_POST["day"];
        $month=$_POST["month"];
        $year=$_POST["year"];
        $gender=$_POST["name"];

        //echo $name." ".$lastname." ".$pass." ".$repass." ".$mail." ".$country." ".$city." ".$day." ".$month." ".$year." ".$gender;
    }
  ?>

<script>
$(document).ready(function(){
    $("#reg-btn").click(function () {
        var name = $("#name").val();
        var lastname = $("#lastname").val();
        var mail = $("#mail").val();
        var pass = $("#pass").val();
        var repass = $("#repass").val();
        var country = $("#country").val();
        var city = $("#city").val();
        var day = $("#day").val();
        var month = $("#month").val();
        var year = $("#year").val();
        var gender = $("#gender").val();
       if( !$('#name').val() ||  !$('#lastname').val() || !$('#mail').val() || !$('#pass').val() || !$('#repass').val() || !$('#country').val() || !$('#city').val() || !$('#day').val() || !$('#month').val() || !$('#year').val() || !$('#gender').val()) {
            $("#error").html("Please Fill all Fields");
            return false;
    }else{
            var emailRegex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            var nameRegex=/^[a-zA-Z ]+$/;
            var passRegex=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/;
            if(nameRegex.test(name)){
               if(nameRegex.test(lastname)){
                   if(emailRegex.test(mail)){
                       if(passRegex.test(pass)){
                           if(repass==pass){
                               if(nameRegex.test(country)){
                                   if(nameRegex.test(city)){
                                       <?php signup();?>
                                   }else{$("#error").html("invalid City Name");return false;};
                               }else{$("#error").html("invalid Country Name");return false;};
                           }else{$("#error").html("Passwords do not Match");return false;};
                       }else{$("#error").html("invalid Password");return false;};
                   }else{$("#error").html("invalid Email Address");return false;};
               }else{$("#error").html("invalid Last Name");return false;};
            }else{$("#error").html("invalid First Name");return false;}
        }
    });
});

</script>
  • You can't use your php(server) function as a clientside function. – himyata Aug 23 '17 at 08:26
  • PHP runs on the server, JavaScript (in this case) on the client / browser. You can't directly call PHP functions in JavaScript. – Jonnix Aug 23 '17 at 08:26
  • push it hard, maybe it will work eventually :) – Bara' ayyash Aug 23 '17 at 08:27
  • If you need to execute PHP code from a JS event then you can use AJAX. There are lots of questions and tutorials about this already, if you search. The marked duplicate has a thorough explanation of the differences between server and client code which I suggest you familiarise yourself with. – Rory McCrossan Aug 23 '17 at 08:28
  • @Jon Stirling then what should i do?? any other way to do it?? – Dani Amberseriya Aug 23 '17 at 08:28
  • Call an Ajax function that runs a PHP script with the PHP function in it. – Qirel Aug 23 '17 at 08:33

0 Answers0