TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Could I use Ajax to preload a dropbox? (http://www.talkphp.com/javascript-ajax-e4x/3651-could-i-use-ajax-preload-dropbox.html)

superthin 11-25-2008 01:18 AM

Could I use Ajax to preload a dropbox?
 
Hello all,

I have got a html form to input books' information. When I run my PHP script in localhost, everthing is good. The problem is when I upload it to my shared hosting, the hosting provider tells me that my script consumes a lot of server's resources.

My friend told me that I should use Ajax to pre-load dropbox(es). I don't know how to do that.



I want to "Choose a language" (1) it will have an influence on "Choose an author" (2) and "Choose a publisher" (3). That means "When I choose French, American authors and publishers will not to be listed".

I am using an "onchange" event in HTML to refresh / reload. It makes a slow flicker, a long delayed time, a lot of hosting resource,...

Some queries with Google and Altavista I cannot solve the problem. Will everybody here tell me what Ajax tool(s) I should use?

Thanks in advance.

Best regards,

martins256 11-25-2008 09:13 AM

It's easy with jquery! I'll make an example for you:

some place in your form
HTML Code:

<select name="authors" id="authors" >
<option value="1">French</option>
<option value="2">American</option>
</select>

<select name="publisher" id="publisher" ></select>

JS to do all the ajax magic(I use jquery so you will need that to try my example too).
HTML Code:

$(document).ready(function(){
  $("#authors").change(function(){
    id = $("#authors").val();
    $("#publisher").load("ajax.publisher.php?id="+id);
  });
});


And the "ajax.publisher.php"
PHP Code:

<?php
session_start
();
error_reporting(E_ALL);

include(
'config.php');
include(
'functions.php');

if(isset(
$_GET['id']) and ctype_digit($_GET['id']))
{
  
$sql "SELECT * FROM publishers WHERE author_id = {$_GET['id']}";
  
$result mysql_query($sql);
  while(
$row mysql_fetch_assoc($result))
  {
    echo 
'<option value="'.$row['id'].'">'.$row['publisher'].'</option>';
  }
}
?>


superthin 11-25-2008 04:57 PM

Oh, thank martins256 very much. I would like to say "You are a star" :)


All times are GMT. The time now is 09:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0