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

  • Home
  • SEARCH
  • 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 6351671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:03:04+00:00 2026-05-24T22:03:04+00:00

Is there a way I can run a php function through a JS function?

  • 0

Is there a way I can run a php function through a JS function?

something like this:

<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php 
query("hello");       ?>";    
}
</script>

<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;"     
onclick="test(); return false;"> test </a>
<span id="php_code"> </span>

I basically want to run the php function query("hello"), when I click on the href called “Test” which would call the php function.

  • 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-05-24T22:03:04+00:00Added an answer on May 24, 2026 at 10:03 pm

    This is, in essence, what AJAX is for. Your page loads, and you add an event to an element. When the user causes the event to be triggered, say by clicking something, your Javascript uses the XMLHttpRequest object to send a request to a server.

    After the server responds (presumably with output), another Javascript function/event gives you a place to work with that output, including simply sticking it into the page like any other piece of HTML.

    You can do it “by hand” with plain Javascript , or you can use jQuery. Depending on the size of your project and particular situation, it may be more simple to just use plain Javascript .

    Plain Javascript

    In this very basic example, we send a request to myAjax.php when the user clicks a link. The server will generate some content, in this case “hello world!”. We will put into the HTML element with the id output.

    The javascript

    // handles the click event for link 1, sends the query
    function getOutput() {
      getRequest(
          'myAjax.php', // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }  
    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('output');
        container.innerHTML = 'Bummer: there was an error!';
    }
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('output');
        container.innerHTML = responseText;
    }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ? 
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }
    

    The HTML

    <a href="#" onclick="return getOutput();"> test </a>
    <div id="output">waiting for action</div>
    

    The PHP

    // file myAjax.php
    <?php
      echo 'hello world!';
    ?>
    

    Try it out: http://jsfiddle.net/GRMule/m8CTk/


    With a javascript library (jQuery et al)

    Arguably, that is a lot of Javascript code. You can shorten that up by tightening the blocks or using more terse logic operators, of course, but there’s still a lot going on there. If you plan on doing a lot of this type of thing on your project, you might be better off with a javascript library.

    Using the same HTML and PHP from above, this is your entire script (with jQuery included on the page). I’ve tightened up the code a little to be more consistent with jQuery’s general style, but you get the idea:

    // handles the click event, sends the query
    function getOutput() {
       $.ajax({
          url:'myAjax.php',
          complete: function (response) {
              $('#output').html(response.responseText);
          },
          error: function () {
              $('#output').html('Bummer: there was an error!');
          }
      });
      return false;
    }
    

    Try it out: http://jsfiddle.net/GRMule/WQXXT/

    Don’t rush out for jQuery just yet: adding any library is still adding hundreds or thousands of lines of code to your project just as surely as if you had written them. Inside the jQuery library file, you’ll find similar code to that in the first example, plus a whole lot more. That may be a good thing, it may not. Plan, and consider your project’s current size and future possibility for expansion and the target environment or platform.

    If this is all you need to do, write the plain javascript once and you’re done.

    Documentation

    • AJAX on MDN – https://developer.mozilla.org/en/ajax
    • XMLHttpRequest on MDN – https://developer.mozilla.org/en/XMLHttpRequest
    • XMLHttpRequest on MSDN – http://msdn.microsoft.com/en-us/library/ie/ms535874%28v=vs.85%29.aspx
    • jQuery – http://jquery.com/download/
    • jQuery.ajax – http://api.jquery.com/jQuery.ajax/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can run this code with async:false? I notice the
Is there a way you can run your UnitTest project against a compiled DLL
Is there any way I can run class files (i.e. with main as the
Is there a way that I can run a ASP.NET MVC Project on godaddy.com
Is there any way to group tests in JUnit, so that I can run
Is there any way can declare a bean in just like JSP UseBean in
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
I am wondering if there is a way to specify a php function or
Hey! i'm looking for some way that can help me in task like this:
Is there a way I can loop through the set of classes in a

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.