PHP - Dropdown Problem
Hi Guys,
I've been pulling my hair out regarding this peoblem. Hope you can help. On my webpage I have 4 html dropdowns and 2 html textboxs. What I need to do is populate the 2nd dropdown from the results of the first, populate the 3rd dropdown from the results of the 2nd and finally populate the 2 textboxs from the results of the 4th dropdown. So far I have been using the $_POST variable to send back to some PHP functions to populate the dropdowns. To make the dropdowns remember the last selection i've been using 'echo $_POST[' ']' just to show what the user choose before. My question is; is there a more simple way of doing this because its getting pretty messy. I've been looking at AJAX but i really dont wanna go there just yet. Hope you help TIM Similar TutorialsI have set up a dynamic dropdown to filter records on a page but it is not working and I feel it is down to the way I am using the GET. My guess is that you cannot use dates as a filter in this way but I cannot find any evidence of that through searching, mainly as I do not know what to search for! The url generated is 'games.php?date=2005-09-03&submit=Filter' so it is passing the data from the dropdown correctly but I am clearly missing something as it just returns that no records have been found. I am probably missing something simple and am sure that someone on here will spot it within seconds but I am lost. The actual testing page for this, in case you want to have a look at it not working, is http://www.margate-fc.com/content/test/games.php. As always, any advice would be greatly appreciated! Steve The Query Part // The 'WHERE' bit if (isset($_GET['date'])) { if ($_GET['date'] == "") { $filter = ""; } else { $filter = "WHERE date = {$_GET['date']}"; } } // Main Query $limit = 5; $all_games = mysql_query("SELECT *, HT.team_name as HomeTeam, VT.team_name as AwayTeam, COMP.comp_short_name as CP FROM all_games JOIN teams as HT ON all_games.home_team = HT.team_id JOIN teams as VT ON all_games.away_team = VT.team_id JOIN competitions as COMP ON all_games.comp = COMP.comp_id $filter ORDER BY all_games_id ASC "); if (!$all_games) { die("Query to show fields from table failed"); } // Filter query $dates = mysql_query("SELECT DISTINCT date FROM all_games ORDER BY date ASC "); The form Code: [Select] <!--Filter Form--> <form method='get' enctype='text/plain'> <select size='1' name='date'> <?php while ($row_dates = mysql_fetch_assoc($dates)){ echo "<option value='".$row_dates['date']."'>".$row_dates['date']."</a></option>"; } ?> </select> <input type='submit' name='submit' value='Filter'> </form> The table part (excluding header row) Code: [Select] <?php if(!mysql_num_rows($all_games)) { echo "<tr><td style=\"text-align:left\" COLSPAN=\"8\">No records returned</td></tr>"; } else { while ($row_all_games = mysql_fetch_assoc($all_games)){ echo '<tr> <td style="text-align:left" width="10PX">'.$row_all_games['all_games_id'].'</td>'; if ($row_all_games['date']=="") echo '<td>TBA</td>'; else echo '<td style="text-align:left" width="100PX">'.date('d M y',strtotime($row_all_games['date'])).'</td> <td style="text-align:left">'.$row_all_games['CP'].'</td> <td style="text-align:left"> '.$row_all_games['HomeTeam'].'</td> <td style="text-align:left"> '.$row_all_games['home_goals']. '</td> <td style="text-align:left"> '.$row_all_games['AwayTeam']. '</td> <td style="text-align:left"> '.$row_all_games['away_goals']. '</td> <td style="text-align:left"> '.$row_all_games['attendance']. '</td> </tr>'; }} ?> I am using the code below to select a folder for an image upload. It works fine in that it does show all of the sub folders of the images folder but the first two options in the dropdown are . and .. I am not sure where they are coming from (although they do make me think of 'up one level' and 'go to top level option's) but cannot work out how to remove them. Also, I have been searching for a way to list the folders alphabetically but am stuck on that too! Thanks in advance for any suggestions Steve $dirs = scandir('/path/to/images'); foreach ($dirs as $dir) { if (is_dir('/path/to/'.$dir)) { echo '<option value="'.$dir.'">'.$dir.'</option>'; } } Hello,
I have been trying to figure this for a while now and reading the tutorials are not helping, I think I'm a little over my head on this one and was hoping someone could help me out with this issue.
I am making a User Edit page and would like to have the access level part of the form show the users access current access level thats set in the database when the page loads, and if it needs to be changed you can press the dropdown box and select a new access level. I can't figure out how to show the current access level as default and populate the drop down box with the other access levels in my table.
My Tables look like this
Users table (users):
---------------------------------------------------------------------------------------------------
| id | username | password | flag | realfirst | reallast | dept |
---------------------------------------------------------------------------------------------------
1 loderd 9 test guy Service
Auth Table (auth):
--------------------------------------------
| id | auth_level | descrip |
--------------------------------------------
1 1 Service Tech
2 2 Office Staff
3 9 Super Admin
My SQL Query looks like this
$users = $db->fetch_all_array("SELECT users.id, users.username, users.password, users.realfirst, users.reallast, users.dept, users.flag, auth.auth_level, auth.descrip, auth.id FROM users LEFT JOIN auth ON users.flag = auth.auth_level WHERE users.id = '".$_GET['id']."'");I can't seem to figure out how I can do this for the Access Level dropdown box. <tr> <td width="19%" align="right" valign="top">Access Level :</td> <td width="1%" align="left"> </td> <td width="80%" align="left" valign="top"> <?php echo "<select name='flag' id='flag'>"; foreach ($users as $row){ if($row[auth_level]==$row[auth_level]){ echo "<option value=$row[auth_level] selected>$row[auth_level] - $row[descrip]</option>"; }else{ echo "<option value=$row[auth_level]>$row[auth_level] - $row[descrip]</option>"; } } echo "</select>"; ?> </td> </tr>Any help would be greatly appreciated how come the lines: PHP Code: echo $form['catcher_id']; $catcher_names = $form['catcher_id'] foreach($catcher_names as $val) { echo "testing loop"; } produce nothing?? its not doing the foreach. $form['catcher_id']; is a dropdown list containing catcher names please help? thanks Hi, I'll try and keep this brief but it really is a case of searching for needle in a haystack... I've just started working for a UK charity whose main activity is supported by a massive website. Stacks of information sheets, 3 distinct forums (each about the size of Freaks here), numerous blogs, events pages, webinars etc. etc. Essentially, it is an on-line free community that serves the needs of a very sizeable percentage of man and womankind. Like many charities it relies on gratis contributions from those with time or money to spare. I don't have either, but I do believe in what they are doing. The problem is that that the site itself has grown or rather mutated to become a huge data repository, built from a number of contributors with different skills sets and ways of doing things. At the back of it lurks a CMS which staff (a limited number) use to update site content. 95% of the site is in PHP, with a host of other ingredients thrown into the pot (Tiny-MCE, Perl, Ajax, Fancybox, html5, flash, blah and blah). The problem is in the CMS when someone tries to enter a date beyond 2014 there is no option to do so - -.i.e. that's as far as the dropdown goes. I can see the problem but can't find the offending script/files. Anywhere. Here's a snippet from the viewable source for those with keener eyesight:
<form action="index.php?content=data_universal_new&table=events" method="post" ENCTYPE="multipart/form-data" > <input type="hidden" name="change" value=""> <table summary="" border="0"> <tr> <td valign="top">Title</td> <td><input type="text" name="titlex" size="30" maxlength="256" value=""></td> </tr> <tr> <td valign="top">Microsite</td> <td> <select name="micrositex"> <option value="INF UK" > INF UK</option> <option value="MTL" > MTL</option> <option value="ACE BABES" > ACE BABES</option> </select> </td> </tr> <tr> <td valign="top">Short description</td> <td><textarea name="short_descriptionx" cols="40" rows="5" ></textarea></td> </tr> <tr> <td valign="top">Thumbnail</td> <td> <input type="file" name="graphic[thumbnailx]"> <input type="checkbox" name="deletepic[thumbnail]" value="yes"> Delete? </td> </tr> <tr> <td valign="top">Main page</td> <td> <div id="tm"><textarea id="elm1" name="main_pagex" style=" height: 400px;width:460px;" class="mceEditor"></textarea></div> <p> <script type="text/javascript"> function toggleEditor(id) { if (!tinyMCE.getInstanceById(id)) tinyMCE.execCommand('mceAddControl', false, id); else tinyMCE.execCommand('mceRemoveControl', false, id); } </script> <div><a href="javascript:toggleEditor('elm1');">[Add/Remove editor]</a></div> <a href="upload.php" title="Upload Documents" class="iframe">Upload Documents</a> <span class="tooltip" title="Click here to start the document uploader,The documents will be available to link to in the link list drop-down"> <img src="../images/help.png" border="0" width="15" height="15" alt="" align="top" > </span> </p> </td> </tr> <tr> <td valign="top">Date</td> <td> <select name="date[4]"> <option >1</option> <option >2</option> <option >3</option> <option >4</option> <option >5</option> <option >6</option> <option >7</option> <option >8</option> <option >9</option> <option >10</option> <option >11</option> <option >12</option> <option >13</option> <option >14</option> <option >15</option> <option >16</option> <option >17</option> <option >18</option> <option >19</option> <option >20</option> <option >21</option> <option >22</option> <option >23</option> <option >24</option> <option selected>25</option> <option >26</option> <option >27</option> <option >28</option> <option >29</option> <option >30</option> <option >31</option> </select> <select name="date[3]"> <option value="1" >Jan</option> <option value="2" >Feb</option> <option value="3" >Mar</option> <option value="4" >Apr</option> <option value="5" >May</option> <option value="6" >Jun</option> <option value="7" >Jul</option> <option value="8" >Aug</option> <option value="9" >Sep</option> <option value="10" >Oct</option> <option value="11" selected>Nov</option> <option value="12" >Dec</option> </select> <select name="date[5]"> <option value="2002" >2002</option> <option value="2003" >2003</option> <option value="2004" >2004</option> <option value="2005" >2005</option> <option value="2006" >2006</option> <option value="2007" >2007</option> <option value="2008" >2008</option> <option value="2009" >2009</option> <option value="2010" >2010</option> <option value="2011" >2011</option> <option value="2012" >2012</option> <option value="2013" >2013</option> <option value="2014" selected >2014</option> </select> </td></tr> <tr> <td> </td> <td><input type="submit" value="Submit"></td> </tr> </table> </form> </div> <div id="man_div"> <p><a href="index.php?content=data_universal_new&add=yes&table=events">Add event</a></p>Hmm. Any thoughts would be greatly appreciated. Edited by Ch0cu3r, 25 November 2014 - 07:15 AM. I have the following code which works (thanks to PHP Freaks members)
foreach ($swap["sims"] as $swap) { echo $swap['voice'] . "</br>"; }But I can't figure out why the following doesn't work <select name="sim_for_swap[]"> <?php foreach ($swap["sims"] as $swap) { ?> <option value="<?php echo $swap['voice'];?>"><?php echo $swap['voice'];?></option> <?php } ?> the line : echo $form['catcher_id'] gives me a dropdown list when i choose another item from the dropdown i want to do a few things but my code not working: $selected_catcher = $form['catcher_id']; foreach($selected_catcher as $val) { $catcher_name = $val->getName(); echo $catcher_name." ".$val->getId(); if ($catcher_name = "zed-catcher") { echo $form['service_code']->renderLabel(); echo $form['service_code']->renderError(); echo $form['service_code']; } } please help? thanks I have a form with dropdown list that is populated with values from Sql Server table. Now i would like to use this selected item in SQL query. The results of this query should be shown in label or text field. So when a user selects item from dropdown menu, results from SQL query are shown at the same time. I have two dropdown list at the moment in my form. First one gets all values from a column in table in SQL Server. And the second one should get a value from the same table based on a selection in first dropdown list.
When i load ajax.php i get 2 error mesages: This is my code so far. I have tried to do it with this Ajax script. But i can only get first dropdown to work. The second dropdown(sub_machinery) does not show values, when first dropdown item is selected. The second dropdown should show values from databse table with this query( *$machineryID* is first dropdown selected item): SELECT MachineID FROM T013 WHERE Machinery=".$machineryID. Index.php <!doctype html> <?PHP $server = "server"; $options = array( "UID" => "user", "PWD" => "pass", "Database" => "database"); $conn2 = sqlsrv_connect($server, $options); if ($conn2 === false) die("<pre>".print_r(sqlsrv_errors(), true)); echo " "; ?> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <section id="formaT2" class="formaT2 formContent"> <div class="row"> <div class="col-md-2 col-3 row-color remove-mob"></div> <div class="col-md-5 col-9 bg-img" style="padding-left: 0; padding-right: 0;"> <h1>Form</h1> <div class="rest-text"> <div class="contactFrm"> <p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p> <form action="connection.php" method="post"> <div>machinery</div> <select id="machinery"> <option value="0">--Please Select Machinery--</option> <?php // Fetch Department $sql = "SELECT Machinery FROM T013"; $machanery_data = sqlsrv_query($conn2,$sql); while($row = sqlsrv_fetch_array($machanery_data) ){ $id = $row['Id']; $machinery = $row['Machinery']; // Option echo "<option value='".$id."' >".$machinery."</option>"; } ?> </select> <div class="clear"></div> <div>Sub Machinery</div> <select id="sub_machinery"> <option value="0">- Select -</option> </select> <input type="submit" name="submit" id="submit" class="strelka-send" value="Insert"> <div class="clear"> </div> </form> </div> </div> </div> </div> </section> </script> <script type="text/javascript"> $(document).ready(function(){ $("#machinery").change(function(){ var machinery_id = $(this).val(); $.ajax({ url:'ajaxfile.php', type: 'post', data: {machinery:machinery_id}, dataType: 'json', success:function(response){ var len = response.length; $("#sub_machinery").empty(); for( var i = 0; i<len; i++){ var machinery_id = response[i]['machinery_id']; var machinery = response[i]['machinery']; $("#sub_machinery").append("<option value='"+machinery_id+"'>"+machinery+"</option>"); } } }); }); }); </script> </body> </html> Ajaxfile.php <?php $server = "server"; $options = array( "UID" => "user", "PWD" => "pass", "Database" => "database"); $conn2 = sqlsrv_connect($server, $options); if ($conn2 === false) die("<pre>".print_r(sqlsrv_errors(), true)); echo " "; $machineryID = $_POST['machinery']; // department id $sql = "SELECT MachineID FROM T013 WHERE Machinery=".$machineryID; $result = sqlsrv_query($conn2,$sql); $machinery_arr = array(); while( $row = sqlsrv_fetch_array($result) ){ $machinery_id = $row['ID']; $machinery = $row['MachineID']; $machinery_arr[] = array("ID" => $machinery_id, "MachineID" => $machinery); } // encoding array to json format echo json_encode($machinery_arr); ?>Edited May 6, 2019 by davidd Hi freaks, I'm new to php first of all. I'm dynamically binding a dropdownlist with mysql database . After the user selects an item from it , I want to match that item with another table so as to populate another database. The code I'm using to populate dropdown: Code: [Select] <?php $con = mysql_connect("localhost","root",""); if(!$con) { die ('Can not connect to : '.mysql_error()); } mysql_select_db("ims",$con); $result=mysql_query("select cat_id,cat_name from category"); echo "<select name=cat>"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[cat_id]> $nt[cat_name] </option>"; } echo "</select>"; mysql_close($con); ?> Now after the user selects any one of the item , I want to bind another dropdown on the same page using such query like $result=mysql_query("select subcategory.sc_id,subactegory.sc_name from subcategory,category where subcategory.sc_id=$nt[cat_id]"); Please anyone tell me the logic and code to do it. Also tell me do I need an intermediate page to post the 1st dropdown value and then continue with 2nd dropdown. I couldn't figure out the concept anyhow. Help on this will be highly appreiable . (Tell me if I'm not clear with my question) Ok i'm trying to make a dropdown menu to sort content on my site. The dropdown looks like this: Code: [Select] <select> <option selected="selected">Date</option> <option>Clicks</option> <option>Ratings</option> <option>Comments</option> </select> The content that the dropdown needs to sort are taken from my MySQL database and put on the site using these lines of code: (there is alot more code to do other stuff aswell, but that isn't importand for my problem) $sql = "SELECT * FROM links LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $dyn_table = '<table class="main_table">'; while ($row = mysql_fetch_assoc($result)) { etc... The dropdown and the php script that inserts the content on the page are (and needs to be) in two seperate files. So how can i make so that when a user selects "Sort by: Clicks" for example, the page refresh and maybe inserts "ORDER BY clicks ASC" into "$sql = "SELECT * FROM links LIMIT $offset, $rowsperpage";"or something?? I'm really open for ideas here, Javascript and jQuery is also welcome if it needs to be used! I am not a developer but I can modify code to work for me. The following code works on my test machine (Windows 10, IIS, PHP 7.4) but doesn't work on my website (cPanel, Some version of Linux, PHP 7.4). The two dropdowns are for State and City. You are supposed to be able to select the state and then select a city from that state then bring up a report for craft breweries in the city. When selecting State from the first dropdown, the page refreshes, the URL is correct with the reports.php?cat=<STATE> so $cat is being set, but the first dropdown no longer has the state selected and the second dropdown is populated with All cities and not just one ones from the selected state. Any ides why this is working fine on one machine and not the other? Selected code from reports.php
<?php
?>
/////// for second drop down list we will check if State is selected else we will display all the cities/////
echo "<form method=post action='brewerylistbycity.php'>";
}
////////// Starting of second drop downlist /////////
//// End Form /////
I currently have a web page which the user selects the value from the drop menu. I then want that value to go into another form in the same page inside a hidden field. Which will then be submitted to the processing php. The second option is the user selects the drop menu then the processing php would have to grab the value without a submit button because I am using a save button on the same page in the other form. Any help Hi, I have a dropdown box, and I want it so that if dropdownbox.value='selected:' then show error message. The checkbox is dynamically populated. Pleaee Advise, - stuart Hi, I'm wondering how i would go about making a drop down menu in php to insert the option into users > Maint of my database, if anyone has any tutorials or examples it would be great, Thanks a lot. I'm trying to have it shows the data from my array as each option will have a value of the user's id and the text for the option will be their name. As of right now it only shows the last person. Code: [Select] <?php echo form_label('Recipient', 'recipient'); ?> <?php $data = array( 'name' => 'to', 'class' => 'required' ); <?php echo form_label('Recipient', 'recipient'); ?> <?php $data = array( 'name' => 'to', 'class' => 'required' ); $options = array(); foreach($users AS $user) { $options = array ( $user->user_id => $user->first_name.' '.$user->last_name ); } ?> <?php echo form_dropdown($data, $options); ?> Hi all. I kinda need some from a more advanced PHP expert. I have a table that displays time slots that are available to be booked. As you can see I have a table with two columns, The 1st column displays the times and the 2nd column has a link that says 'Available'. I want to be able to put all this in a select dropdown box to save space, but how can I do this?
<?php $doc = JFactory::getDocument(); $doc->addStyleSheet(JURI::root(false)."components/com_pbbooking/user_view.css"); ?> <style> table#pbbooking td, table#pbbooking th {padding: 0em;} </style> <h1><?php echo JText::_('COM_PBBOOKING_DAY_VIEW_HEADING').' '.Jhtml::_('date',$this->dateparam->format(DATE_ATOM),JText::_('COM_PBBOOKING_DAY_VIEW_DATE_FORMAT'));?></h1> <table id="pbbooking"> <!-- Draw header row showing calendars across the top....--> <tr> <th></th> <!-- first column left blank to display time slots --> <?php foreach ($this->cals as $cal) :?> <th><?php echo $cal->name;?></th> <?php endforeach;?> </tr> <!-- draw table data rows --> <?php while ($this->day_dt_start <= $this->dt_last_slot) :?> <?php $slot_end = date_create($this->day_dt_start->format(DATE_ATOM),new DateTimezone(PBBOOKING_TIMEZONE));?> <?php $slot_end->modify('+ '.$this->config->time_increment.' minutes');?> <tr> <th><?php echo Jhtml::_('date',$this->day_dt_start->format(DATE_ATOM),JText::_('COM_PBBOOKING_SUCCESS_TIME_FORMAT'));?></th> <?php foreach ($this->cals as $cal) :?> <td class="pbbooking-<?php echo (!$cal->is_free_from_to($this->day_dt_start,$slot_end)) ? 'free' : 'busy';?>-cell"> <?php if ($this->day_dt_start>date_create("now",new DateTimeZone(PBBOOKING_TIMEZONE)) && !$cal->is_free_from_to($this->day_dt_start,$slot_end)) :?> <a href="<?php echo JRoute::_('index.php?option=com_pbbooking&task=create&dtstart='.$this->day_dt_start->format('YmdHi').'&cal_id='.$cal->cal_id);?>"> <?php echo (!$cal->is_free_from_to($this->day_dt_start,$slot_end)) ? JText::_('COM_PBBOOKING_FREE') : JText::_('COM_PBBOOKING_BUSY');?> </a> <?php else :?> <?php echo JText::_('COM_PBBOOKING_BUSY');?> <?php endif;?> </td> <?php endforeach;?> </tr> <?php $this->day_dt_start->modify('+ '.$this->config->time_increment.' minutes');?> <?php endwhile;?> <!-- end draw table data rows--> </table> I cannot get this to echo out the day from mysql. Basically I need it to say if the field is empty show Day as selected=selected with the list of days, but if it isn't empty, make the day in mysql set to selected=selected. I have also check the query in mysql so I know the query is correct. <?php if (empty($r['dayofbirth'])) { $isset = isset($_POST['date_of_birth']); for ($day = 1; $day <= 31; ++$day) { echo '<option'; if ($isset && $_POST['date_of_birth'] === $day) { echo 'selected="selected"'; } echo ">${day}</option>"; } else { echo "${r['dayofbirth']}"; } ?> Hi guys, I've got this php script which display the users of my database in a dynamic dropdown: <?php include "leadscript/connect_to_mysql.php"; $canvass_name=""; $sql = mysql_query("SELECT * FROM csj_canvasser"); $appointmentCount6 = mysql_num_rows($sql); // count the output amount if ($appointmentCount6 > 0) { while($row = mysql_fetch_array($sql)){ $c_employee = $row["c_employee"]; $canvass_name .='<option value="' . $c_employee . '">' . $c_employee . ' </option>'; } } ?> <form> <select name="c_employee"> <option value="">Select a person:</option> <?php echo $canvass_name; ?> </select> </form> I was wondering if there's a way I can write a code to GET value I select from the dynamic dropdown and use it to write a select query. Thank Hi, I am trying to call the data from Mysql but I am getting an empty drop down list, this is the code: mysql: Code: [Select] create table years ( yearID integer auto_increment, year varchar(30), primary key (yearID) ); insert into years (yearID, year) values ('1', '2007-2008'); insert into years (yearID, year) values ('2', '2008-2009'); insert into years (yearID, year) values ('3', '2009-2010'); insert into years (yearID, year) values ('4', '2010-2011'); insert into years (yearID, year) values ('5', '2011-2012'); insert into years (yearID, year) values ('6', '2012-2013'); PHP: Code: [Select] <?php require_once('../Connections/connection.php'); ?> <?php $result = @mysql_query( "select yearID, year, from sss.years"); print "<p>Select a year:\n"; print "<select name=\"yearID\">\n"; while ($row = mysql_fetch_assoc($result)){ $yearID = $row[ 'yearID' ]; $year = $row[ 'year' ]; print "<option value=$yearID>$year\n"; } print "</select>\n"; print "</p>\n"; ?> Thank you! |