Jump to content

Converting an array to read from a MySQL database


NathanS

Recommended Posts

Hi guys,

 

I've just created an ajax form whereby if you select one option from a dropdown list, it'll populate the next one dependent on the choice.

 

However, I have used an array for this so far, but I need to read from a MySQL database - anyone give any clues as to the best way of doing this?

 

I've included my code below.

 

Thanks in advance!!

 

Array (dataPage.php)

<?php
    $dataOptions = array();

    $dataOptions["Ford"] = array("Choose One", "Fiesta", "Focus", "Escort", "KA");

    $dataOptions["Renault"] = array("Choose One", "Scenic", "Clio", "Espace", "Megane");

    $dataOptions["Fiesta"] = array("Choose One", "GTI", "Blah", "BlahExec");

    if(isset($_POST["dataRequest"]) && isset($dataOptions[$_POST["dataRequest"]]))
    {
        foreach($dataOptions[$_POST["dataRequest"]] as $secondaryOptions)
        {
            printf("%s,", $secondaryOptions);
     }}
?>

 

dynamicForm AJAX

 

function clearField(obj) {
if (obj.defaultValue==obj.value) obj.value = '';
}

function chooseVehicle(requestedData,objectID) {
fetchData('dataPage.php',requestedData,objectID);
}

function filterData(pageRequest,objectID){
if (pageRequest.readyState == 4 && (pageRequest.status==200 || window.location.href.indexOf("http")==-1)) {
	var object = document.getElementById(objectID);
	object.options.length = 0;
	if(pageRequest.responseText != '') {
		var arrSecondaryData = pageRequest.responseText.split(',');
		for(i = 0; i < arrSecondaryData.length; i++) {
			if(arrSecondaryData[i] != '')
				object.options[object.options.length] = new Option(arrSecondaryData[i], arrSecondaryData[i]);
}}}}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.