Dropdownlist in yii - php

I have a problem with a dropdownlist created in Yii. I wanted to create a dropdown list with sample below:
<select id="con_id">
<option value="1">Con 1</option>
<option value="2">Con 2</option>
<option value="3">Con 3</option>
<option value="4">Con 4</option>
<option value="5">Con 5</option>
<option value="6">Con 6</option>
<option value="all">--All--</option>
</select>
Dropdown menu list are obtained from the database. Below is the sample code that I created in Yii format:
echo $form->dropDownList($model, '_conId', array_merge(CHtml::listData(Consultant::model()->findAll(), 'id', 'name'), array('client_id' => '--All--')));
However, the result of the dropdown list created in Yii become like this:
<select id="con_id">
<option value="">Con 1</option>
<option value="1">Con 2</option>
<option value="2">Con 3</option>
<option value="3">Con 4</option>
<option value="4">Con 5</option>
<option value="5">Con 6</option>
<option value="all">--All--</option>
</select>
The value on the option is not correctly assigned. Can anyone help me?
UPDATE:
Somehow I have found the solution:
echo $form->dropDownList($model, '_conId', array('all' => '--All--') + CHtml::listData(Consultant::model()->findAll(), 'id', 'name'));

There's an 'empty' option for CHtml::listData, where you can set the text for the 'empty' option.
Here's an example using your code:
<?php echo $form->dropDownList(
$model,
'_conId',
CHtml::listData(
Consultant::model()->findAll(),'id','name'), // list all data from model
array('empty'=>'--All--') // set empty data label
); ?>

Related

How and where do I send a GET request from an HTML form in Wordpress?

I making a page in Wordpress and I need to be able to use the data submitted by an HTML form as variables. The form in my case is two simple dropdown select forms with a submit button. I am not sure what to put as the action attribute for the form and how to access it. I want to use the GET method to do this, but I am not sure how to use PHP in Wordpress or where I would store a PHP page/file. Any help would be appreciated.
This is the HTML code of my page, the items in square brackets are from plugins. kids.php is a file on my computer that I ran it locally with, it has nothing to do with my Wordpress site and does not do anything there.
<form id="form" action="kids.php" method="get">
[one_half]
<h3>Select a Country</h3>
<div class="dropdown">
<select id="dropdownl" name=dropdownl>
<option disabled="true">Country</option>
<option disabled="true">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="true">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="true">Oceania</option>
<option value="32137">Australia & New Zealand</option>
<option disabled="true">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="true">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="true">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>
[/one_half]
[one_half_last]
<h3>Select a Country</h3>
<div class="dropdown">
<select id="dropdownr" name=dropdownr>
<option disabled="true">Country</option>
<option disabled="true">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="true">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="true">Oceania</option>
<option value="32137">Australia & New Zealand</option>
<option disabled="true">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="true">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="true">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>
[/one_half_last]
<input type="submit" value="Submit">
</form>
In order to integrate your custom form in Wordpress, you have to do the following:
Create a custom page template by creating a new php file in your theme and include this convention on the top of the page:
<?php /* Template Name: Example name */ ?>
You can include your form on this template file, but ensure that you leave the action attribute empty. Do not use common names for the input fields in your form, because you can easily make a conflict with some of WordPress' reserved names.
<form id="form" action="" method="get">
You can access the $_GET variable in this file or through the "init" action hook.
Make sure to create a new Page from the WordPress dashboard and use your newly created custom template with this page. This way you can access your form on the front-end.
The path to the PHP page would need to be the action. If the page containing your form is the same page where the PHP script will execute, then you would use that path as the action.
So lets say your application runs on example.com/subdir/mypage.php, then your action could simply look like this: action="/subdir/mypage.php"
But if your PHP script runs in a different location, then your action would have to point to that location.
To access the parameters submitted from the form, your PHP script would need something like this:
<?php
$myvar=$_SERVER['QUERY_STRING'];
parse_str($myvar,$output);
$dropdownl_val=$output['dropdownl'];
$dropdownr_val=$output['dropdownr'];
?>
I would recommend you look into Wordpress plugin development though, because adding any scripts to your template means it would execute on every request, and that is not a good idea unless it is exactly what you want to achieve, or unless the page runs independently from your Wordpress application.

