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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:20:11+00:00 2026-05-18T20:20:11+00:00

I’m banging my head for some hours now. I have a select option that

  • 0

I’m banging my head for some hours now. I have a select option that it updates a database using AJAX (at least it’s trying to!). What happens is while running the PHP script directly with params required the database gets updated but not when running indirectly through AJAX, instead I get 3 times the alert("There was a problem in the returned data:\n"); and then it gets updated. JavaScript is on <head> and not in an external file. Here it goes:

JavaScript:

function updateHub(){  
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari  
        xmlhttp=new XMLHttpRequest();  
    }else{
        // code for IE6, IE5  
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
    }  
    xmlhttp.onreadystatechange = function(){  
       if (xmlhttp.readyState==4 && xmlhttp.status==200){  
           document.getElementById("hubinfo").innerHTML=xmlhttp.responseText;  
       }else{  
           alert("There was a problem in the returned data:\n");  
       }  
    }  
    var prefHub = document.getElementById("prefHub");  
    var hubID = prefHub.options[prefHub.selectedIndex].value;  
    xmlhttp.open("GET","updateHub.php?hubID="+hubID,true);  
    xmlhttp.send();  
}

updateHub.php:

session_start();
include '../../../common/config.php';
$hubID = '';
if(isset($_POST['hubID'])){
    $hubID = strip_tags(mysql_real_escape_string(trim($_POST['hubID'])));
}elseif(isset($_GET['hubID'])){
    $hubID = strip_tags(mysql_real_escape_string(trim($_GET['hubID'])));
}
mysql_query("UPDATE prefs set hubID='$hubID' where userID = '".$_SESSION['userID']."'") or die(mysql_error());
if(mysql_affected_rows()){
    echo "Updated";
}else{
    echo 'Error';
}
return $hubID;

and the HTML:

<form action="" method="post" onsubmit="updateHub();">
    <select name="prefHub" id="prefHub">
        <option value="43">opt1</option>
        <option value="64">opt2</option>
        <option value="30">opt2</option>
     </select>
     <input type="submit" name="update" value="Update Hub"/>
</form>
<div id="hubinfo"></div>
  • 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-18T20:20:12+00:00Added an answer on May 18, 2026 at 8:20 pm

    instead I get 3 times the alert(“There
    was a problem in the returned
    data:\n”);

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("hubinfo").innerHTML=xmlhttp.responseText;
    }
    else{
    alert("There was a problem in the returned data:\n");
    }
    

    This means that you will always see the alert. Since for any other xmlhttp.readyState values the else {} block will be executed.


    The XMLHttpRequest object can be in several states. The readyState attribute must return the current state, which must be one of the following values:

    UNSENT (numeric value 0)

    The object has been constructed. 
    

    OPENED (numeric value 1)

    The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method. 
    

    HEADERS_RECEIVED (numeric value 2)

    All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available. 
    

    LOADING (numeric value 3)

    The response entity body is being received. 
    

    DONE (numeric value 4)

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6

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.