html select dropdown wont stay selected - html

I have a select dropdown list on my website(www.irishbonus.comule.com/en/). However when I select an option from the dropdown list and press submit I would like that the option stay selected.
Here is the code for the select:
<form action="" method="post">
<strong> Select Subject:</strong>
<select name="formSubject" class="dropdown">
<option value=">>>">">>>"</option>
<option value="Accounting">Accounting</option>
<option value="Agricultural Science">Agricultural Science</option>
<option value="Agricultural Economics">Agricultural Economics</option>
<option value="Applied Mathematics">Applied Mathematics</option>
<option value="Arabic">Arabic</option>
<option value="Art (jc only)">Art (jc only)</option>
<option value="Biology">Biology</option>
<option value="Business (jc only)">Business (jc only)</option>
<option value="Business Studies">Business Studies</option>
<option value="Chemistry">Chemistry</option>
<option value="Civic (jc only)">Civic (jc only)</option>
<option value="Classical Studies">Classical Studies</option>
<option value="Construction Studies">Construction Studies</option>
//more options
<option value="Typewriting (jc only)">Typewriting (jc only)</option>
</select>
<table width="300px">
<tr>
<td valign="top">
<strong>Insert Mark:</strong>
<input type="text" name="formMark" maxlength="2" size="4" value="<?=$mark;?>"/>
</td>
</tr>
</table>
<input type="submit" />
</form>

You need to use a server-side language for this. You already seem to be using PHP so just add some PHP code to do it. The best solution would be storing your options inside an array and then iterating over the array, outputting the <option> tags and adding the selected attribute if the value matches the submitted value.
<select name="formSubject" class="dropdown">
<?php
$options = ['Accounting', 'Agricultural Science', '...'];
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>

He forgot the array(), that is why the parse error is there
here:
<select name="formSubject" class="dropdown">
<?php
$options = array('Accounting', 'Agricultural Science', '...');
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>

while ($row = $result->fetch_assoc()) {
$selected = "";
if(isset($_GET['spec'])) {
if($_GET['spec'] == $row['id']) {
$selected = "selected='selected'";
}
}
print "<option ". $selected ." value='". $row['id'] ."'>". $row['ime']. "</option>";
}
$result->free();

Related

How can I use the output of a shortcode inside of another shortcode in Wordpress?

