How about this:
<?php
$arr = ["Cat", "Dog", "Cow" ];
if( $_POST['animal']){
$animal=$_POST['animal'];
echo $animal;
}
?>
<form name="f" id="a" method="post" action="">
<select id="animal" name="animal" onchange="this.form.submit()" >
<option value="0">--Select Animal--</option>
<?php
foreach ($arr as $a){
if($a == $animal){
echo "<option value="{$a}" selected >$a</option>";
}else{
echo "<option value="{$a}" >$a</option>";
}
}
?>
</select>
</form>
Note you may also unset
the variable at the end but garbage collector is also there in PHP. This code assumes you use at least PHP 5.4+, else define te array via $arr = array("Cat", "Dog", "Cow" );
Related Posts:
- How to add a placeholder to the protected post password input
- Back button not working after input / search submit
- How to call or add password input / generate password / password strenght meter in custom registration form?
- Remove active cursor from form field
- Passing input value into name of input
- Dropdown List Won’t Display Selected Option After Submit / Refresh
- Add options to WordPress Blocks form dropdown field
- How to define form action in JSF?
- Input Type URL – Says “Please enter a URL” if HTTP is not included
- How to display user registration form on front-end of the website?
- Creating a contact form without a plugin [closed]
- How to submit data from HTML form?
- Can I verify nonce which was generated on a different WP site?
- Sending form data via PHPMailer – How to action PHP script from a form
- how to handle forms in multiple pages?
- Conditional two level dropdown filter for custom post type
- Dynamic dropdown custom post list based on dropdown selection
- recommended practice for form submission
- Built-in data validation function for URLs
- Gravity Forms skip form if already filled out using cookie?
- One comment per user per post
- Trying to save and display a wp_editor()
- Contact forms going into spam folder
- Performing a POST action on homepage goes to posts page
- creating form for wp_remote_post
- How to know what submit button the user clicked?
- CRUD front end for mySQL in WordPress
- How to edit custom user meta information front end
- Google Map Latitude and Longitude values in form
- How to properly add a custom submenu to the user’s admin menu
- How can I find actual logic php file in form’s action path?
- How do I use the info submitted in a form to create a form reception page? (like “thank you {firstName}”) [closed]
- WordPress form to shortcode
- Create Contact7 Form programmatically [closed]
- recaptcha working on local but not on live wordpress sites [closed]
- User input form field value validation ninja forms 3
- Can you disable the mail function in contact form 7 [closed]
- Using form parameters within a WordPress “Page”
- Can I make Contact Form 7 change over to a new page? [closed]
- A mandatory agreement form to access another page?
- placeholder text in category form label
- Creating short code for search form
- nonce in custom form is not verifying
- textarea field is getting escaped for some unknown reason
- Use $_POST data in functions.php
- Where to add css file that I want my forms to use?
- Downloadable content only for subscribers?
- How to Collect Keyword and Search Engine Data on Webforms?
- free form wordpress
- Form submission in WordPress front end
- form action wordpress and php
- Show button once Contact form 7 is submitted
- WordPress Emails & Contact Forms [closed]
- How to move HTML form to WP Theme
- Is there a reset_button()?
- How can I modify labels in default wordpress registration form? [duplicate]
- Display number of sign-ups [closed]
- Contact form submit, having to click twice
- Form within thickbox
- Restore Cforms II Form Presets
- Making an input field required from WP’s perspective
- Contact Form 7 “non-selectable” options in a drop down [closed]
- How to take large file uploads from users the right way
- Cannot modify header form resubmits data on refresh
- wordpress forms submit
- wordpress blog, is there a way to make anyone post in a blog without having login info
- Exclude category from drop down list form
- gravityform – retrieve multiple check answers “keys” instead of “values”
- Code for front end validation for forms not working
- using esc_html_e string in custom form using code snippets
- How do I implement form handling when form is custom HTML?
- Forms cut off when on mobile
- How to create a custom range slider in wordpress form?
- Sorting wordpress data in excel
- Quickly Maintain/edit 100+ WordPress Forms on multiple websites
- Pricing depending on selected items in a multi step form
- Prevent malicious scripts to be submitted in post
- Sending Form data as Attachment
- Trigger action when submitting form with WPForms [closed]
- Form submissions that require users ID# [closed]
- Unable to submit form using admin post wordpress
- If input field has value Require another field – PHP
- Form data from wordpress theme to be submitted in other site url
- wp_nonce for Front-End submission form not working
- Pay before posting (frontend insert post)
- admin_post in object oriented plugin
- Simple email input store in database
- Contact form spam, without form?
- How to use the in WP
- How to send form values from the script in functions.php and not from the form on my website?
- How do I capture the selected option and pass in sending the registration form?
- Issue with contact form 7
- Advanced searching form
- Cannot access a file in the theme (twentynineteen-child) folder
- How do I register user without being authenticated
- How to add or double field?
- How to manage to submit WPForms at our webpage
- Adding default quicktags to textarea
- How can I get lost form data back? [closed]
- Forminator + Hubspot Workflows