dropdown list that refresh a div content upon select - javascript

i have totally no idea about how to do this, so i'm gonna just ask away.
I have a dropdown menu which list dates say
1/2/2013
2/2/2013
3/2/2013
4/2/2013
5/2/2013
6/2/2013
7/2/2013
if you were to select one of the dates, a div will pop out with say 5 choices
A
B
C
D
E
each choices are stored in the database, and if say B item is not available on 2/2/2013, i would have a script to disable it being selected. I've figured how to create that in php, but my ultimate question is
how do you select any of the dates but yet still able to retrieve the 5 choices from a database?
I'm currently doing something like this
function TheDisabler($aa)
{
global $con, $vdate;
$myresult = mysqli_query($con,"SELECT * FROM burger WHERE timeslot = '$aa' AND date = '$vdate'");
list($mycount) = mysqli_fetch_row($myresult);
if($mycount >= 1) {
echo "disabled";
}
}
but i figured that this works only once and if i were to change the date, the items within the div will not change =/

You should have a script that works every time the div element is clicked. So add an onClick event to div and then retrieve the required data from database on that event's handler.

if your table does not have large amount of data, create a php file that behave as if it's a javascript file like that:
<?php
$js = 'data = new Array()';
$sql = 'select ...';
$count = 0;
while($row = fetch_rows($sql)){
// do your check here
$js .= 'data[' . $row['date'] . '] = new Array()';
$js .= 'data[][' . $row['date'] . '][$count] . ' = "'.$row['item'] . '";';
$count++;
}
header('Content-Type: text/javascript');
echo $js;
?>
now you have a javascript array which have every date items, then on each click ask this array for that date key should return you the your options list

I think what you are looking for is the onchange event of the ckeck box. Each time there is a change in the drop down, call the function to retrieve the values from the database.

Related

I have a problem with outputting 2 values from a single drop down

Im trying to modify a calculator PHP which is based on drop down selections
I have 5 Drop downs. One of which when the value is selected it outputs either "18" or "72" based on what is selected. Within that table is another column which has the values "1" "0.5" "0.4" "0" how do I get the drop down to select both rows to help me get them into a string for calculation?
This is the input / selector (this is where i need to add the extra row based on what the user selects in the drop down)
<?php
while ($row = mysqli_fetch_array($bunkernic)) {
echo '<option value="'.$row['nic_mg'].'"'.($_POST['nic'] == $row['nic_mg'] ? " selected": "").'>'.$row['select_nic'].'</option>';
}
?>
Currently that outputs the nic_mg to a $input which changes when a user selects (i can see this when i use print_r ($input);)
Could anyone guide me to achieve this?
If you need any more info / or files to take a look at let me know
I may not be making any sense here, I'm a complete beginner at this but I feel I'm picking little bits up
Thanks
Put both column values into the value attribute, with a delimiter between them.
$val = $row['nic_mg'].'|'.$row['other_column'];
echo '<option value="'.$val.'"'.($_POST['nic'] == $val ? " selected": "").'>'.$row['select_nic'].'</option>';
Then when the form is submitted, you can split the value:
list($nic_mg, $other) = explode('|', $_POST['nic']);

JavaScript PHP Dropdown Menu

