SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax refresh/reload

    Im using this script, http://www.captain.at/howto-ajax-form-post-get.php, to make an ajax request to php via a drop-down (DD) select. This works fine. Then using this script, http://www.dhtmlgoodies.com/index.ht...ynamic-content, i request to show a php page with other form elements in it. These elements are determined by what the first DD of the first request (captain.at) sets a variable to. This works fine.. only the first time though. What seems to be happening is that even though each time a DD option is selected, the variable changes as it should, but when the second script fires it still shows the previous variable. Its like the div is not reloading the page with new vars set, its just showing the page it first ran. I have tried to make another call to show a different page, this works for clearing content of the div, which i want, but not making the div reload with a page using fresh vars. I hope i got my point across.

    My main question i think is, how, when i call the dhtmlgoodies script, can i get the div/page to show using the newest variable? Here is some stripped out important code.

    index.php
    Code:
    <select id="sale_type_id" name="sale_type_id" onmousedown="ajax_loadContent('search','ajax_search_blank.php');" onchange="makeRequest('ajax_search.php?sti=',this.value);ajax_loadContent('search','ajax_search.php');">
    
    	<? $_stt = db_query("SELECT sale_type_id, sale_type_name FROM sale_type WHERE sale_type_id <> 43 ORDER BY sale_type_name ASC");
    	while($stt = db_fetch_array($_stt)) { ?>
    		<option value="<?php pv($stt['sale_type_id']); ?>"><?php pv($stt['sale_type_name']); ?></option>
    	<? } ?>
    
    </select>
    
    <p>
    
    <DIV id="search"></DIV>
    ajax_search.php
    Code:
    <? 
    /*   set the sale_type id for the search drop down.   */
    if(isset($_GET['sti'])) {
    
    	$_SESSION['sti'] = $_GET['sti'];
    
    }elseif(isset($_SESSION['sti'])) { 
    
    	$_stt = db_query("
    	Select distinct
    	sale_type_field.sale_type_field_name,
    	sale_type_field.sale_type_field_id
    	From
    	sale_type_field
    	JOIN connector_sale_type_field ON sale_type_field.sale_type_field_id = connector_sale_type_field.sale_type_field_id
    	Where
    	connector_sale_type_field.sale_type_id = '".$_SESSION['sti']."'
    	ORDER BY
    	sale_type_field.sale_type_field_name ASC
    	");
    	while($stt = db_fetch_array($_stt)) {
    		$sale_type_field_id[] = $stt['sale_type_field_id'];
    		$sale_type_field_name[] = $stt['sale_type_field_name'];
    	} ?>
    
    	<select name="search_field[]">
    		<option value=""></option>
    		<option value="sale_id">Sale Id</option>
    
    		<?php for ($j = 0; $j < count($sale_type_field_name); $j++) { ?>
    
    			<option value="<?php pv($sale_type_field_id[$j]); ?>"><?php pv($sale_type_field_name[$j]); ?></option>
    
    		<?php } ?>
    
    	</select>
    
    <?php } ?>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE is notorious for caching ajax GET requests. That's probably what's happening.
    - put code in your PHP to never cache the page, or
    - add a request header to the ajax object to not allow caching, or
    - use POST
    Last edited by jimfraser; Feb 21, 2007 at 07:40. Reason: typo

  3. #3
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you. how would i change the request method to post? isint this set in the ajax somewhere?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically instead of using
    httprequest.open("GET",url + "?var1=1&var2=2&var3=3",true);
    httprequest.send(null);

    you want to use
    httprequest.open("POST", url, true);
    httprequest.send("var1=1&var2=2&var3=3");


    Using the example at http://www.captain.at/howto-ajax-form-post-get.php

    1. modify the get function to remove the ? at the beginning of the returned string ( recommended: wrap the name and value of each parameter in encodeURIComponent() )

    2. in the function makeRequest, modify these lines:

    http_request.open('GET', url + parameters, true);
    http_request.send(null);

    to read

    http_request.open('POST', url, true);
    http_request.send(parameters);

    3. Obviously make changes to the php file to look for POST data instead of GET.


Bookmarks

Posting Permissions

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