How do I save and echo dropdown value in PHP?

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.

Basic example:

      <form method="post">
         <select name="sort_by">
            <option>Select Option</option>
            <?php
               $array = ['id' => 'ID', 'name' => 'Name', 'amt' => 'Amount', 'status_filter' => 'Status'];
               foreach ($array as $key => $value)
                   {
                   $selected = isset($_POST['sort_by']) && $_POST['sort_by'] == $key ? 'selected' : '';
                   echo "<option value='$key' $selected>$value</option>\n";
                   }
               ?>
         </select>
         <input type="submit" value="Submit">
      </form>

select elements don’t take a value attribute. The option tags take a selected attribute for the one that is to be selected.

You have hardcoded your select to choose “Cat”, rather than using the PHP value to determine which option should be selected.

Hello benanamen, I just added my try catch block that I am using.

Hello m_hutley,
Thanks for your help. How would I correct this problem?

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.

I am at work. I read the posts from bottom to top. Sorry if you were offended. I will try it when I am in front of my laptop. And yes, I do know HTML.

Try Fix it this way.


<select id="species" name="species" title="select species">
	<option selected="selected"><?php if(isset($_POST['species']) {echo $_POST['species’];}?></option>
	<option value="dog">Dog</option>															
	<option value="raccoon">Raccoon</option>
	<option value="fox">Fox</option>
	<option value="bear">Bear</option>
	</select>

Thats where you missed it like @m_hutley and @benanamen did pointed out, your default selection is cat

That wont work. Now there is no Cat option and you just created an undefined index error when the page loads.

You are right, he needs to merge it with an if(isset($_POST[‘species’]) { echo $_POST[‘species’];}

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.

Okay, noted, i usually use !empty though.

6 posts were split to a new topic: Getting things wrong

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.