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]);
}}}}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.