Make the option selected based on Url parameter in php

I need to select both drop down based on the url parameter
Here is my code
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl(this.value)">
<option value="www.site.co/?test=101">Test 1</option>
<option value="www.site.co/?test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl(this.value)">
<option value="www.site.com/?sample=95">Sample 1</option>
<option value="www.site.co/?sample=96">Sample 2</option>
<option value="www.site.co/?sample=97">Sample 3</option>
<option value="www.site.co/?sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
<script>
function getFilterUrl(filterurl) {
var url = filterurl;
if (url) {
window.location = url; // redirect
}
return false;
}
</script>
Right now after selecting each drop down, that is refreshed to selected url,
I am looking for code, if we select first drop down, and next one,
I need to form url like below for second drop down first option.
Example: if Test 1 is selected, then, Sample 1 selected, the Url should be like beloww with both option as selected
site.co/?test=102&sample=95
How this can be done?
Please anyone help me to achieve this.
Thanks
As I said, your question is not clear
Here is a fixed version of your code.
document.getElementById("container").addEventListener("change",function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
if (parent && child) {
var url = "https://site.co?"+parent+"&"+child;
console.log(url)
// window.location=url;
}
});
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat" >
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</DIV>
In PHP:
document.getElementById("container").addEventListener("change", function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
document.getElementById("message").innerHTML= parent && child ? "":"Please choose both";
if (parent && child) document.getElementById("myForm").submit();
});
<form action="redirect.php" id="myForm" method="post">
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat">
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</div>
</form>
<span id="message"></span>
using
$parent = isset($_POST['parent-cat']) ? $_POST['parent-cat'] : null;
$child = isset($_POST['child-cat']) ? $_POST['child-cat'] : null;
if ($parent && $child) {
header("location: https:///www.site.co?".$parent."&".$child);
}
If you don't want to redirect immediately after choosing anyone just erase one of the onchange event.
HTML:
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl()">
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl()">
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
Script:
function getFilterUrl(filterurl) {
var s1 = $('#parent-cat').val();
var s2 = $('#child-cat').val();
if (s2 == 'www.site.co') {
s2 = '';
}
window.location.replace("http://www.site.co/?"+s1+'&'+s2);
return false;
}
In case of 5th option from the second select I exclude the second param from the url. If you have any questions - ask them.

Pushing Data to a MySQL Database from a Form

