0

I have used PHP array for HTML select, then I put my codes in a template called data.php. I linked the data.php into index.php. so I want to define a variable for my HTML select <select name="color"> in order that I can call this <select name="color"> with its defined variable all across my theme.

$color=array(
     "R" => 'red',
     "Y" => 'yellow',
     "B" => 'blue', 
     "G" => 'green',
     "P" => 'purple',
     "O" => 'orange', 
     "B" => 'black',
     "G" => 'gray'
           );

<select name="color">
   <option value="">-----------------</option>
  <?php
  foreach($color as $key => $value):
  echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
  endforeach;
   ?>
</select>
1
  • 1
    use jquery/javascript to fetch option selected and then pass value to the php file via $.ajax if u wish to use jquery Commented Jun 18, 2012 at 6:13

1 Answer 1

2

If I have understood your question correctly, is this what you need ?

<?php
  $ColorOtions = "<option value=''>...</option>"; 
  foreach($color as $key => $value):
  $ColorOtions .= '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
  endforeach;
?>

Now you can call as <select name="color">echo $ColorOtions;</select>

Hope that helps..

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. it should be <select name="color"><?php echo $ColorOtions; ?></select> exactly. do you know how can I array fonts from Google Fonts like colors???

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.