Weird result in PHP form

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
bukwus
Forum Newbie
Posts: 6
Joined: Thu Dec 17, 2009 2:01 pm

Weird result in PHP form

Post 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
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Weird result in PHP form

Post 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.
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Weird result in PHP form

Post by roders »

how about if you do this

Code: Select all

if(isset($_POST['submit']))
{
   echo $_POST['choice1'];
}else{
  echo "Choose One";
}
bukwus
Forum Newbie
Posts: 6
Joined: Thu Dec 17, 2009 2:01 pm

Re: Weird result in PHP form

Post 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();
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Weird result in PHP form

Post 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?
bukwus
Forum Newbie
Posts: 6
Joined: Thu Dec 17, 2009 2:01 pm

Re: Weird result in PHP form

Post 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.
Post Reply