bukwus Posted April 13, 2010 Share Posted April 13, 2010 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: 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: <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 Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/ Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 Did you ever change the value of $value before it gets printed out? Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041048 Share on other sites More sharing options...
aeroswat Posted April 13, 2010 Share Posted April 13, 2010 You did not define $value as an array before you assigned values to it. It thinks it is a string or character array maybe? I believe it is also better to assign it like this $value[] = "Choose one..." Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041052 Share on other sites More sharing options...
bukwus Posted April 13, 2010 Author Share Posted April 13, 2010 You did not define $value as an array before you assigned values to it. It thinks it is a string or character array maybe? I believe it is also better to assign it like this $value[] = "Choose one..." Should this work, or am I incorrectly defining $value as an array? $value = array(); if(isset($_POST['submit'])){ $value[1] = $_POST['choice1']; $value[2] = $_POST['choice2']; $value[3] = $_POST['choice3']; } else { $value[] = 'Choose one...'; } Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041088 Share on other sites More sharing options...
aeroswat Posted April 13, 2010 Share Posted April 13, 2010 You should be able to do this and it will automatically assign key values 0,1,2,etc for the stuff $value = array(); if(isset($_POST['submit'])){ $value[] = $_POST['choice1']; $value[] = $_POST['choice2']; $value[] = $_POST['choice3']; } else { $value[] = 'Choose one...'; } Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041096 Share on other sites More sharing options...
bukwus Posted April 13, 2010 Author Share Posted April 13, 2010 You should be able to do this and it will automatically assign key values 0,1,2,etc for the stuff $value = array(); if(isset($_POST['submit'])){ $value[] = $_POST['choice1']; $value[] = $_POST['choice2']; $value[] = $_POST['choice3']; } else { $value[] = 'Choose one...'; } Okay, here's what it looks like now: <a name="result"></a> <?php $value = array(); if(isset($_POST['submit'])){ $value[] = $_POST['choice1']; $value[] = $_POST['choice2']; $value[] = $_POST['choice3']; } else { $value[] = 'Choose one...'; } ?> And the form: <form action="page-name.html#result" method="POST"> <select name="choice1"> <option>'.$value[0].'</option> <option value="Addressed">Addressed</option> <option value="Does not apply">Does not apply</option> <option value="Needs attention">Needs attention</option> </select> I made sure the first $value key was 0. When I fill out the form and submit, the values carry over just fine. Unfortunately, when you first come to the page, only the first select box is populated with "Choose one...". The others are blank. Any thoughts why? Thank you Andy Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041186 Share on other sites More sharing options...
aeroswat Posted April 13, 2010 Share Posted April 13, 2010 You need to write $value[] = 'Choose one...'; for each instance Link to comment https://forums.phpfreaks.com/topic/198399-weird-result-in-php-form/#findComment-1041229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.