Jump to content

echo/print form input name instead of the value


Marcos01

Recommended Posts

Hello, I would like to know if it is possible to print/echo the name from

a input type radio instead of the value.

 

I am trying to do it with this simple form:

 

$A='0101';

print($_POST['\"$A\"']);


echo 	'<form id="form " name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo 	'<label>question ';
echo	'<input type="radio" name=\"$A\" id="q1" value="10" />TEST</label>';
echo   	'<input name="next" type="submit" /> </form>';

You can loop through and echo out the names of the $_POST array.

 

foreach($_POST as $key => $value){
echo $key . "<br />";
}

 

You can also use array_keys to make a new array out of the keys of an existing array.

 

$keys = array_keys($_POST);

 

But why? You can't do this another way?

Hello ProjectFear, thanks for your help.

 

With the foreach code I get more data then I need. It also gives me the name of the button.

 

I have questionnumber and page in the name and the answer in the value.

 

No clue if it can be done differently.

It could, if its a multiple choice quiz you could do something like this:

 

<?php
$answers = array("a","c"); //An array of your answers, in order.

if(isset($_POST['submit'])){
if(count($_POST['question']) != count($answers)){
	//They didn't answer all the questions!
	echo "Sorry you didn't answer all the questions.";
}else{
	//Create 2 arrays, one can store the correct answers and one can store the incorrect. Of course you can just use one array for this and make it store another array.
	$correct = array();
	$wrong = array();

	//Begin a loop through all the questions, $question is the question number and $answer contains either a, b or c (you could make this go up to anything).
	foreach($_POST['question'] as $question => $answer){
		//If $answer is the same as the answer specified in our $answers array.
		if($answer == $answers[$question]){
			//Add it to the correct array.
			$correct[] = $question;
		}else{
			//They got it wrong, add it to the wrong array.
			$wrong[] = $question;
		}
	}

	//Show the results to the user.
	echo "Thankyou for doing the quiz. Here are your results:<br />";
	echo "You got <strong>" . count($correct) . "</strong> questions right and <strong>" . count($wrong) . "</strong> questions wrong!<br /><br />";
}
}

echo <<<html
<form action="" method="post">
Question 1: Answer is A.<br />
<input type="radio" name="question[0]" value="a" />A<br />
<input type="radio" name="question[0]" value="b" />B<br />
<input type="radio" name="question[0]" value="c" />C<br />
<br />
Question 2: Answer is C.<br />
<input type="radio" name="question[1]" value="a" />A<br />
<input type="radio" name="question[1]" value="b" />B<br />
<input type="radio" name="question[1]" value="c" />C<br />
<input type="submit" name="submit" value="Go!" />
</form>
html;

?>

 

Why are you putting the question number and page in the same string?

Archived

This topic is now archived and is closed to further replies.

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