Populating fields and php

Hi,

Apologies if this sound ammature.

I am creating a form that has radio buttons and select tags.

I want some fields to be populated on the previous selection. For example if a radio button is selected, populate fields related to that selection and so on. I have scratched my head a lot on php and MySQL. I m nt a computer person. My question is this :

Do I have to create the form with all the fields added already and hide them and show then only when th respective selection is done? Is there an alternative to this ? Kindly describe any readings / tutorials for this.

Regarding the php part, il post a question in the php section.

I read simply JavaScript by Kevin Yank and Cameron Adams but as I said m nt a computer person so m doin al stupid things …

Help appreciated !!

Thanks

The usual way to pre-populate a form field is to set the value to be a PHP variable.

<input type='text' name='name' id='name' value='<?php echo $value; ?>' />

Where you create the variable is up to you. It can be from a previous showing of the form, or a default value generated in your PHP, etc.

For <select> and radio buttons you can pre-select a default using ‘selected’ like this:


<select name='status' id='status' >
	<option value='prov' selected='selected'>Provisional</option>
	<option value='canc'>Cancel</option>
	<option value='closed'>Closed</option>
</select>

(I have read that it’s no longer necessary to use “selected=‘selected’”, you can just use “selected”, but I’ve not proved this for myself).
If you want to change the default selection of <select> and radio buttons you have to deal with the selectedIndex property (using JavaScript).

It’s not clear to me whether you want to pre-set the selects and radio buttons, or take the user selections and use them to put values into other form fields (such as text inputs or text areas) ? If you can tell me a bit more I’ll try to answer more fully.

Yep, you can safely use the selected attribute without a value.

If you want stuff to happen dynamically when you click a radio button (for example), you’ll need to use JavaScript for that.
PHP is interpreted on the server and will just serve up HTML to your browser, so cannot by definition, cause anything dynamic to happen on your page.

If this is what you want, it would be great if you could go into a little more detail.
If this is not what you want, then just forget I spoke :slight_smile:

Thanks, Force Flow. I never seem to get around to testing it.

You can do a little fake-out where you just use javascript to submit the form when a radio is selected but only do full processing if button is submitted. Here’s a rough sample.

<?php

if (isset($_POST['submit_choice'])){
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
}


//Set vategory as Checked
$cat_car = (isset($_POST['category']) && $_POST['category'] == "car" ? " checked=\\"checked\\"" : '');
$cat_truck = (isset($_POST['category']) && $_POST['category'] == "truck" ? " checked=\\"checked\\"" : '');
$cat_van = (isset($_POST['category']) && $_POST['category'] == "van" ? " checked=\\"checked\\"" : '');
//
$category = (isset($_POST['category']) ? "{$_POST['category']}" : '');
//
$vehicles = array(
"car" => array("Select","Acura","Buick","Cadillac","Chevrolet","Dodge","Ferrari","Ford","Jaguar","Jeep","Mercedes","Pontiac"),
"truck" => array("Select","Chevrolet","Dodge","Ford","Toyota"),
"van" => array("Select","Mazda Mazda5","Chrysler Town &amp; Country","Toyota Sienna","Honda Odyssey")
);

$content = "";
?>
<html>
<body>
<?php
$content .= "<form method=\\"post\\" action=\\"\\" name=\\"categoryform\\">
Car <input type=\\"radio\\" name=\\"category\\" value=\\"car\\" onclick=\\"document.categoryform.submit ();\\"$cat_car />
Truck <input type=\\"radio\\" name=\\"category\\" value=\\"truck\\" onclick=\\"document.categoryform.submit ();\\"$cat_truck />
Van <input type=\\"radio\\" name=\\"category\\" value=\\"van\\" onclick=\\"document.categoryform.submit ();\\"$cat_van /><br />

<select name=\\"vehicle\\">\\r";

if (isset($_POST['category'])){
	foreach($vehicles[$category] as $vehicle){
		$content .= "<option value=\\"$vehicle\\">$vehicle</option><br />\\r";
	}
}else{
	$content .= "<option value=\\"\\">Choose Model</option><br />\\r";
}
$content .= "</select>
<input type=\\"submit\\" name=\\"submit_choice\\" value=\\"Submit\\" />
</form>\\r";

if (isset($content)){ echo "$content";}
?>
</body>
</html>

I’ve made a PHP GUI API, you may want to try it out if you wish to implement your view layer in PHP rather than HTML. Note this version is not namespaced, so you may end up experiencing some naming conflicts with your own classes:
http://www.phpclasses.org/package/7857-PHP-Render-HTML-pages-composed-programmatically.html

Thank you all for all ur help !! I got it !!! Thanks