0

Q. Is there a way to pass values from ajax to a certain php class having functions? Let's say validating a username on the registration form whether the user exist or not.

This is a simple form that will accept username and has a span tag to display the message.

<form action="" method="POST">
  <input type="text" name="username"><span class="check"></span>
  <input type="submit" name="signup">
</form>

And for the php class:

<?php 
  class User {
    function isUserExist($username) {
      $query = mysql_query("SELECT username FROM users WHERE username='$username'");
      $result = mysql_num_rows($query);

      return ($result !== 0 ? true : false);
    }
  }
?>

It is initialized on the php class that established connection to the database. So calling to the php page will become like this: $user->isUserExist($_POST['username']);.

So is it possible to pass values from the form to ajax and send it to the php class function?

2 Answers 2

1

From Html to ajax

var username = $("input[name='username']").value;

Fetch in ajax & Send it to php(server)

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      //set your span to this -> xhttp.responseText;
    }
  };
  xhttp.open("POST", "your php script url", true);
  xhttp.send("username="+username);

Receive it on the server(php)

$mUsername = $_POST['username'];
echo $mUsername;

Read this tutorial for more help Tutorial on PHP + AJAX

Sign up to request clarification or add additional context in comments.

8 Comments

Can you give me some explanation with this? I'm still quite new about ajax.
read about it on w3schools. Pretty simple and neat explanation. What are we doing here is : We create a request XmlHttpRequest , wait for a change in that change status (readyState == 200 means good to go). then we put params and send it using send method.
I've tried it. But how to call the function inside the class? I declare the $username = $_POST['username']; before the class to make it global but it says that the username is undefined.
Function isUserExist($username) inside my User() class. Also inside a class has many functions, I also have a php file with 2 classes. So how can I access a specific class and a specific function?
I would suggest you to look at php tutorial for this. In general if a class has a function, calling a function from that class requires you to create that class first and get an object and call a method on it. But do look at some php tutorial on w3schools to help you with that.
|
0
Try this,

<script type="text/javascript">
$(document).ready(function(){
    $("input[name = 'signup']").click(function(e) {
        var username = $("input[name = 'username']").val();
        $.ajax ({
            url: "isUserExist_function_existing_file.php", 
            data: { username : username },
            success: function( result ) {
                if(result)
                    alert("Name allready Exist");
                else
                    alert("Name available");
            }
        });
    });
});
</script>

4 Comments

The name of the file is user.php. How can I access the function isUserExist($username) of the class User();? There are many more functions inside the class, so how can I call the specific function?
Are you able to access the function like url: "user/isUserExist", or Try if(isset( $_POST['username'] )) { $user = new user(); $result = $user->isUserNameExist($_POST['username']); return $result; }
I don't think it will work since the path of the php file is needed. Calling the function inside the class is what I need.
Ajax should have some endpoint URL that can access public eg:- facebook.com/chat/user_info/?dpr=1 Are you using any Framework?

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.