I have written a shortcode that returns a variable that was set in my php file, and I need to use the value of that variable in another shortcode which I got from a plugin, "Insert Pages". I understand that I cannot do [insert page='[left]' display='content'] because the Wordpress parser ends the shortcode at the first ], but I am wondering if there is a way around this.
Here is the code of my page, apologies for the bad indentation. The shortcodes from my php file are [right] and [left] which return a number corresponding to the value of the option the user chose. The other shortcode is from the Insert Pages plugin and takes the desired ID as the value for which page to show.
The code in question is below the first 'select' tag. 143380 is an example of a value, that is where I need the result of my shortcode to go.
<form id="form" action="" method="get">
[one_half]
<h3>Select a Country</h3>
<div class="dropdown"><select id="dropdownl" name="dropdownl">
<option disabled="disabled">Country</option>
<option disabled="disabled">North America</option>
<option value="62292">Canada</option>
<option value="72808">Mexico</option>
<option value="144586">Puerto Rico</option>
<option value="163668">United States</option>
<option disabled="disabled">South America</option>
<option value="63839">Argentina</option>
<option value="67922">Brazil</option>
<option value="78702">Colombia</option>
<option value="60670">Peru</option>
<option disabled="disabled">Oceania</option>
<option value="32137">Australia & New Zealand</option>
<option disabled="disabled">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="disabled">Asia</option>
<option value="66137">China</option>
<option value="49932">India</option>
<option value="40308">Israel</option>
<option value="78709">Japan</option>
<option value="223252">Philippines</option>
<option value="60438">Republic of Korea</option>
<option value="69605">Singapore</option>
<option value="70391">Taiwan</option>
<option value="71971">Thailand</option>
<option disabled="disabled">Europe</option>
<option value="32142">Austria</option>
<option value="32146">Belgium</option>
<option value="78707">Bulgaria</option>
<option value="72763">Czech Republic</option>
<option value="31277">Denmark</option>
<option value="62393">Finland</option>
<option value="31223">France</option>
<option value="31026">Germany</option>
<option value="63208">Greece</option>
<option value="63016">Hungary</option>
<option value="183519">Ireland</option>
<option value="122492">Italy</option>
<option value="142792">Lithuania</option>
<option value="162411">Netherlands</option>
<option value="67164">Norway</option>
<option value="56320">Poland</option>
<option value="62344">Portugal</option>
<option value="67210">Romania</option>
<option value="68895">Russian Federation</option>
<option value="143380">Serbia</option>
<option value="78705">Slovakia</option>
<option value="25372">Spain</option>
<option value="25520">Sweden</option>
<option value="25408">Switzerland</option>
<option value="67375">Turkey</option>
<option value="63212">Ukraine</option>
<option value="25482">United Kingdom</option>
</select></div>
[left]
[insert page='143380' display='content']
[/one_half]
[one_half_last]
<h3>Select a Country</h3>
<div class="dropdown">
<select id="dropdownr" name="dropdownr">
<option disabled="disabled">Country</option>
<option disabled="disabled">North America</option>
<option value="62292">Canada</option>
<option value="72808">Mexico</option>
<option value="144586">Puerto Rico</option>
<option value="163668">United States</option>
<option disabled="disabled">South America</option>
<option value="63839">Argentina</option>
<option value="67922">Brazil</option>
<option value="78702">Colombia</option>
<option value="60670">Peru</option>
<option disabled="disabled">Oceania</option>
<option value="32137">Australia & New Zealand</option>
<option disabled="disabled">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="disabled">Asia</option>
<option value="66137">China</option>
<option value="49932">India</option>
<option value="40308">Israel</option>
<option value="78709">Japan</option>
<option value="223252">Philippines</option>
<option value="60438">Republic of Korea</option>
<option value="69605">Singapore</option>
<option value="70391">Taiwan</option>
<option value="71971">Thailand</option>
<option disabled="disabled">Europe</option>
<option value="32142">Austria</option>
<option value="32146">Belgium</option>
<option value="78707">Bulgaria</option>
<option value="72763">Czech Republic</option>
<option value="31277">Denmark</option>
<option value="62393">Finland</option>
<option value="31223">France</option>
<option value="31026">Germany</option>
<option value="63208">Greece</option>
<option value="63016">Hungary</option>
<option value="183519">Ireland</option>
<option value="122492">Italy</option>
<option value="142792">Lithuania</option>
<option value="162411">Netherlands</option>
<option value="67164">Norway</option>
<option value="56320">Poland</option>
<option value="62344">Portugal</option>
<option value="67210">Romania</option>
<option value="68895">Russian Federation</option>
<option value="143380">Serbia</option>
<option value="78705">Slovakia</option>
<option value="25372">Spain</option>
<option value="25520">Sweden</option>
<option value="25408">Switzerland</option>
<option value="67375">Turkey</option>
<option value="63212">Ukraine</option>
<option value="25482">United Kingdom</option>
</select></div>
[right]
[/one_half_last]
<input type="submit" value="Submit" />
</form>
This is my template php file that I wrote the shortcode in. They are defined in left_shortcode and right_shortcode. The file is based off my theme's page.php:
<?php
/*
Template Name: Deven Template
*/
?>
<?php
/**
* The template for displaying pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of paages and that
* other "pages" on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
get_header(); ?>
<?php
$left = "";
$right = "";
?>
<?php
if(isset($_GET['dropdownl']) and isset($_GET['dropdownr'])) {
global $left, $right;
$left = $_GET['dropdownl'];
$right = $_GET['dropdownr'];
//echo $left;
}
?>
<?php
function left_shortcode( $atts, $content = null ) {
global $left; // if $unique is global var add this line too
return $left;
}
add_shortcode( 'left', 'left_shortcode' );
?>
<?php
function right_shortcode( $atts, $content = null ) {
global $right; // if $unique is global var add this line too
return $right;
}
add_shortcode( 'right', 'right_shortcode' );
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) :
the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This answer was given to me on reddit:
"It may be possible to get nested shortcode to work, but you would probably need to process the content twice or something and I would imagine it isn't going to be straightforward.
I think the easiest thing to do would just create a new shortcode as a wrapper and call the original shortcode in it.
So, if the original looks like this: [original_shortcode page=1]
You could create one like this: [original_shortcode_new page_alignment='left']
Then write a shortcode, escape/validate user input, and call the main shortcode.
Essentially:
function original_shortcode_new( $args ) {
$args = shortcode_atts(
array(
'page_alignment' => ''
), $args, 'original_shortcode_new' );
$page_id = '';
if($args['page_alignment'] == 'left' && isset($_GET['dropdownl'])){
$page_id = intval($_GET['dropdownl']);
} else if($args['page_alignment'] == 'right' && isset($_GET['dropdownr'])){
$page_id = intval($_GET['dropdownr']);
}
if(empty($page_id)){
return "<p>Error, bad page</p>";
}
return do_shortcode("[original_shortcode page={$page_id}]");
}
add_shortcode( 'original_shortcode_new', 'original_shortcode_new' );
Again, when doing the above, I would get rid of the extra shortcodes and globals and just use some logic inside a single shortcode."
This is the part of the answer that answers my question above, but to see the full post click here: Reddit Question

Multi forms with selects but all $_POST are overwritten with last $_POST

In below test code I try to save and use 3 $_POST variables. But only the last $_POST variable is stored. The rest is overwritten with the last $_POST variable.
Purpose of the question is to select first the wanted country. Select from a table the country as selected. Then select the wanted a car_brand from the selected country. Then select the wanted company and filter this from the selected country+car_brand.
I have tried to store the 3 $_POST into a $_SESSION also tried it with $_REQUEST via type="hidden" without getting the 3 $_POST variables.
Question: what do I do wrong and how can I solve this problem?
session_start();
$content .= ' <form id="sel_country" method="POST">
<select name="country" onchange="this.form.submit()" >
<option value="NL">Netherlands</option>
<option value="DE">German</option>
<option value="GB">England</option>
</select>
</form>';
$content .= '<form id="sel_car_brand" method="POST">
<select name="brand" onchange="this.form.submit()" >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
</form>';
$content .= ' <form id="sel_company" method="POST">
<select name="company" onchange="this.form.submit()">
<option value="dealer">Dealer</option>
<option value="service">Service</option>
<option value="import">Importer</option>
</select>
</form>';
$_SESSION['country'] = $_POST['country'];
$_SESSION['brand'] = $_POST['brand'];
$_SESSION['company'] = $_POST['company'];
var_dump($_SESSION) ;
Had a session_start() at the beginning and then stock your variable in it. Like $_SESSION['country'] = $_POST['country'] when $_POST['country'] is defined (do a var_dump() to verify it) and you'll be able to access your datas stocked in your $_SESSION.
session_start();
$content .= ' <form id="sel_country" method="POST">
<select name="country" onchange="this.form.submit()" >
<option value="NL">Netherlands</option>
<option value="DE">German</option>
<option value="GB">England</option>
</select>
</form>';
$_SESSION['country'] = $_POST['country'];
$content .= '<form id="sel_car_brand" method="POST">
<select name="brand" onchange="this.form.submit()" >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
</form>';
$_SESSION['brand'] = $_POST['brand'];
$content .= ' <form id="sel_company" method="POST">
<select name="company" onchange="this.form.submit()">
<option value="dealer">Dealer</option>
<option value="service">Service</option>
<option value="import">Importer</option>
</select>
</form>';
$_SESSION['company'] = $_POST['company'];
var_dump($_SESSION) ;
// Edited by moliets: var_dump response: array(3) { ["country"]=> NULL ["brand"]=> NULL ["company"]=> string(6) "import" }

html form many options

Hi I would like to hear if there are alternatives for these forms
so I dont have to write for every single option
1. For the age form
For the country form
<select name="age">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<!--And again and again and... again-->
<option value="99">99</option>
</select>
<select name="country">
<option value="England">England</option>
<option value="Sweeden">Sweeden</option>
<option value="Norway">Norway</option>
<option value="Denmark">Denmark</option>
<option value="Usa">Usa</option>
<option value="Spain">Spain</option>
<option value="Scotland">Scotland</option>
<option value="Ireland">Ireland</option>
<option value="French">French</option>
<option value="Italy">Italy</option>
<option value="Germany">Germany</option>
<option value="Poland">Poland</option>
<option value="Netherland">Netherland</option>
<option value="Australia">Australia</option>
<option value="China">China</option>
<option value="Japan">Japan</option>
<option value="Singapore">Singapore</option>
<option value="Russia">Russia</option>
<option value="Other">Other</option>
</select>
Hope you understand my question. Sos for bad english
There is a HTML "range" input, but it's not the most common input method out there and will require some javascript with it. Other than that, the only reasonable method is PHP. Though, if you do use PHP, you can copy and paste the HTML and then insert it into your HTML file if you don't want that page to have PHP. Below are a few HTML only examples. May not work in all browsers, so your server side code should always validate. http://jsfiddle.net/h96nU/1/
The below code shows a few inputs using various methods. The range input will update to show the range value that is currently selected. The last number input uses the HTML5 pattern attribute.
input:invalid {
border: 1px solid red;
}
input:valid {
border: 1px solid green;
}
<div>
<input id="range" type="range" min="1" max="120" step="1" />
<span id="range_value">0</span>
</div>
<div>
<input type="number" />
</div>
<div>
<input type="text" />
</div>
<div>
<input type="number" min="1" max="120" step="1" id="n1" name="age" pattern="\d+" />
</div>
var input = document.getElementById('range');
input.addEventListener('change',input_change,false);
function input_change(e) {
var target = e.target || e.srcElement;
var value = target.value;
var showdiv = document.getElementById('range_value');
showdiv.textContent = value;
}
More about Data Validation in HTML can be found at https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation
For PHP method of an age input loop, look below.
<?php
$min = 1;
$max = 100;
echo '<select name="age">';
for($i = $min; $i <= $max; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
If you are using PHP for example you could do it like this:
<?php
$myArray = ['USA','Australia','ETC ETC ETC'];
echo '<select name="country">';
foreach ($myArray as $value) {
echo '<option value="'.$value.'">';
echo $value;
echo "</option>";
}
echo "</select>";
?>
The output of this example would be :
<select name="country">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="ETC ETC ETC">ETC ETC ETC</option>
</select>
For your age selectbox:
<?php
$maxAge = 99;
echo '<select name="age">';
for ($i=1; $i < $maxAge; $i++) {
echo '<option value="'.$i.'">';
echo $i;
echo "</option>";
}
echo "</select>";
?>

MySQL select query with ajax in wordpress

I am passing variable with ajax to list.php in my twentytwelve template. In list.php I am executing mysql select query but when I see in console I am getting this error:
<br />
<b>Fatal error</b>: Call to a member function get_results() on a non-object in <b>D:\xampp\htdocs\wordpress\wp-content\themes\twentytwelve\list.php</b> on line <b>4</b><br />
My Code:
search.php
<?php
/*
Template Name: Search
*/
get_header();?>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#parent_category").change(function()
{
var parent_category = $(this).val();
if(parent_category != '')
{
$.ajax
({
type: "POST",
url: "<?php echo get_template_directory_uri(); ?>/list.php",
data: "parent_category="+ parent_category,
success: function(option)
{
$("#child_category").html(option);
}
});
}
else
{
$("#child_category").html("<option value=''>-- No category selected --</option>");
}
return false;
});
});
</script>
<select id="parent_category" name="parent_category">
<option value="" selected="selected">-- Select blood group --</option>
<option value="A1 positive">A1 positive</option>
<option value="A1 negative">A1 negative</option>
<option value="A2 positive">A2 positive</option>
<option value="A2 negative">A2 negative</option>
<option value="B positive">B positive</option>
<option value="B negative">B negative</option>
<option value="A1B positive">A1B positive</option>
<option value="A1B negative">A1B negative</option>
<option value="A2B positive">A2B positive</option>
<option value="A2B negative">A2B negative</option>
<option value="AB positive">AB positive</option>
<option value="AB negative">AB negative</option>
<option value="O positive">O positive</option>
<option value="O negative">O negative</option>
<option value="A positive">A positive</option>
<option value="A negative">A negative</option>
</select>
<select id="child_category" name="child_category">
<option value="">-- No location selected --</option>
</select>
<?php get_footer(); ?>
list.php
<?php
if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
{
$result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
print_r($result);
}
?>
Any ideas or suggestions? Thanks.
you have to include the wp-blog-header in your list.php then define $wpdb as global the error you are is due to the definition of $wpdb which is undefined
<?php include("yourpath/wp-blog-header.php");
global $wpdb;
if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
{
$result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
print_r($result);
}
?>

