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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post

    changing an AJAX $_GET to $_POST

    I have found the following code and wish to have it do a POST instead of the GET

    can someone tell me what needs changing so this does a POST

    ajax.php
    Code:
    <?php
    if (is_numeric($_GET['client_id'])) {
        include("database.php");
        $query="SELECT * FROM `client_addresses` WHERE `client_id` = '". db_input($_GET['client_id']) ."'";
        $result=mysql_query($query);
    	?><select name="client_address">
        <option>Select address</option><?
        while( $res_array = mysql_fetch_assoc($result) ) {
            echo "<option value=\"".$res_array['client_address']."\">".$res_array['client_address']."</option>";
        } ?>
    	</select>
    	<?
    }
    ?>
    index.php
    Code:
    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function display_data(client_id) { 
        xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null) {
            alert ("Your browser does not support AJAX!");
            return;
        } 
        var url="ajax.php";
        url=url+"?client_id="+client_id;
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
                document.getElementById('employ_data').innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    }
    function GetXmlHttpObject() {
        var xmlhttp=null;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlhttp=new XMLHttpRequest();
        }
        catch (e) {
            // Internet Explorer
            try {
                xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlhttp;
    }
    </script>
    </head>
    <body>
    <form>
    <select name="client_name" onchange="display_data(this.value);">
        <option>Select client</option>
        <?php
        include("database.php");
    	$customer_id = "11";
        $query="SELECT * FROM `client_names`";// WHERE `customer_id` = '". $customer_id ."'
        $result=mysql_query($query);
        while( $res_array = mysql_fetch_assoc($result) ) {
            echo "<option value=\"".$res_array['client_id']."\">".$res_array['client_id']."</option>";
        }
        ?>
    </select>
    <div id="employ_data"><div>
    </form>
    </body>
    </html>
    Last edited by jasonpc1; 09-06-2010 at 08:12 AM.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Well, I guess the first thing I'd have tried was changing:
    Code:
    xmlhttp.open("GET",url,true);
    to
    Code:
    xmlhttp.open("POST",url,true);
    ... and see what happens.

    But a more efficient way might be to avoid rolling your own AJAX code altogether and let jQuery do all the heavy lifting:

    Code:
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    	$('#client_name').change(function(){
    		var client_id = $(this).val();
    		$.post('ajax.php',{client_id:client_id}, function(response_text){
    			$('#employ_data').html(response_text);
    		});
    	});
    });
    </script>
    </head>
    <body>
    <form>
    <select id="client_name">
        <option>Select client</option>
        <?php
        include("database.php");
    	$customer_id = "11";
        $query="SELECT * FROM `client_names`";// WHERE `customer_id` = '". $customer_id ."'
        $result=mysql_query($query);
        while( $res_array = mysql_fetch_assoc($result) ) {
            echo "<option value=\"".$res_array['client_id']."\">".$res_array['client_id']."</option>";
        }
        ?>
    </select>
    <div id="employ_data"><div>
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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