I want to make a DropDown Menu, where someone can Choose an option out of several options. Ans depending on this Value the other Dropdown menu should be updated with values out a Database.
Like
Option 1: kind of sport
Option 2: Time
Option 3: Place
.
.
.
// EDIT adding actual option.
$query = 'SELECT * FROM kurs';
$result = $mysqli->query($query);
echo
"<table border = 1>
<tr align = 'middle'>
<td> <select>";
while ($zeile = $result->fetch_assoc()) {
echo "<option>" . $zeile['name'] . "</option>";
}
$result = $mysqli->query($query);
echo
"</td><td> <select>";
while ($zeile = $result->fetch_assoc()) {
echo "<option>" . $zeile['day'] . "</option>";
}
$result = $mysqli->query($query);
and if someone update choose one option out of 'day', the other option 'name' should change depending on the 'day' and vice versa.
I know how to get the information out of my SQL Database but i don't know how to make the Select Area dynamic. I'm not allowed to use JQuery.
Any help is appreciated
Summary: Use a <select onchange... event, XMLHttpRequest, and two PHP scripts.
To simplify the discussion, let's forget about Sport. The user first chooses a race date and then chooses an entrant name. Initially, the date dropdown shows all the dates, and the name dropdown is blank. After selecting a date, the name dropdown is populated with all the names for that date.
And suppose the database is so large that you don't want to send all the data to the browser in one go. You only want to send the names after the user has chosen a date.
This is classic AJAX. If you don't use jQuery (or another Javascript library), then you need to roll-your-own by using XMLHttpRequest (see http://www.w3schools.com/ajax ). That's what jQuery does.
Now, to examine your code. 'SELECT * FROM kurs' is going to give you all the records in the database. If you have 1000 records, your Date dropdown will have 1000 options! You might want:
SELECT DISTINCT [day] FROM kurs ORDER BY [day]
If you want the Date dropdown to feed names into the Name dropdown as soon as the user chooses a date, without the need to click a Submit button, then do this:
$optionlist = '<option value="">--Choose Race Date--</option>';
while ($zeile = $result->fetch_assoc()) {
$optionlist .= '<option>' . $zeile['day'] . '</option>';
}
echo "
<table border='1'><tr>
<td><select onchange='datechange(this)'>$optionlist</select></td>
<td id='tdSelectName'><select disabled></select></td>
</tr></table>";
The --Choose Race Date-- option is there so that if the user happens to want the first date then there will still be an onchange() event.
Your Javascript will need:
function datechange(sel) {
raceDate = sel.value;
if ( raceDate ) {
var myRequest = ...
// your synchronous XMLHttpRequest code goes here
document.getElementById("tdSelectName") = myRequest.responseText;
}
}
To see a fancier version of this in action, look at http://www.mostbryte.com/where_to_buy

Select specific row in <select> list after return from SQL query

I have this rather tricky problem to solve whereby I have used the following to display a list of named shops with ID number for selection from a drop down list when creating a new employee record. This works well at this point. A piece of javascript splits the displayed text from the the user selection and sends the shop ID number off with the new employee details to be inserted into the employee table in the database. I am using a hidden shopID text box to store the number as can be seen in the javascript.
Here is the code PHP first then javascript:
$result = mysqli_query($link,"SELECT shopID, shopName FROM SHOP");
echo "<br><select class='formInput' name='listbox' id='listbox' onchange='captureShopID()' tabindex=9>";
#Use onchange instead of onclick where Keyboard is used. onclick does not register changes fro keyboard
while($row = mysqli_fetch_array($result))
{
$shopID = $row['shopID'];
$shopName = $row['shopName'];
$allText = "$shopID, $shopName";
echo "<option value='$allText'>S00$shopID $shopName</option>";
}
echo "</select>";
AND (including this, because it may contain hints to a solution for my problem)
<script>
function captureShopID()
{
var sel = document.getElementById("listbox");
var result;
result = sel.options[sel.selectedIndex].value;
var shopNumber = result.split(',');
document.getElementById("shopID").value = shopNumber[0];
}
</script>
OK. So all good so far. What I am trying to do is use the same set up for amendments to the same record. So the update layout is similar to the one for creating the record. I have the list element again but what I would like is to have it showing the shop that the employee works at otherwise it is confusing as the list defaults to the first item in the list, in most cases not the actual shop that the employee works in.
So, instead of:
S001 London
Maybe it should be:
S003 Paris… Where the employee works.
I have tried various things but it is a tricky one. The fact that there is the option value concatenation of $shopID and $shopName may be complicating things a bit in my quest for a solution.
Pretty new to PHP and javascript (javascript pretty mysterious) and programming as a whole. Learning quickly but suffer many days of brain cell overload.
Any pointers in the right direction appreciated.
My Solution… After some outside the box tangental thinking working on some other aspects of my project.
<?php
include "../db.php";
$result = mysqli_query($link,"SELECT shopID, shopName FROM SHOP");
echo "<br><select class='formInput' name='listbox' id='listbox' onchange='captureShopID()' tabindex=9>";
#Use onchange instead of onclick where Keyboard is used. onclick does not register changes from keyboard
echo '<option>Works at:'.$shopName.'</option>';
while($row = mysqli_fetch_array($result))
{
$shopID = $row['shopID'];
$shopName = $row['shopName'];
$allText = "$shopID, $shopName";
echo "<option value='$allText'>S00$shopID $shopName</option>";
}
echo "</select>";
?>
I achieved what I wanted by adding the extra to the top of the list outside the loop to act as the default item listed:
echo 'Works at:'.$shopName.'';
This has the result of presenting the update employee record so that shop from their record is what you see in the menu like so:
Works at: London Southwark
And the list pops up to show the other shop options
Works at: London Southwark
S001 DISTRIBUTION CENTRAL Paris
S002 Paris Montmartre
S003 London Southwark
S004 Roma Trastevere
and so on.
Not exactly what I initially intended but just as good
I am aiming for a clean and spasre interface without too many labels and this solution makes the list element self explanatory.
If any one has a good suggestion for the thread title please post as this one is quite useful.

