0

I'm using the Simple Fields plugin for a custom WordPress theme. I have the code below to create a dropdown for a size selector, pulling from the options that the admin has created:

<select>
<?php if($sizetotal > 0 && $sizes[0] != 0){?>
<?php for($x = 0; $x < $sizetotal; $x++){?>
    <option value="<?php echo $sizes[$x]; ?>"><?php echo $sizes[$x]; ?></option>
<?php }?>
<?php }?>
</select>

This is working fine, but I'm wondering how I can echo $sizes[$x]; again, outside this loop so that it reflects what the user chose (in a form, for example).

Very new to PHP, so any help would be much appreciated.

Thanks

5 Answers 5

1
  1. You should put the name to the <select> tag. Ex:

    <select name="sizeSelector">
    
  2. You can only see the user's chosen value after user submit the form. To read the value use:

    echo $_POST['sizeSelector']
    

    or

    echo $_GET['sizeSelector']
    

    $_GET or $_POST is depends on the method of your form.

The final code shoul be like this:

<?php
if (isset($_POST['sizeSelector'])){
  echo "<div>User chosed the {$_POST['sizeSelector']} size.</div>";
}
?>
<form action="" method="POST">
  <select name="sizeSelector">
   <?php if($sizetotal > 0 && $sizes[0] != 0) {
     for($x = 0; $x < $sizetotal; $x++) {
   ?>
     <option value="<?php echo $sizes[$x]; ?>"><?php echo $sizes[$x]; ?></option>
   <?php }
   } ?>
  </select>
  <input type="submit">
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

Not ever having wordpress or this plugin, a google search returns this within 5 seconds:

<?php
  // Get the saved value for a field called "video"
  $video_url = simple_fields_value('video');
?>

1 Comment

@blicht454: Your question was "what the user chose". And that's exactly what I posted.
0

You can get to use the value when the user submit the form using the following

<?php
     Echo $_POST["optionName"];
?>

Assuming the method on your form. All form sent using post method values are stored in a super global variable called $_POST. So you can easily retrieve your value from that array using the element name. You should have the name within the element.

Comments

0

You can do this Using JQuery like this

For single select dom elements, to get the currently selected value:

var selected = $('#dropDownId').val();

To get the currently selected text:

var selected = $('#dropDownId :selected').text();

Or

var selected = $("#dropDownId option:selected").text();

Then to view the value

alert(select);

to know more visit :http://jquery.com/

Comments

0

I suppose you want to get the value of the selected option to display it elsewhere in your page. In this case you can use javacsript instead :

<select onChange="displaySelectedValue(this);">
...
</select>

<script>
function displaySelectedValue(select){
alert(select[select.selectedIndex].value);
};
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.