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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:00:30+00:00 2026-06-09T12:00:30+00:00

i am having trouble making this part of code to work, basically i want

  • 0

i am having trouble making this part of code to work, basically i want to call this function which sends a variable to a php page. Ive tested that the variable is there and also tested that my php page is accepting information as it should be , however i cant make this Ajax thing work.

function ajaxRequest(myname) {
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) 
{ // Does this browser have an XMLHttpRequest object?
    AJAX=new XMLHttpRequest(); // Yes -- initialize it.
} else 
{ // No, try to initialize it IE style
    AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.

if (AJAX==null) 
{ // If we couldn't initialize Ajax...
    alert("Your browser doesn't support AJAX."); // Sorry msg.
return false // Return false, couldn't set up ajax
}
AJAX.onreadystatechange = function() 
{ // When the browser has the request info..
    if (AJAX.readyState==4 || AJAX.readyState=="complete") 
    { // see if the complete flag is set.
        callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
    } // End Ajax readystate check.
}

alert("Alert1");
var url='http://localhost/main.php?Name=myname';    
AJAX.open("POST", url, true); // Open the url this object was set-up with.
alert("Alert2");
AJAX.send(); // Send the request.

}

This is my php part which should accept the variable

<?php
$var=$_GET['Name'];

echo $var;
?>
  • 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-09T12:00:31+00:00Added an answer on June 9, 2026 at 12:00 pm

    Okay firstly you need to change your request to GET from POST
    like

    AJAX.open("GET", url, true); // Open the url this object was set-up with.
    

    and you also need to update this line
    from

    var url='http://localhost/main.php?Name=myname'; 
    

    to

    var url='http://localhost/main.php?Name='+myname; 
    

    my full script is:

    <script type="text/javascript">
        function ajaxRequest(myname) {
            var AJAX = null; // Initialize the AJAX variable.
            if (window.XMLHttpRequest) 
            { // Does this browser have an XMLHttpRequest object?
                AJAX=new XMLHttpRequest(); // Yes -- initialize it.
            } else { // No, try to initialize it IE style
                AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
            } // End setup Ajax.
    
            if (AJAX==null) 
            { // If we couldn't initialize Ajax...
                alert("Your browser doesn't support AJAX."); // Sorry msg.
                return false // Return false, couldn't set up ajax
            }
    
            AJAX.onreadystatechange = function() 
            { // When the browser has the request info..
                if (AJAX.readyState==4 || AJAX.readyState=="complete") 
                { // see if the complete flag is set.
                    callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
                } // End Ajax readystate check.
            }
    
            alert("Alert1");
            var url='http://localhost/main.php?Name='+myname;    
            AJAX.open("GET", url, true); // Open the url this object was set-up with.
            alert("Alert2");
            AJAX.send(); // Send the request.
        }
    </script>
    

    you might also be missing the callback function so add it so that it looks like this

    function callback(x, y) {
        alert(x);
    }
    

    And call your AJAX function by

    ajaxRequest("ashley");
    

    Here is your required main.php code (even though this isn’t what you should be using AJAX for

    <?php 
    session_start();
    if(isset($_GET["Name"])) {
       $_SESSION["Name"] = $_GET["Name"];
    }
    if(isset($_SESSION["Name"])) {
       echo $_SESSION["Name"];
    } else {
       echo "The AJAX has not been run!";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble making this work and am looking for some help. Currently
I'm having trouble making this work. Apparently, i can't use > or < in
Having Trouble making sql connection. this is my code private static SqlConnection GetConnection() {
I'm having a bit of trouble making this JQuery work in Internet Explorer, it
I'm having trouble with the way I designed this little report I'm making. Is
i'm having some trouble in making this on right. I'm trying to get the
I'm having trouble making this simple thing happen. I have this loop adding values
I'm trying to install Redmine, and I am having trouble making Phusion Passenger work
I'm having trouble making this mouseover event repeatable. Meaning each time you mouseover, then
I am having trouble making calls to multiple URI's , currently this works in

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.