Page 1 of 1

Weird result in PHP form

Posted: Tue Apr 13, 2010 11:12 am
by bukwus
Hi
I'm beginning a form that asks questions and has a drop down list of options to choose from. I'm going to use PHP to check for errors, loops to cut down on repetition, etc., but right now I'm just testing code at a basic level.
Here's the PHP:

Code: Select all

if(isset($_POST['submit'])){
		$value[1] = $_POST['choice1'];
		$value[2] = $_POST['choice2'];
		$value[3] = $_POST['choice3'];
	}
	else {
		$value[1] = 'Choose one...';
		$value[2] = 'Choose one...';
		$value[3] = 'Choose one...';
	}
Here's the basic form element:

Code: Select all

<select name="choice1">
		<option value="0">'.$value[1].'</option>
    <option value="Addressed">Addressed</option>
    <option value="Does not apply">Does not apply</option>
    <option value="Needs attention">Needs attention</option>
    </select>
My problem is that the text displayed in the select box is only the first letter of whatever it's supposed to be. For example: if it's the first time visiting the form, each box only has "C" in it instead of "Choose one..." OR if "Does not apply" was chosen and the form is submitted, "D" is all that shows up in that box.

Any idea what's happening here?

Many thanks,
Andy

Re: Weird result in PHP form

Posted: Tue Apr 13, 2010 11:22 am
by minorDemocritus
Please use [ syntax=php ] for your code blocks... syntax highlighting is nice :mrgreen:

How are you handling the form HTML itself? Please post more of the surrounding code.

Re: Weird result in PHP form

Posted: Tue Apr 13, 2010 11:49 am
by roders
how about if you do this

Code: Select all

if(isset($_POST['submit']))
{
   echo $_POST['choice1'];
}else{
  echo "Choose One";
}

Re: Weird result in PHP form

Posted: Tue Apr 13, 2010 11:57 am
by bukwus
minorDemocritus wrote:Please use [ syntax=php ] for your code blocks... syntax highlighting is nice :mrgreen:

How are you handling the form HTML itself? Please post more of the surrounding code.
Sorry about that. Here's the code:

Code: Select all

<a name="result"></a>
if(isset($_POST['submit'])){
      $value[1] = $_POST['choice1'];
      $value[2] = $_POST['choice2'];
      $value[3] = $_POST['choice3'];
   }
   else {
      $value[1] = 'Choose one...';
      $value[2] = 'Choose one...';
      $value[3] = 'Choose one...';
   }
Here's the form:

Code: Select all

<form action="page-name.html#result" method="POST">
<select name="choice1">
		<option value="0">'.$value[1].'</option>
    <option value="Addressed">Addressed</option>
    <option value="Does not apply">Does not apply</option>
    <option value="Needs attention">Needs attention</option>
    </select>
</form>
It was suggested in another forum that I define $value as an array first. Would the following be correct?

Code: Select all

$value = array();

Re: Weird result in PHP form

Posted: Tue Apr 13, 2010 12:22 pm
by minorDemocritus
minorDemocritus wrote:How are you handling the form HTML itself? Please post more of the surrounding code.
I want to know how the form is being handled. Is it a static HTML page, is it HTML inside a PHP script, or is it inside a string that then gets echoed?

Re: Weird result in PHP form

Posted: Tue Apr 13, 2010 1:49 pm
by bukwus
minorDemocritus wrote:
minorDemocritus wrote:How are you handling the form HTML itself? Please post more of the surrounding code.
I want to know how the form is being handled. Is it a static HTML page, is it HTML inside a PHP script, or is it inside a string that then gets echoed?
It's HTML that is echoed.