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 6010549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:07:55+00:00 2026-05-23T02:07:55+00:00

I have a JavaScript function that is being called. I need to have it

  • 0

I have a JavaScript function that is being called. I need to have it call a PHP function and return a true/false.

The script with the function is in the file /db/cancel_hike.php

My current JS looks like this:

function uncancelHike( hike_id )
{
    //var url = "/db/cancel_hike.php;
    var success = null;

    var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

    request.open("GET", url , true);

    request.onreadystatechange = function()
    {
          if (request.readyState == 4)
          {
                var xmlDoc = request.responseXML;

                // obtain the array of markers and loop through it
                markers = xmlDoc.documentElement.getElementsByTagName("marker");

                for (var i = 0; i < markers.length; i++)
                {
                      // obtain the attribues of each marker
                      success = markers[i].getAttribute("success");

                      if ( success == "true" )
                      {
                        document.getElementById("success").style.display = 'block';
                        document.getElementById("warning").style.display = 'none';
                        document.getElementById("error").style.display = 'error';

                      }

                      if ( success == "false" )
                      {
                        document.getElementById("success").style.display = 'none';
                        document.getElementById("warning").style.display = 'none';
                        document.getElementById("error").style.display = 'block';
                      }
                }
          }
    }

    request.send(null);

    return false;
}

What I am having trouble with is:

  1. How to call an actual function in the PHP script?
  2. Do I absolutely need to have some XML returned? Or is there a way to just get back the returned value?
  3. I am using YUI JS library. Do I need to make some calls to it, or is it not necessary in this case?
  • 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-23T02:07:56+00:00Added an answer on May 23, 2026 at 2:07 am
    1. How to call an actual function in the PHP script?
    2. Do I absolutely need to have some XML returned? Or is there a way to just get back the returned value?

    Well, you don’t call the actual function. What you want to do is pass variables using GET, that is, by appending them to the URL like file_name.php?var1=this&var2=that to pass var1 of “this” and var2 equaling “that.” You retrieve them in the PHP file with $_GET['this'] and $_GET['that']. Whatever PHP outputs to the page via echo, print_r, etc. is then sent back in a request object as part of its responseText property.

    You just set url in request.open to a URL on your site. For example, in your .js file:

    request.open("GET", "answer_me.php?hike_id=" + hike_id, true);
    

    And in your .php file:

    <?php
    $hike_id = $_GET['hike_id'];
    if ($hike_id < 5) {
      echo "true";  // echo true would return "1", BTW
    } else {
      echo "false"; // echo false would return nothing
    }
    

    Note that that will just return a string value to request.responseText of false, thus you could do this:

    var result = request.responseText;
    
    if (result === "true") {
      ...
      document.getElementById("success").style.display = "block";
      ...
    } else {
      ...
      document.getElementById("success").style.display = "none";
      ...
    }
    

    You do not need it to be XML, especially as it looks like you’re not really using the loop (the same three DOM elements are being assigned values each time).

    And honestly, for AJAX I’d recommend using a framework like jQuery (or YUI, although I don’t find its AJAX stuff as intuitive). Your entire code would look like this:

    var $success = $("#success");
    var $error = $("#error");
    
    function cancelHikeCallback(data) {
      var is_success = (data === "true");
      $success.toggle(is_success);
      $error.toggle(!is_success);
    }
    
    function cancelHike(hikeIdToSend) {
      $.get("/db/cancel_hike.php", {hike_id: hikeIdToSend}, cancelHikeCallback);
    }
    

    IMO things like jQuery’s $.ajax ($.get is a specialized form of $.ajax) make this stuff much easier to read and debug.

    jsFiddle Example

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

Sidebar

Related Questions

I have a javascript function being called by an input type=image tag that generates
I have a javascript function that is being built to animate the collapse of
I have a javascript function that manipulates the DOM when it is called (adds
I have a Javascript function that is called from the onchange method in a
We have validation rules in an external JS file that is being called via
I have a javascript function that calls a generic function to make an ajax
I have a javascript function that accepts a number and performs a mathematical operation
I have a javascript function that checks for a date range. Is there any
I have a javascript function that toggles the display for rows in a table.
I currently have a Javascript function that uses a string to reference an object

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.