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. :)

8
  • 1) Directly not possible. You need to make a request to the php script. For example with AJAX. Commented Aug 25, 2015 at 10:19
  • hmm, okay @CharlotteDunois, can you give me an example or a link about it ? Commented Aug 25, 2015 at 10:20
  • 2) You need to fetch the rows. E.g. with mysql_fetch_array() Commented Aug 25, 2015 at 10:20
  • possible duplicate, please see stackoverflow.com/questions/7165395/… Commented Aug 25, 2015 at 10:20
  • 1
    Also you are still using the deprecated mysql_* functions. I recommend to migrate to PDO. Commented Aug 25, 2015 at 10:21

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.