How do I auto-select an option in a select field based on URL parameter?

In the form below, how do I get autoselect the various options based on the values of my URL parameters?
eg: http://example.com/?type=%28shirt%2C+tshirt%2C+t-shirt%29&length=25&width=17&expand=yes
<form name="shirty" method="get">
<select name="type" />
<option value="(shirt, tshirt, t-shirt)">T-Shirt</option>
<option value="(hoodie, sweatshirt)">Sweatshirt</option>
</select>
<select name="length" />
<option value="select">Select a length</option>
<option value="14">14 Inches</option>
<option value="15">15 Inches</option>
<option value="16">16 Inches</option>
<option value="17">17 Inches</option>
<option value="18">18 Inches</option>
<option value="19">19 Inches</option>
<option value="20">20 Inches</option>
</select>
<select name="width" />
<option value="select">Select a Width</option>
<option value="14">14 Inches</option>
<option value="15">15 Inches</option>
<option value="16">16 Inches</option>
<option value="17">17 Inches</option>
<option value="18">18 Inches</option>
<option value="19">19 Inches</option>
<option value="20">20 Inches</option>
</select>
<input type="checkbox" name="expand" value="yes"" checked><small>include ± 1 inch?</small>
<input type="submit" name="Submit" value="Search" />
</form>
Got it.
<form name="shirty" method="get">
<select name="type" />
<?php //create the select options
$options = "
<option value=\"(shirt, tshirt, t-shirt)\">T-Shirt</option>
<option value=\"(hoodie, sweatshirt)\">Sweatshirt</option>";
$saved = $type;
$saved = (!empty($saved))? $saved: false;
if ($saved)
//if there is a saved data set the option to selected
$options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
//echo out the options
echo $options; ?>
?>
</select>
<select name="length" />
<?php //create the select options
$options = "
<option value=\"14\">14 Inches</option>
<option value=\"15\">15 Inches</option>
<option value=\"16\">16 Inches</option>
<option value=\"17\">17 Inches</option>
<option value=\"18\">18 Inches</option>
<option value=\"19\">19 Inches</option>
<option value=\"20\">20 Inches</option>";
$saved = $length;
$saved = (!empty($saved))? $saved: false;
if ($saved)
//if there is a saved data set the option to selected
$options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
//echo out the options
echo $options; ?>
?>
</select>
<select name="width" />
<?php //create the select options
$options = "
<option value=\"14\">14 Inches</option>
<option value=\"15\">15 Inches</option>
<option value=\"16\">16 Inches</option>
<option value=\"17\">17 Inches</option>
<option value=\"18\">18 Inches</option>
<option value=\"19\">19 Inches</option>
<option value=\"20\">20 Inches</option>";
$saved = $width;
$saved = (!empty($saved))? $saved: false;
if ($saved)
//if there is a saved data set the option to selected
$options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
//echo out the options
echo $options; ?>
?>
</select>
<input type="checkbox" name="expand" value="yes"" checked><small>include ± 1 inch?</small>
<input type="submit" name="Submit" value="Search" />
</form>
<?php
//This example creates a select element and marks selected option using selected_id in URL
$attributesAssocArray = array(
"id"=>"someId","class"=>"someClass"
);
$optionsArray = array(
0=>"choose...",1=>"one",2=>"two",3=>"three"
);
$selectedId = (!empty($_GET['selected_id'])&&(is_numeric($_GET['selected_id'])))? intval($_GET['selected_id']):-1;
echo generate_select($attributesAssocArray,$optionsArray,$selectedId);
function generate_select($attributesAssocArray=array(),$optionsArray=array(),$selected=-1)
{
$attributes = generate_attributes($attributesAssocArray);
$html = '<select '.$attributes.'>';
foreach ($optionsArray as $value => $name)
{
$isSelected = $value==$selected ? " selected" : "";
$html.= '<option value="'.$value.'"'.$isSelected.'>'.$name.'</option>';
}
$html.='</select>';
return $html;
}
function generate_attributes($attributesAssocArray)
{
$attributes='';
foreach ($attributesAssocArray as $name => $value)
{
$attributes.= $name.'="'.$value.'" ';
}
return $attributes;
}
?>