Click to See Complete Forum and Search --> : js + php question


hillman
03-27-2006, 06:14 PM
Hi, I'm pretty inexperienced with javascript so any help anyone can give would be greatly appreciated.

I have an sql query that produces a dynamic list box. My project will eventually have a number of these which will be used to populate a 'search box' text field which will be submitted as a search string.

I have been playing around with the code from http://javascript.internet.com/forms/selection-box.html#source.

With this code i have been able to create multiple select boxes that populate a single box.

Is it possible to use this code along with my sql statement so that the entries from my dynamic list box can be used to populate the 'choicebox'. Basically so that it works in the same way, ie with no screen refresh.

I realise there is no direct communication between js and php but I thought because I am creating HTML elements that would bridge the gap - php 'creates' the HTML and then js can pull the values from the HTML. Or does it not work like this?

As I said above any help would be great, thanks in advance.

hillman

This is the list box creation part of my sql query:


$active_job_list = '<select name="active_job_list" size="10 onchange="moveOverActive();">';

while($active_jobs = mysql_fetch_array($sql_active_jobs, MYSQL_ASSOC)){


$active_job_list .= ' <option value="' . $active_jobs['job_id'] . '">' . $active_jobs['job_title'] . '</option>';

}

$active_job_list .= '</select>';


and the js:

function moveOverActive()
{
var boxLengthActive = document.choiceForm.choiceBox.length;
var selectedItemActive = document.choiceForm.active_job_list.selectedIndex;
var selectedTextActive = document.choiceForm.active_job_list.options[selectedItemActive].text;
var selectedValueActive = document.choiceForm.active_job_list.options[selectedItemActive].value;
var a;
var isNewActive = true;
if (boxLengthActive != 0) {
for (a = 0; a < boxLengthActive; a++) {
thisitemActive = document.choiceForm.choiceBox.options[a].text;
if (thisitemActive == selectedTextActive) {
isNewActive = false;
break;
}
}
}
if (isNew) {
newoptionActive = new Option(selectedTextActive, selectedValueActive, false, false);
document.choiceForm.choiceBox.options[boxLengthActive] = newoption;
}
document.choiceForm.available.selectedIndex=-1;
}

phpnovice
03-27-2006, 07:33 PM
Is it possible to use this code along with my sql statement so that the entries from my dynamic list box can be used to populate the 'choicebox'. Basically so that it works in the same way, ie with no screen refresh.

I realise there is no direct communication between js and php ...
Sure there is ... via the XMLHttpRequest object. This allows JavaScript to play client to server requests (nicknamed, AJAX).

hillman
03-28-2006, 03:34 AM
thanks for the reply

ok ill look into that - its just from what i had read i got the impression that it was not possible.

otherwise is what im trying to do possible?

thanks

hillman

bathurst_guy
03-28-2006, 03:40 AM
Yes its possible, as phpnovice said, its known as AJAX (http://en.wikipedia.org/wiki/AJAX)

hillman
03-28-2006, 02:37 PM
cheers anyway - ive managed to do it without ajax

hillman