Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with AJAX. Cant seem to get the form data to the processing part of the script.

    Ok I took a working AJAX Demo and tried to incorporate it into my program and modify it to do what I wanted and I apparently hosed it up totally. I have no idea what I've screwed up so would greatly appreciate some guidance.
    First the AJAX java script in the header.
    <script type="javascript">

    function doHttpRequest2() { // This function does the AJAX request
    http.open("GET", "orders.php?mfm=detail2&cord="+document.getElementById('cord').value, true);
    http.onreadystatechange = getHttpRes;
    http.send(null);
    }

    function getHttpRes() {
    if (http.readyState == 4) {
    res = http.responseText; // These following lines get the response and update the page
    document.getElementById('cust').innerHTML = res;
    }
    }

    function getXHTTP() {
    var xhttp;
    try { // The following "try" blocks get the XMLHTTP object for various browsers…
    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    // This block handles Mozilla/Firefox browsers...
    try {
    xhttp = new XMLHttpRequest();
    } catch (e3) {
    xhttp = false;
    }
    }
    }
    return xhttp; // Return the XMLHTTP object
    }

    var http = getXHTTP(); // This executes when the page first loads.

    </script>
    now the form that is populated from the database (that part is working fine) and is supposed to result in more info showing up via AJAX
    #Ajax form
    echo "<form name=\"myform\">\n";
    echo "<select name=\"cord\" id=\"cord\">\n";
    echo "<option>Select A Customer</option>\n";
    while($row=$result->fetch_object()){
    echo "<option value=\"".$row->id."|".$row->odate."\">".$row->fname." ".$row->lname." -- ".$row->odate."</option>\n";
    }
    echo "</select>\n";
    echo "<input type=\"button\" value=\"Press to submit form\" onClick=doHttpRequest2()>\n";
    echo "</form>\n";
    echo "<br>\n";
    echo "<div id=\"cust\"><b>Order info will be listed here.</b></div>\n";
    #end Ajax form
    Everything looks great but when you click the submit button nothing happens. I've tried just echoing the $_GET['cord'] variable in the script that it is supposed to be calling orders.php?mfm=detail2&cord="+document.getElementById('cord').value I get nothing.

    I've brought up the console in Firefox and when I click the button it says that doHttpRequest2 is not defined although clearly it is.
    I've been looking at this for what seems like forever and I can't find what is wrong with it.

    Please enlighten me.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    <script type="javascript">
    should be
    <script type="text/javascript">

    replace all this ...
    Code:
    function getXHTTP() {
      var xhttp;
       try {   // The following "try" blocks get the XMLHTTP object for various browsers…
    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e2) {
     		 // This block handles Mozilla/Firefox browsers...
    	    try {
    	      xhttp = new XMLHttpRequest();
    	    } catch (e3) {
    	      xhttp = false;
    	    }
          }
        }
      return xhttp; // Return the XMLHTTP object
     }
    
     var http = getXHTTP(); // This executes when the page first loads.
    with just this

    http = new XMLHttpRequest();
    Last edited by DaveyErwin; 02-27-2014 at 07:35 PM.

  • Users who have thanked DaveyErwin for this post:

    Marsha (02-28-2014)

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Davey.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •