Please, I need generate selectable list based on value from other list. I have function for generating first list.
function getCatList ()
{
$result = getCategory ();
echo "<select id=\"catList\">";
echo "<option value=\"\" selected=\"selected\">Select category</option>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value=\"".$row['codename']."\" onclick=\"<?php if($options==".$row['codename'].") echo 'selected=\"selected\"'; ?>".$row['visible_name']."</option>";
}
echo "</select>";
}
and I need generate next list based on selected value from this list.
I try set variable when onclick or some other attributes but without result.
I suppose it's caused because PHP is server-side language.
Can you help me? I would like avoid JS if possible. Is there any option how can I do this?
You cannot launch PHP that way.
If you want to (without refresh) generate a list from a selected value you need to use Javascript. The easy way is to use Ajax, but you can make something even with Javascript alone (in the same page).
You can for example save on loading some arrays of data, and then on select change the list or what you want.
Related
i want to use what we call a struct in C but using PHP, I know Here they are called classes, but i need to use that array of classes for a select tag name, I am doing this
<?php
class info_subject{
public $code_su;
public $time_su;
public $selecction_su;
}
$subjects= new info_subjects();
$i=0;
//THE DATABASE CONNECTION WORKS FINE, I IGNORED CODING ABOUT DATABASE BECAUSE THAT'S NOT THE
//PROBLEM, JUST FOCUS IN THE STATEMENT OF THE ARRAYS IN THE TAGS NAMES PLEASE
while($line = pg_fetch_array($result, null, PGSQL_NUM))//getting some stuff from postgrest
{
echo "$line[0]";//I am printing this
echo "$line[1]";//I am printing this
//here i am creating selects in every loop with some options, and i want to save the
//result of the selection in the field code_su of the array of classes
echo "<select name=$subjects[$i]->code_su>";
echo "<option value='hola'>hola</option>";
//here i am creating checkbox in every loop, and i want to save the
//result of the checkbox in the field selection_su of the array of classes
echo "<input type='checkbox' name=$subjects[$i]->selection_su>";
$i++;
}
?>
The problem is that it is not working, i think i am making a mistake with the statement in the names of the inputs and the selects, like i said before, i need a classes array.
The problem is that youre not adding the vars properly (nor the quotes). Try with:
echo "<select name=\"".$asignaturas[$i]->codigo_as . "\">";
and
echo "<input type='checkbox' name=\"".$asignaturas[$i]->seleccion_as."\">"
Regards.
1) You are echoing HTML wrong (missing ': echo "<select name='{$asignaturas[$i]->codigo_as}'>";
2) Your $asignaturas is not an array. It's only single class. Use it like this: echo $asignaturas->coding_as;
3) (as side note) By standarts, class names is CamelCases and it's name is same as file name.
It seems like you are trying to use the class itself as an array which can not be done.
Put in an constructor to define some of your variables here:
class info_asignatura{
public $codigo_as;
public $periodo_as;
public $seleccion_as;
function __construct(){
$this->seleccion_as = array();
}
}
The change this statement to:
echo "<input type='checkbox' name=$asignaturas->seleccion_as[$i]>";
Although, I fear this will not do the trick for you. Because every time this page is loaded, seleccion_as will be defined as an array when the class is constructed. This will overwrite anything previously declared.
What you will need to obtain your goal is to implement sessions to your code.
I have a list that allows multiple selections to be made and then submitted. What I now need to do is populate the select box so that the values submitted remain in the following page.
I'm passing the variable in the url and can list them using the following:
foreach ($_GET['sector'] as $selectedOption)
My thinking is to store the results in an array and then when the list is created loop through with the following:
if (in_array($optionvalue, $selectedOption))
{
echo "<option value='".$optionvalue."' selected='selected>".$optionvalue."</option>";
}
else
{
echo "<option value='".$optionvalue."'>".$optionvalue."</option>";
}
The above works but only for whatever the last selected item is.
Is this the correct way to do this or is there a simpler method?
Thanks for taking time to look at this.
I have two drop down menus. The first is a list of clients, the second is a list of projects.
All projects are tied to just one client, so I'd like for the code to get user input for the client, then read that value, and modify the PHP code to only print out the values in the second drop down menu that correspond to the client selected.
Here's some code. For the first drop down menu:
<div class="item">
<label for='clSel' id='tsClLabel'>Client:</label>
<select name='clSel' id='wClient' onChange="bGroup();">
<option></option>
<?php
$cQuery = "SELECT * FROM Clients ORDER BY Client_Name";
$cResult = mysql_query($cQuery);
while($cData = mysql_fetch_assoc($cResult)) {
echo '<option id="Cid" value="'.$cData['Id'].'">'.$cData['Client_Name'].'</option>';
}
?>
</select>
Here's my jQuery function to get the user-selected value from the first drop down:
<script>
function bGroup(){
val1 = $("#wClient").val();
// window.alert(val1);
// $('#div1').html(val1);
return val1;
}
</script>
And the code for the second drop down menu:
<label for='billGroupId'>Billing Group: </label>
<select name='billGroupId'>
<option value=''></option>
<?php
$sql = "SELECT * FROM Billing_Groups ORDER BY Client_Id, Name";
$sth=$dbh->prepare($sql);
$sth->execute();
while ($row = $sth->fetch())
{
if ($row['Name']!= ''){
echo "<option value='".$row['Id']."' > ".$row['Name']."</option>";
echo "<script> bGroup(); </script>"
}
}
?>
</select>
I know I need to include a WHERE statement in the second drop down menu
Basically Select * FROM Clients WHERE Client_ID == $jsVAR.
I already have the value I need in the var1 JavaScript variable. How can I get this little piece of data either read by PHP or sent to PHP via JS code?
Thanks!!
You can SELECT all records from the database, and then insert them to your page HTML using json_encode(). Something like that:
<?php
$sql = "SELECT * FROM Billing_Groups ORDER BY Client_Id, Name";
$sth=$dbh->prepare($sql);
$sth->execute();
$projectData = array();
while ($row = $sth->fetch())
{
if ($row['Name']!= ''){
$projectData[$row['Client_Id']][] = $row;
}
}
echo '<script type="text/javascript">var projects=', json_encode($projectData), ';</script>';
?>
Then, in your JS, you use the variable projects as an associative array (object), eg.:
<script type="text/javascript">
for (p in projects[clientId]) {
alert(projects[p].Name);
}
</script>
Tricky one,
You have a choice. One way is to use Ajax to grab the second level menu structure upon getting the first level choice, and populate the second level once that succeeds. That's likely to be a problem, as there will likely be some sort of network delay while that happens, of which you have no control (unless you are in a closed environment). So from a user point of view it could be counter intuitive and sluggish feeling, especially on a slow connection or shared hosting solution where timings can vary enormously.
The other way is to somehow pull all values possible and filter them (so hide the ones that don't apply) using jQuery, perhaps utilising classes or some other attribute as a method of filtering data. Using jQuery you can assign data to elements so you could also use that too. The second method may not be so good if there's a lot of data (can't tell from the scenario you've described). Looking at your second level code I don't see a WHERE condition so I'm not sure how the value from the first level is affecting that of the second level, so it's hard to know how to deal with that for this method.
I have created a website in which when user logs in he specifies the year i.e. 2013-2014, or 2014-2015, and so on...Now, I am storing this value of year in a session variable which I am using it through out the site. Now, if user wants to change the year he will have to sign out and then log in with different year. I have created a dropdown menu on home page which will show all the years from database. I want to change the value of the session variable by selecting a year from home page without signing out.
Here is the code:
session_start();
if(!isset($_SESSION["myusername"])){
header("location:login.php");
}
$year = $_SESSION["year"];
Here is the code for dropdown menu:
$query = "SELECT * FROM year";
$result = mysql_query($query);
echo "<select class='innerinputstyle' id='year' name='year'><option value='$year'>$year</option>";
while($note=mysql_fetch_array($result)){
echo "<option value=$note[year]>$note[year]</option>";
}
echo "</select>";
Can anyone please tell me how to change the value of $year from home page?
I think you have plenty of choices to achieve this, and the AJAX one is one of them , but everything depends on how you want to implement the user interface. You can think to create a little form with the drop down and submit the year, you can do this via a submit button or via javascript and as for the AJAX example the solution is always the same:
$_SESSION['year']=$_GET['year'];
At the end depend all on the user interface, if you want it invisible to the user, without refreshing the page, use AJAX, otherwise just submit your input
Change
echo "<select class='innerinputstyle' id='year' name='year'>"
into
echo "<select class='innerinputstyle' id='year' name='year' onChange='doIt()'>"
and here is the ajax function
function doIt(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
}
var new_year = document.getElementById('year').value;
xmlhttp.open("GET","change_year.php?year="+new_year,true);
xmlhttp.send()
return false;
}
on change_year.php you can easily change
$_SESSION['year']=$_GET['year'];
Warning: Untested code. Used to sketch the idea.
I'm creating an HTML form, which takes some of its options from a database (php + mysql)
In the php, I'm creating a checkbox input, with a select box next to it.
I named the checkbox houseAppsSelected[] and the select customCategories[], so I'll get the values as an array.
I append all the HTML into a var called $options, and I echo it later on.
while ($row=mysql_fetch_array($result)) {
$cat_id=$row["category_id"];
$cat_name=$row["category_name"];
$options.="<INPUT type=\"checkbox\" name=\"houseAppsSelected[]\" VALUE=\"$cat_id\">".$cat_name." ---> ";
$custom_sql="SELECT custom_cat_id, cat_name FROM custom_categories WHERE house_app='$cat_id'";
$custom_result=mysql_query($custom_sql);
$options.="<SELECT name=\"customCategories[]\">";
$options.="<OPTION value=\"0\"> Choose Category </option>";
while ($custom_row=mysql_fetch_array($custom_result)) {
$custom_id = $custom_row['custom_cat_id'];
$custom_name = $custom_row['cat_name'];
$options.="<OPTION value=\"$custom_id\">".$custom_name."</option>";
}
$options.="</SELECT> <br /> <br />";
}
I want to have the checkbox control whether the select box is enabled or disabled.
I found this article, which makes it look easy, but if all the select boxes have the same name, it will disable all of them.
Is there a way to have a specific checkbox disable/enable only a specific select box, if I build them dynamically with php? (they all have the same name).
You can use the nextSibling property to find the select.
function chkboxClick(chkbox) {
chkbox.nextSibling.nextSibling.disabled = !chkbox.checked;
}
Add the click handler like this:
<INPUT type="checkbox" onclick="chkboxClick(this)" ... />
Demo here: http://jsfiddle.net/gilly3/vAK7N/
You can give each tag a unique "id" value, which is independent of the "name". Then you can use
var elem = document.getElementById(something);
to access them by that unique value.
Exactly how your php code makes up the unique values sort-of depends on what you need, exactly. It can really be anything.