CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2007
    Posts
    2

    Unhappy About to GO insane! ajax + php == :*( Please gurus help me!

    Hi guys im trying to get this simple page to work and i dont know why it does not owrk. im new to ajax


    i crreated a form and then i use ajax to call a php script which prints out options for the select.

    however when i submit my php can not capture the values! but my js sees the value, i use and alert to display it when it is changed and it shows the appropriate value. forexample $_POST['testing']


    Code:
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
    
    <script language="javascript"> 
        function checking() 
        { 
            var xmlHttp = false; 
            try 
            { 
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
            }    catch(e) 
            { 
                try 
                { 
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
                }    catch(e) 
                { 
                    try 
                    { 
                        xmlHttp = new XMLHttpRequest(); 
                    }    catch(e) 
                    { 
                        altert("your browser is too old") 
                    } 
                } 
            } 
             
            var obj = document.getElementById("testing"); 
             
            xmlHttp.open("GET","select.php"); 
             
            xmlHttp.onreadystatechange = function() 
            { 
                if(xmlHttp.readyState == 4) 
                { 
                    obj.innerHTML = xmlHttp.responseText; 
                } 
            } 
             
            xmlHttp.send(null); 
        } 
         
        function castvalue(v) 
        { 
            alert(v); 
        } 
             
    </script> 
    
    <? 
        if($_POST['submit']) 
        { 
            echo("Testing: - " . $_POST['testing'] . " -"); 
        } 
    ?> 
    
    </head> 
    
    <body onload="checking();"> 
        <form method="post"> 
            <select id="testing" onchange="castvalue(this.value);"> 
                <div id="grab" name="grab"> 
                </div> 
            </select> 
            <input type="submit" id="submit" name="submit" value="submit" /> 
        </form> 
    </body> 
    </html>

    select.php

    PHP Code:
    Code:
    <? 
        include("../include/cms/connect.inc.php"); 
                 
        $platformlist = "SELECT * FROM platform"; 
    
        $myplatformlist = mysql_query($platformlist) or die(mysql_error()); 
         
        while($queryplatformlist = mysql_fetch_array($myplatformlist)) 
        { 
                echo  "            <option value=\"" . $queryplatformlist["id"] . "\">" . $queryplatformlist["name"] . "</option>"; 
        } 
         
        mysql_free_result($myplatformlist); 
        mysql_close();     
    ?>

    Plz plz help me~

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: About to GO insane! ajax + php == :*( Please gurus help me!

    XMLHttpRequest creates an entirely new request, separate from your page. This means it doesn't matter what form(s) you have on your page (which wouldn't make sense anyways - how would it know what form to send and what if you didn't want to send them?)

    So what you have to do is to add the values you want to send to your request object. I've actually made a class library in JavaScript that will do this for you for any form you like (it'll replace the form's submit action so all you have to do is add one attribute to your form and it'll work.) Here it is: http://javascript.mezane.org/ajax-form/ajax-form.js
    Try to submit a comment at http://mezane.org/ to see it in action (also look in the source code to see how it is used.)

  3. #3
    Join Date
    Nov 2007
    Posts
    2

    Unhappy Re: About to GO insane! ajax + php == :*( Please gurus help me!

    Can you explain this a little more, im really dumb and need step by step instructions :*(

    i have no idea what to do with that code

  4. #4
    Join Date
    Jun 2006
    Posts
    8

    Re: About to GO insane! ajax + php == :*( Please gurus help me!

    There is at least one mistake in the code you copy and pasted into your post. the function name alert is misspelled as "altert". Note, that in JavaScript even if you have one error the code will not compile and may not execute it at all.
    Authentic Society
    www.authenticsociety.com

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured