Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8895605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:49:25+00:00 2026-06-14T23:49:25+00:00

I have a javascript file and I want to call a function on the

  • 0

I have a javascript file and I want to call a function on the php server side and return the result back to the client side using ajax, but I am not sure how to make the request to the specific php function.

Here are my files:

The javascript file basically retrieves the username from the html form, and I want to send that username to php and check against the database for availability.

In something.js:

function check_availability()
{
    var username = $.trim(document.getElementById('usernameField').value);
    var xmlhttp= new XMLHttpRequest();

    // not sure how to make the request here to check_availability() under php
}

The PHP file will just check the username thats being passed from the js file against the database, if its available return true, else false.

In something.php:

    class Something_Model {
    private $data;
    private $table;

    public function __construct() {
        $this->data = new udata(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);
    }

    # check for username availability
    public function check_availability()
    {
        // make the check here, but needs to retrieve username from js file
        echo "here";
    }
}

So, inside the Something_Model class, I want to make the check_availability() call from javascript, can someone give me an example to make this ajax call? Also, how do I return the result back to the javascript? Do I need to encode it as a JSON obj?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:49:26+00:00Added an answer on June 14, 2026 at 11:49 pm

    You can’t call a function in PHP directly – but you can call a PHP page that in turn can call the PHP function. For example…

    service.php

    include_once('something.php');
    $model = new Something_Model();
    $model->check_availability();
    

    something.js

    function check_availability() {
    
        var request = new XMLHttpRequest();
        request.open('GET', 'http://yoursite/service.php', false);
        request.send();
    
        if (request.status === 200) {
          alert(request.responseText);
        }
    }
    

    I have used a really simple example of an XMLHttpRequest – this example actually blocks while the request is made. In reality you may want to use a callback and allow it to run asynchronously but I wanted to keep the answer as short as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AJAX function inside a javascript file and I want it to
I have an HTML file and I want to use javascript to call a
I have a php file that returns a div and a javascript. I want
I have a javascript file which has a function calling a server (url) to
I want to call a javascript function from the code-behind file - but not
I want to make an AJAX call to the server periodically, the server script(PHP)
I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html I want to
Suppose I have a Javascript file function js_main(args){ /* some code */ var x
I have a function in javascript called dumpData which I call from a button
I'm having problems trying to call a Javascript function from an enqueued javascript file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.