Page 1 of 1

Help with submitting data from radio buttons

Posted: Wed Jan 19, 2005 12:04 pm
by pthomas
Yet another item im stuck on....

First, here's the code

Code: Select all

echo "<h3>Add/Drop into the $name category</h3>\n";
		echo "<table name="$name" border=1 >\n";
		echo "<form action="review.php?amt=$counter" method=get>\n";
		$counter = 0;
		foreach ($items AS $item)
		&#123;
			$item = trim($item);			//Remove newline char
			echo "<tr><td>$item</td>\n";
			echo "<td><INPUT TYPE=RADIO NAME="" . $name . $counter . "" VALUE="add" 	CHECKED	>Add</td>\n";
			echo "<td><INPUT TYPE=RADIO NAME="" . $name . $counter . "" VALUE="drop"		>Drop</td>\n";
			echo "<td><INPUT TYPE=RADIO NAME="" . $name . $counter . "" VALUE="pass"		>Pass</td>\n";
			echo "</tr>\n";
			$counter++;
		&#125;
		echo "</table><br />\n";
		echo "<input type="submit" name="submit" value="Submit">\n";
		echo "</form>\n";
		echo "<br />";
This creates a nice table with items listed and beside each item is the option to either add, remove, or pass on each item. I'm trying to figure out how I can send the info to the next page of which item is set to what.

Any ideas?
Paul

Posted: Wed Jan 19, 2005 12:09 pm
by feyd
you could zip through the select that generated this list matching them back up.. or place the id information into the name of the radio like so:

Code: Select all

<input type="radio" name="' . $name . '&#1111;' . $item&#1111;'id'] . ']"' value="add" />
that's if this stuff is coming from a database..

Posted: Wed Jan 19, 2005 12:23 pm
by pthomas
Yeah, everything is coming from the database. I'm not sure exactly what you mean though!

I've been working with PHP for about 3 weeks now and ended up giving myself a crash course in HTML near the end of Dec....my background is c++. So most of this stuff is kinda hard for me to follow still.

So each name of the radio buttons need to be different, that much I understand. What I'm lost on is how to pass that data to the next page. Is the method you're using putting the data into an associative array? Is that where the

Code: Select all

$item&#1111;'id']
comes into play?

So if there were 3 names, timmy, bob, jessica the array would contain this after pressing submit:
$item['timmy'] = add
$item['bob'] = drop
$item['jessica'] = add
?

Can you feel the newbie in the text?

Thanks for the help so far,
Paul

Posted: Wed Jan 19, 2005 12:30 pm
by feyd
it's similar to that, but doesn't use the name as the index, just the id.. the $name is from elsewhere in your code, I didn't know specifically where that was coming from, and didn't want to alter that logic. You could infact name the radio buttons the same base name, such as 'items' and as long as the bracketted value (posted index) is different among the groups, you will get an array of all the selections on the other end, using the index you chose.

for instance, if you wrote this as final html

Code: Select all

<input type="radio" name="items&#1111;34]" value="add" />
<input type="radio" name="items&#1111;34]" value="drop" />
<input type="radio" name="items&#1111;34]" value="pass" />
and the user selected.. pass.. then you can expect $_POST['items']['34'] = 'pass' on the processing page. (using post method in this instance)

Posted: Wed Jan 19, 2005 1:23 pm
by pthomas
Just got done with a session of lunch and thinking, I'm going to try to code what you suggested... I'll post back in bit!

Paul

Posted: Thu Jan 20, 2005 4:03 pm
by pthomas
I've been messing with the $_POST global and just can;t get any info from it.

I've got a few dynamically created buttons and all I want is to be able to click on a button, reload the page and then be able to find out by accessing that $_POST item if it was set or not.

Here's how my buttons are created

Code: Select all

function category_buttons(& $filename)
&#123;
	echo "<FORM ACTION="review.php" METHOD="post">\n";
	echo "<TABLE ALIGN=CENTER BORDER=0 CELLPADDING=5>\n";
	echo "<TR>\n";
	foreach ($filename AS $item => $file)
	&#123;
		if (file_exists($file))			//If the file exists, give the darn thing a button!
		&#123;
			echo "<TD><INPUT TYPE="submit" name="category" value="$item"></TD>\n";
		&#125;
	&#125;
	echo "</TR></TABLE></FORM>\n";
&#125;
So consider the buttons to be called animals, people, food. If I click on the animals button, I just want the $_POST['animals'] to be able to be picked up as set when the page reloads. User clicks on the button, reloads the same page with

Code: Select all

if (isset($_POST&#1111;'animals']))
equal to true.

Right now, I'm not getting squat. Here's the chunk of code that should be able to pickup if the $_POST var was set

Code: Select all

function review_items(& $filename, & $num_files)
&#123;							//User gets to play God
	if ($num_files > 1)
	&#123;
		echo "<h3 align=center>Select an category to judge</h3>\n";
		category_buttons($filename);		//Create category buttons
	&#125;
	
	if (isset($_POST&#1111;'category']))			//A cateory has been selected
	&#123;
		$category = $_POST&#1111;'category'];
		echo "<p align=center>Currently judging "" . $category . ""</p>\n";
		gen_table_item($category, $filename&#1111;$category]);
	&#125;
	else
	&#123;
		echo "<p align=center>No category currently selected</p>\n";
	&#125;
&#125;
What am I doing wrong?!
Paul

Posted: Thu Jan 20, 2005 4:08 pm
by pthomas
I meant to say that

Code: Select all

if (isset($_POST&#1111;'category']))
is evaluated to true.

Paul

Posted: Thu Jan 20, 2005 4:10 pm
by pthomas
omg! it works! omg!

My forgetting to hit the save button before testing is really biting me in the arse!

Paul

Posted: Thu Jan 20, 2005 4:10 pm
by feyd
so you're getting "No category currently selected" ?

what does adding this in that area show you?

Code: Select all

echo '<pre>' . htmlentities(var_export(array('request method' => $_SERVER&#1111;'REQUEST_METHOD'], 'POST'=>$_POST, 'GET'=>$_GET), true), ENT_QUOTES) . '</pre>';


edit: :lol::lol: that helps ;)