Hello,
I have a PHP class that handles my form input. I am able to successfully save text fields into PHP and echo them back. I am not able to echo back select/option dropdown values nor echo them back. I am hoping someone can spot my problem and help me to fix my error. I will attach some code. If you can help I would greatly appreciate your help. Thanks
type or paste code here
PHP:
<?php
class Input {
public static function exists($type = 'post') {
switch($type) {
case 'post':
return (!empty($_POST)) ? true : false;
break;
case 'get':
return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}
}
public static function get($item) {
if(isset($_POST[$item])) {
return $_POST[$item];
} else if(isset($_GET[$item])) {
return $_GET[$item];
}
return '';
}
}
HTML:
//This one works
<li>
<label for="name" >Name</label>
<p><input type="text" id="name" value="<?php echo escape(Input::get('name')); ?>" name="name"></p>
</li>
//This one does not work
<li>
<label for="species">Species</label>
<select id="species" value="<?php echo escape(Input::get('species')); ?>" name="species" title="select species">
<option value="cat" selected="selected">Cat</option>
<option value="dog">Dog</option>
<option value="raccoon">Raccoon</option>
<option value="fox">Fox</option>
<option value="bear">Bear</option>
</select>
</li>
try {
$user->create(array(
'species' => Input::get('species')
));
} catch(Exception $e) {
die($e->getMessage());
}
You have no code that checks the POST result in your Html Select on top of what @m_hutley said as well. Your class in general leaves a bit to be desired.
And? What does that have to do with the posted problem? I gave you a simple working example that you did nothing with.
Just wondering, did you even write any of that code? I ask because you have demonstrated that you don’t even understand basic HTML and then still ask for an answer when one is staring you right in the face in this thread.
Sorry, wrong again. In a properly designed/submit from the form, the form elements will ALWAYS be isset. The only exception is checkbox’s. I already provided the answer in post #2.