Hello!

Wondering if someone could give me a hand.. Im looking to make a form kind of like this one.. "http://www.nvidia.com/Download/index.aspx?lang=en-us"

Where the fields change depending on what is slected.. I would like to link the values of the of the form to a mySQL DB. So the form when loaded askes the DB what values to display. The end result would be a list of results searched by the form.

very much like the form listed above.. but what im really hanging up on is the changing what fields are showing..

Thanks,
-LTT

Recommended Answers

All 9 Replies

Member Avatar for diafol

Are you sure you want the pHp forum or ASP?

Do you want to change the language of the page (as in your example) or to display/not display certain widgets (linked to DB fields)?

The example shows url parameters. This isn't a very secure way of directing changes as an user could rewrite the url and refresh the page to totally mash your app.

Could you be more specific with your problem?

Do you want to change values in a select widget (listbox or dropdown) based on a value selected in some other widget?If so, you'll probably need to use Ajax to force a refresh of data without page reload.

I would like to do the form in PHP if possible. I will be creating a form with values that will constantly be changing/ added to. Im going to build a simple back end to add/ remove DB entrys.

In the end when a user hits submit the form will get a list of results and display it on the same page if possible or on a different page if need be. Kind of like when you have a PHP searh on a site.

The example provided. "http://www.nvidia.com/Download/index.aspx?lang=en-us" is only being referenced for its dynamic field abilities. My form will not have a "Language" option.. that form is for reference only.

Basicly I just need find a way to make a PHP form where as you click a drop down box.. depending on what you select from that box, the next box displayed has different content in it.

EX:

BOX 1
Values=
A
B
C

BOX 2
IF value A selected this box displays
C
D
E
IF value B selected this box displays
F
G
H
IF value C selected this box displays
I
J
K

and so on.. with each box following depending on the previous box..

Thanks,
-LTT

Member Avatar for diafol

Thought so, you need Ajax - otherwise you'll have to reload the page to get new DB values into the next dropdown.

I suggest that you download the prototype library (or any other comparable js library) and use that to handle the js part. If you try to build the object yourself from scratch, it can get a bit messy.

In short:

1) include the prototype file in your form page.
2) Use the onblur attribute (or onclick, etc) of your first dropdown to call your ajax custom function

You'll need a custom js file with your function (or you can use the script tag with inline js).
You'll need a pHp file to process the incoming data from the ajax call (js) and to echo back the update info to the same js function. This info will then be used to update the second widget.

You should be able to use the Ajax.Update function to update your second widget.

IMO, this is the only real way to go without reloading the page

suppose i allow a page refresh to update the form what would it look like/ how well do you think it would work?

Thanks again,
-LTT

Member Avatar for diafol

You could - but it's a bit extreme these days just to populate a dropdown.

I think the time invested into getting to grips with something like Ajax would really afford benefits. Using a 'pure' PHP solution will be a messy fix with reloads.

As a halfway house, you could populate a few javascript variables on page load with data from the DB that will be activated (change the 2nd dropdown) when the 1st dropdown is changed. This would avoid reloads and you wouldn't have to 'dirty your hands' with Ajax.

You could start with something like this:

Option tag values and labels put into array variables from MySQL queries, e.g. $a_values, $a_labels, $b_values, $b_queries.

These can then be used to parse into js variables in inline SCRIPT tags.

Mind you as I type this, I haven't tried this myself, but it should work in theory.

Member Avatar for diafol

OK, OK, I'm being a bit obscure..

Here's a fix - untested, but on the right lines...
It uses Ajax (prototype) but it's really easy - if I can use it anyone can!

On top of your form page in the head area, put this:

<script src="path/to/prototype.js" type="text/javascript"></script> 
<script src="path/to/myAjax.js" type="text/javascript"></script>

In your form (1st dropdown) do this (the values and labels should really be generated from the DB, because the values (4,8,10) should relate to mysql foreign keys for the second dropdown, but could be coded by hand:

<select id="myselect" name="myselect" onChange="getOptions();return false;">
    <option value = "4">Wales</option>
    <option value = "8">Scotland</option>
    <option value = "10">Ireland</option>
</select>

The second dropdown (assuming default is Wales from dropdown 1):

<select id="myselect2" name="myselect2">
    <option value = "1">Cardiff</option>
    <option value = "7">Swansea</option>
    <option value = "13">Aberystwyth</option>
</select>

Here's your custom function written in your myAjax.js file:

function getOptions(){
	var sVal = $F('myselect');
	var sContainer = 'myselect2';
	var pars = "id=" + sVal;
	var sURL = 'includes/get_options.inc.php'
	var getAjax = new Ajax.Updater(sContainer,sURL,{method: 'post',parameters: pars});
}

Your php file for handling the request and posting back the data is as follows:

//(set your mysql connection details to $conn)

$id = addslashes(htmlentities($_POST['id']));

$sql = mysql_query("SELECT * FROM cities WHERE country = '{$id}'",$conn); 
$output = "";
while($data = mysql_fetch_array($sql)){
    $output = $output . "\n\t<option value=\"{$data['city_id']}\">{$data['city_name']}</option>"
}
echo $output;

You'll probably need to debug this as I typed it off the top of my head and haven't tried it out. Good luck.
By the way you can get the prototype library from the scriptaculous site.

Thanks again! Ill see what I can do.. you were very helpful!

-LTT

Ok I have all that set up and a connection to my DB.
Im a pretty big newb.. what should my DB structure look like for this example to work ex: tables and such.. Im using PHPmyAdmin to edit my DB..

-Thanks

Moving this to the Ajax forum.. thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.