0

I have a .php file which has so far two functions. Here is the file :

          <?php
        function Register()
        {
        $Name=$_POST["NameTxt"];
        $Email=$_POST["EmailTxt"] ;
        $Pass=$_POST["PasswordTxt"];
        $RePass = $_POST["RePasswordTxt"];
        if (!preg_match("/^[a-zA-Z\- ]*$/",$Name) or empty($Name)) {
          header("Location: http://localhost/tutorial/Mdaeg-login.html");
        }
        elseif (!filter_var($Email, FILTER_VALIDATE_EMAIL)or empty($Email) or empty($Pass)) {
              header("Location: http://localhost/tutorial/Mdaeg-login.html");
        }
        elseif($Pass!=$RePass)
        {
              header("Location: http://localhost/tutorial/Mdaeg-login.html");   
        }

        $connect=mysql_connect("localhost" , "root" , "Password") or die(mysql_error());
        mysql_select_db("Database", $connect);
        $read="select  Email from users where Email='$Email'";
        $result=mysql_query($read,$connect);
        $rowcount=mysql_num_rows($result); 
        if($rowcount==0)
        {
        $sql="INSERT INTO Users (Name,Email,Password) VALUES ('$Name','$Email','$Pass')";
        mysql_query($sql,$connect);
        }
        mysql_close($connect);

        header("Location: http://localhost/tutorial/Mdaeg-login.html");
        }
    function Login()
    {
$Email=$_POST["EmailLogin"] ;
$Pass=$_POST["PassLogin"];
$connect=mysql_connect("localhost" , "root" , "kamal3007") or die(mysql_error());
mysql_select_db("Mdaeg", $connect);
$read="select  Email,Password from users where Email='$Email'";
$result=mysql_query($read,$connect);
$rowcount=mysql_num_rows($result); 
mysql_close($connect);
if($rowcount==1)
{
      header("Location: http://localhost/tutorial/Profile.html");

}

    }

        ?>

I have two questions about this code. First,is there any possible way to call a .php function from javascript? Second question, in the second function I'll get the email and the password from the database, but how can I read each column separately so that I can check the password? Thank you in advance. :)

S.Joe
  • 77
  • 1
  • 1
  • 6

2 Answers2

2

You have to call a ajax function

var function getOutput() {
 $.ajax({
  url:'your_function_url',
  complete: function (response) {
      $('#output').html(response.responseText);
  },
  error: function () {
      $('#output').html('Bummer: there was an error!');
  }
 });
return false;
}
Drop Shadow
  • 845
  • 3
  • 12
  • 28
0

Try to post data from your page using ajax function

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

And then create a class file having those two functions

class clsname{
  public function Register(){}
  public function Login(){}
}

try {
  //  catch the post veriable
  // create an object to call function/method of class
  // get return value like header location and redirect
}catch(Exception $e){
}

And you can you the mysql_fetch_array to fetch the value, but those are deprecated function try to use php PDO native library, that will definately help you as per future prespactive

Ashish Ranade
  • 595
  • 2
  • 14