I am trying to send data from an HTML form to a MySQL database in phpmyadmin. I have a database named bhs2018 and a table called game 1. Here are the contents of that table.
Here is my form:
<form name="game" action="insert.php" method="post">
<p> <select id="player" name = 'player'>
<option value="b">B</option>
<option value="n">N</option>
<option value="a">A</option>
<option value="c">C</option>
<option value="m">M</option>
<option value="j">J</option>
<option value="ja">Ja</option>
</select>
<select id="what" name = 'what'>
<option value="shoton">Shot on Cage</option>
<option value="shotoff">Shot off Cage</option>
<option value="goal">Goal</option>
<option value="countergoal">Goal on Counter</option>
<option value="countershot">Shot on Counter</option>
<option value="assist">Assist</option>
<option value="block">Block</option>
<option value="steal">Steal</option>
<option value="turnover">Turnover</option>
<option value="drawn">Ejection Drawn</option>
<option value="ejected">Ejected</option>
</select>
<select id="where" name = 'where'>
<option value="set">Set</option>
<option value="navy">Navy</option>
<option value="leftwing">1/2 side past 5</option>
<option value="rightwing">4/5 side past 5</option>
<option value="point">Point/3</option>
<option value="lefttwo">1/2 side 2 meter</option>
<option value="righttwo">4/5 side 2 meter</option>
<option value="1">6 on 5 1</option>
<option value="2">6 on 5 2</option>
<option value="3">6 on 5 3</option>
<option value="4">6 on 5 4</option>
<option value="5">6 on 5 5</option>
<option value="6">6 on 5 6</option>
</select>
<select id="quarter" name = 'quarter'>
<option value="q1">Quarter 1</option>
<option value="q2">Quarter 2</option>
<option value="q3">Quarter 3</option>
<option value="q4">Quarter 4</option>
</select>
<select id="time" name = 'time'>
<option value="0:30">0:30</option>
<option value="1:00">1:00</option>
<option value="1:30">1:30</option>
<option value="2:00">2:00</option>
<option value="2:30">2:30</option>
<option value="3:00">3:00</option>
<option value="3:30">3:30</option>
<option value="4:00">4:00</option>
<option value="4:30">4:30</option>
<option value="5:00">5:00</option>
<option value="5:30">5:30</option>
<option value="6:00">6:00</option>
<option value="6:30">6:30</option>
<option value="7:00">7:00</option>
</select>
Notes: <input type="text" id = 'notes' name = 'notes'>
<button type="submit" onclick="save()"> Save </button> </p>
</form>
Whenever I click my "Save" button, the insert.php script loads. Instead of echoing something, it just shows the code. Here is insert.php.
<?php
$con = mysqli_connect('127.0.0.1','root','password'(my actual password is here);
if(!$con){
echo 'Not Connected to Server';
}
if (!mysqli_select_db($con,'bhs2018')){
echo 'Not Selected';
}
$Player = $_POST['player'];
$Quarter = $_POST['quarter'];
$Time = $_POST['time'];
$Where = $_POST['where'];
$Notes = $_POST['notes'];
$What = $_POST['what'];
$sql = "INSERT INTO game1 (player,quarter,time1,where1,notes,what) VALUES ('$Player', '$Quarter', '$Time', '$Where','$Notes','$What')";
if(!mysqli_query($con,$sql)){
echo'Not Inserted';
}
else{
echo 'Inserted';
}
header('refresh:2; url=index.html');
?>
What is looking wrong with my code? Why does it not run the php script? Thank you so much!
Check your database. Your PRIMARY KEY player should be an integer like int(30) or bigint(200) but not varchar. Create a new column for PRIMARY KEY something like player_id and shift player to next column. Also when primary key is an integer, there is no manual insertion option for inputting it's value in your form. So make your PRIMARY KEY is set to AUTO_INCREMENT in case you don't want to insert it's value manually.

How to insert multiple data from listbox to database

Please look this link to see the code use by me
<pre>
http://jsfiddle.net/JaavS/
</pre>
<pre>
step1: select values in List A
step2: Move the values to list b
Step3:click save to database button
</pre>
After step3, the item in the list b values to be store in database. Can any one help me
Give your selects a name:
Select1:
<select name ="list1[]" id="list1" multiple="multiple" rows=2>
<option value=1>Option 1</option>
<option value=2>Option 2</option>
<option value=3>Option 3</option>
<option value=4>Option 4</option>
<option value=5>Option 5</option>
<option value=6>Option 6</option>
</select>
Select2:
<select name="list2[]" id="list2" multiple="multiple" rows=2>
</select>
In your php file:
<?php
//List 2
foreach ($_POST['list2'] as $selectedOption)
{
$query = "INSERT INTO table (option) VALUES ('$selectedOption')";
mysql_query($query) or die(mysql_error());
}
?>

select lis menu echo

i have a 2 types of select list menus:
<select name="category">
<option>----Select----</option>
<option>Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<select name="category_product">
<option>----Select----</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
</select>
What i liked to to is when i choose "Category 1" of the select list menu, it echos the select list menu "category_product", how can i do this?
I was thinking in put the category_product list menu in a variable, and when i chose a category it echo´s the category_products list menu, but i dont no how to make this detect when the category is chosen.
Hope for some help
Your only way is doing this with JS or jQuery, here is en example doing it with JS,
function showProducts(cat){
document.getElementById(cat).style.display = 'block';
}
<select name="category" onchange="showProducts(this.value)">
<option>----Select----</option>
<option value="category_product">Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<select style="display:none" id="category_product" name="category_product">
<option>----Select----</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
</select>
Or another option what you have to make an AJAX call if you want the result should be straight from the database.
UPDATE:
Use this function instead of the above,
function showProducts(cat){
for(var i=1;i<document.getElementsByTagName('select').length;i++){
document.getElementsByTagName('select')[i].style.display = 'none';
};
document.getElementById(cat).style.display = 'block';
}
You can play around with it let me know for any further help.

Categories