Get data from Selection List

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmallia
Forum Commoner
Posts: 25
Joined: Sat Nov 19, 2011 3:18 pm

Get data from Selection List

Post by dmallia »

I made this form.

Code: Select all

<form method="post" enctype="multipart/form-data">
	<select name="file" style="width: 150px">
		<option value="1">pbsv.cfg</option>
		<option value="2">server.cfg</option>
	</select>
	
	<input type="submit" name="load" value="Load">
</form>
now using php I am going to set a variable named $selected and in that variable I want to store the value that the user selects. Does anyone knows how I can get the selected data?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Get data from Selection List

Post by califdon »

What PHP script will receive the data from this form? You haven't provided an action= parameter, so your code will default to calling the same script again. If this is what you intend, then your script will have to have some code to determine whether it should just display the form (the first time it is called) or whether it should look for user form data and take some action based on that. You should probably read some tutorials on using POST variables, such as http://www.html-form-guide.com/php-form ... -post.html. Basically, the script that handles the form data will find the value selected in the $_POST array, using the index "file" (the name you have assigned to the select element of your form).
dmallia
Forum Commoner
Posts: 25
Joined: Sat Nov 19, 2011 3:18 pm

Re: Get data from Selection List

Post by dmallia »

so i made this script.

Code: Select all

<?php
	$fn = "test.txt";
	
	if (isset($_POST['content']))
		{
			$content = stripslashes($_POST['content']);
			$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
			fputs($fp,$content);
			fclose($fp) or die ("Error closing file!");
		}
?>
and under that i have this form

Code: Select all

<form  method="post" enctype="multipart/form-data">
	<textarea name="content"><?php readfile($fn) ?></textarea><br />
	<input type="submit" name ="save" value="Save"> 
</form>
now i created a drop down list

Code: Select all

<form method="post" enctype="multipart/form-data">
	<select name="file" style="width: 150px">
		<option>test1.txt</option>
		<option>test2.txt</option>
	</select>
	<input type="submit" name="load" value="Load"></a>
</form>
and would like to add it to the php script above, so I can choose the file i want to edit.
Post Reply