javascript - AJAX connect to PHP OOP WebService

897

Why does this not work?

class WebService {
    public static function CheckUserLogin() {
    }
}

How i can call this function?

My login.php Form

<form id="form" name="form" method="POST" onsubmit="" action="">

E-Mail: <input type="text" size="30" name="email" id="email" >
Passwort: <input type="text" size="30" name="passwort" id="passwort" >

<input type="submit" value="Login" name="submit" />

</form>

login.js

 $(function () {
    $('form').on('submit', function (e) {
        var email = document.getElementById('email').value;
        var passwort = document.getElementById('passwort').value;

        $.ajax({
            type: "POST",
            url: "WebService.php",
            data: "{ 'email': '" + email + "', passwort: '" + passwort + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response);
            },
            failure: function (response) {
                alert(response);
            }
        });
      e.preventDefault();
    });
  });

I want to Call the Function CheckUserLogin(). How i can do this? Or what i must do?

Thank you very much!

813

Answer

Solution:

In your ajax call, alongwith the url you can send a parameter, say- action:

....
url: "WebService.php?action=checkLogin",
....

Then in PHP, just get this variable and call the desired function-

if(isset($_GET['type'])){
   if($_GET['type'] == "checkLogin"){
      $ws = new WebService;
      echo $ws->CheckUserLogin(); 
      // this will call the desired function and the result will be obtained by the ajax
   }
}

Similarly you can use the same concept to call different functions at different points.

198

Answer

Solution:

You need to instantiate the class WebService. The call to the file is just running the file procedurally.

$webservice = new WebService;
$webservice->CheckUserLogin();
383

Answer

Solution:

you must check POST data in webservice.php like this

if(isset($_POST['email'],$_POST['passport'])){}

and call your method if condition true like this

$a = new WebService;
$a->CheckUserLogin();

Success!)

People are also looking for solutions to the problem: php - Javascript location.href doesnt redirect

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.