Clicked link submit value to SQL SELECT

I am trying to get this to allow the user to click on a link, which will then change the value of what is submitted in the SQL SELECT Statement, if that makes sense.
My Code so far:
<div class="Tabs" >
<?php
require 'database/connect.php';
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks"> Week' . $row{'Week'} . '</a>';
}
?>
</div>
<div class="Pages">
<?php
require 'database/connect.php';
$sql = "SELECT * FROM PMWUpdates WHERE Week='1'";
?>
</div>
I want the value of SELECT * FROM PMWUpdates WHERE Week="'1'"; to be what link the user has clicked on above, not just always 1. So if the user clicks on the link 2, the SQL changes to SELECT * FROM PMWUpdates WHERE Week="'2'";
I am basically trying to accomplish this, but instead, I want to display the number of tabs, based on the number of weeks in a database, and then, each week will display data from the database about that particular week.
Any help is appreciated.
Thank You
Your statement should be like this...
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks" href="$row['Week_link']"> Week' . $row['Week'] . '</a>';
}
If you mean dynamically changing data on a page which requires PHP to be processed, you have to take a look at AJAX. You can't do it this way, once your script is loaded, there is no more php, only HTML.
If you don't want to use AJAX, you will have to do it in 2 pages. First display links, then display your data related to whatever was selected.
The MySQL extension you are using is deprecated (>= 5.5). Use MySQLi or PDO instead.
What you are trying to accomplish is that where your "2" is listed, should change with the use of a variable. As you want to do it via Javascript, you will need a click event on those links which then submits the variable via AJAX.

Execute script when select option is changed

I have a html form which includes a dynamically created dorpdown list. The dropdown list contains the names of newsletters which are stored in my MySQL database. What I want the dropdown list to do is: When I select a newsletter an other php script will activate which will take the data from the newsletter in my DB and writes it to a .txt file. The codes I currently have is:
The dropdown list:
<?php
echo "<select id=\"NieuwsbriefSelect\" name=\"show\">";
echo "<option size =30 selected>Select</option>";
if(mysql_num_rows($sql_result))
{
while($row = mysql_fetch_assoc($sql_result))
{
echo "<option value=\"$row[Titel]\">$row[Titel]</option>";
}
}
else {
echo "<option>No Names Present</option>";
}
?>
And the write script:
<?php
$title = $_REQUEST["show"];
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL = "SELECT Content from NAW.Mail where Titel = '$title' ";
$sql_result = mysql_query($strSQL);
$row = mysql_fetch_assoc($sql_result);
$file = 'nieuwsbrief.txt';
$current = $row["Content"];
file_put_contents($file, $current);
?>
I do not want the page to redirect to the write script but just to execute it (I hope you get this^^). Is this possible using the HTML onChange Event or do I have to use javascript? Any help would be great and if you have a question about my code just ask in the comments!
NOTE!
I know I shouldn't be using Mysql_* and That I am vulnerable to sql injection but that is not the point.
You need to define a onchange event on your SELECT option and inside this onchange event, you can redirect to another page with selected item OR do an AJAX call to another PHP script
Unfortunately what you are asking is not possible without JavaScript. PHP is a server side language and therefore cannot be run, without a call from the client-side browser. You're best bet would be to use ajax in conjunction with the JavaScript onchange event. When the change event occurs, fire an ajax call off to the server to run the script with the selected option. This is the only way to do what you're asking without a page reload or redirect.

Categories