PHP - Echo Dropdown Selection
I'm trying to do a couple of things using the dropdown list below. I have a table with id, rank, and amount.
1. I need to echo the amount that corresponds to the rank selected from the dropdown list. 2. I also need to put the amount value in a variable so I can do some math with it later on. I've got the dropdown pulling the rank values from the db just not sure how to make it do the rest I'm trying to do. Any help would be greatly appreciated Code: [Select] <?php db($host,$db_name,$user,$pass); function db($host,$db_name,$user,$pass) { global $link; $link=mysql_connect ("$host","$user","$pass"); if(!$link){die("Could not connect to MySQL");} mysql_select_db("$db_name",$link) or die ("could not open db".mysql_error()); } $sql="SELECT * FROM dlaw"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $rank=$row["rank"]; $options.="<OPTION VALUE=\"$rank\">".$rank.'</option>'; } ?> <SELECT NAME=rank><OPTION VALUE=0>Select Rank<?=$options?></SELECT> Similar TutorialsHello everyone, i'm kinda stuck here although i had this working before, i don't know what's wrong here. Basically i've got a form with a drop-down menu, that menu has 3 values, x,y and z. Now that form is working with post, sending me to the next page where i have a set of arrays and a echo which should print some text based on the option chosen from the drop down menu. my code in the form is this: Code: [Select] <select name="value1" id="value1"> <option value="x">x</option> <option value="y">y</option> <option value="z">z</option> </select> the array on the next page: Code: [Select] $arrayvalue = array("x"=>"test1", "y"=>"test2", "z"=>"test3"); and the echo: Code: [Select] <?php echo $arrayvalue["value1"];?> That leaves me with an empty page. What am i doing wrong here ? am i missing something ? any help is greatly appreciated. Sabrina Need some help to define the php logic to echo back a selection if/when chosen from the drop down menu. Note: I'm using just a small subset of the USA states to show setup and what I'm trying to accomplish - it will be the same for all the other drop down menus as well.... Here's the example code: Code: [Select] <form name="form1" method="post" action="<?php $_SERVER[PHP_SELF]; ?>"> <select name="address_state" id="address_state" tabindex="25"> <option value="" selected> - Select State -</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option> <option value="CO">Colorado</option> <option value="CT">Connecticut</option> <option value="DC">District of Columbia</option> </select> <input name="submit" type="submit" value="submit" tabindex="900" id="submit" /> </form> Issue: I have validation (logic) define to check all the (other) text, radio, and check boxes of the form and that all works great! However, any time one of those particular fields fail validation, any selected "drop down" item is lost - it defaults back to no selection and the user has to make all drop down menu selections again. Can someone provide method to echo back - in php being the page reloads to itself - the drop down selection as in the example above? Thanks! I have a dropdown menu in a form where users must select their instrument: here's the dropdown: Code: [Select] <label for="instrument"><span class='red'>*</span>Your main instrument</label> <select name="instrument" id="instrument"> <option value="select" selected="selected">Select your instrument…</option> <option value="bassoon">Bassoon</option> <option value="cello">Cello</option> <option value="clarinet">Clarinet</option> <option value="double_bass">Double Bass</option> <option value="flute">Flute</option> <option value="french_horn">French Horn</option> <option value="oboe">Oboe</option> <option value="percussion">Percussion</option> <option value="trombone">Trombone</option> <option value="trumpet">Trumpet</option> <option value="tuba">Tuba</option> <option value="viola">Viola</option> <option value="violin">Violin</option> <option value="other">Other</option> </select> I want to validate to ensure that the user has selected an instrument from the list. I was going to do something like this: Code: [Select] // validate for instrument selection if(!isset($_POST['instrument'])) { $serrors[] = 'Please select your instrument.'; } else { $_SESSION['instrument'] = $_POST['instrument']; } The problem is, the session variable gets set if the user bypasses the menu altogether, leaving the default 'Select your instrument...' option (at the top) as it is. How can I ensure a selection other than the 'Select your instrument...' at the top of the list? Thanks in advance. Hi all, I'm trying to display a form based on a dropd own selection. The drop down is propagated from a database query. I have the drop down list displaying, but when I click submit, I can't get the result to display. Code: [Select] $sql="SELECT id, company_name FROM webprojects ORDER BY company_name ASC"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["id"]; $company_name=$row["company_name"]; $options.="<OPTION VALUE=\"$id\">".$company_name; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Dynamic Drop Down Menu</title> </head> <body> <form id="form1" name="form1" method="post" action="selected.php"> <SELECT NAME=id> <OPTION VALUE=0>Choose <?=$options.="<OPTION VALUE=\"$id\">".$company_name.'</option>';?> </SELECT> <label> <input type="submit" name="submit" id="submit" value="Submit" /> </label> </form> </body> </html> Any help is greatly appreciated I have 10 tables in my database. I want load these 10 table names into a drop-down list. If a user selects a value form the drop-down list, a form should be displayed with related to the table. (form fields should be appropriate table's column headers) When user fill the form and submitted, data should be saved in the selected table. Give me a code example to do this. 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']}"; } ?> 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 OK, have no idea what's going on... I've done this a million times... why wont this output!?? I must have a major brain meltdown and dont know it yet!!! Code: [Select] <?php // this echoes just fine: echo $_POST['testfield']; // but this wont echo: echo if (isset($_POST['testfield'])) { $_POST['testfield'] = $test; } echo $test; /// or even this DOESNT echo either!: $_POST['testfield'] = $test; echo $test; ?> Hi All, I'm trying to echo the response from an SLA query, the query works and returns the data when I test it on an SQL application.. but when I run it on my webpage it won't echo the result. Please help? <?php $mysqli = mysqli_connect("removed", "removed", "removed", "removed"); $sql = "SELECT posts.message FROM posts INNER JOIN threads ON posts.pid=threads.firstpost WHERE threads.firstpost='1'"; $result = mysqli_query($mysqli, $sql); echo {$result['message']}; ?> 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) So I need to echo a row from my database with php, but where i need to echo is already inside an echo. This is my part of my code: $con = mysql_connect("$host","$username","$password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("main", $con); $result = mysql_query("SELECT * FROM Vendor"); while($row = mysql_fetch_array($result)) { //I need to echo right here .................. but I get a blank page when I try this. Please Help. echo '<option value=$row['vendor_id']>'; echo $row['vendor_id']; echo '</option>'; } mysql_close($con); Result: A Blank page. Thanks in advance! I have a log system that allows 10 logs on each side(Left and right). I am trying to make it so that the left side has the 10 most recent logs, then the right as the next 10. Any ideas? Hi
I try to echo out random lines of a html file and want after submit password to whole content of the same html file. I have two Problems.
1st Problem When I echo out the random lines of the html file I don't get just the text but the code of the html file as well. I don't want that. I just want the text. How to do that?
for($x = 1;$x<=40;$x++) { $lines = file("$filename.html"); echo $lines[rand(0, count($lines)-1)]."<br>"; }I tried instead of "file("$filename.html");" "readfile("$filename.html");" But then I get the random lines plus the whole content. Is there anything else I can use instead of file so that I get the random lines of text without the html code?P.S file_get_contents doesn't work either have tried that one. 2nd Problem: As you could see in my first problem I have a file called $filename.html. After I submit the value of a password I want the whole content. But it is like the program did forget what $filename.html is. How can I make the program remember what $filename.html is? Or with other words how to get the whole content of the html file? My code: if($_POST['submitPasswordIT']){ if ($_POST['passIT']== $password ){ $my_file = file_get_contents("$filename.html"); echo $my_file; } else{ echo "You entered wrong password"; } }If the password isn't correct I get: You entered wrong password. If the password is correct I get nothing. I probably need to create a path to the file "$filename.html", but I don't know exactly how to do that. 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 /////
how do i make it so that all the items appear on eventname Code: [Select] <?php require "scripts/connect.php"; $query = mysql_query("SELECT eventname FROM events"); while ($row = mysql_fetch_array($query)){ $eventname = $row['eventname']; $form = "<form action='addcat.php' method='post'> <table> <tr> <td>SELECT EVENT</td> <td> <select value='$eventname'> <option value='$eventname'>$eventname</option> </select> </td> </tr> <tr> <td>SELECT VENUE</td> <td> <select> <option></option> </select> </td> </tr> <tr> <td>SELECT CATEGORY</td> <td> <select> <option>FOOTBALL</option> <option>BASKETBALL</option> <option>HOCKEY</option> <option>DRAMA</option> <option>MUSIC</option> <option>DANCE</option> <option>VISUAL ARTS</option> </select> </td> </tr> </table> </form>"; echo "$form"; } ?> Is there a simple way to keep a yes or no selection in a drop down menu after the page is submitted? Code: [Select] <form name="survey" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Satisfied<select name="satisfied"> <option value="yes"> yes </option> <option value="no"> no </option> </select> <input name="submit" type="submit" value="Submit"> </form> Hi! I want to have a first page on my website where you as visitor can choose the preferred language. No login at all. I want to save this choice in a cookie or something. The easiest way So next time he/she visits, the language choice is already made for he/she. Any good suggestions for a php code that works? Hi all, I will wish to modify this code to get a choice of multiple selection, so I can select multiple category and not a single as it is currently. Here is the original code: Code: [Select] {if $step==1} <div class="form_content"> <form name="newad" method="post" action="newad.php?step=1"> <label>{$lng.listings.category}<span class="mandatory"> *</span></label> <select id="category" name="category"> <option value="">{$lng.listings.category}</option> {foreach from=$categories item=v name=cat} {if $v.parent==1} <optgroup label="{$v.str}{$v.name}"> {else} <option value="{$v.id}" {if $tmp.category==$v.id}selected="selected"{/if}>{$v.str}{$v.name|escape:"html"}</option> {if ( $smarty.foreach.cat.index!=$categories|@count-1 && $v.parent_id!=$categories[$smarty.foreach.cat.iteration].parent_id) || ($smarty.foreach.cat.index==($categories|@count-1) && $v.parent_id!=0)} </optgroup> {/if} {/if} {/foreach} </select> {* ----------------------- levels drop down categories --------------------- *} {* <div> <select id="category1" name="category1" class="levels" onChange="chooseCateg(this.form, 1)"> <option value="">{$lng.listings.category}</option> {foreach from=$categories item=v name=cat} {if $v.parent_id==0} <option value="{$v.id}">{$v.name|escape:"html"}</option> {/if} {/foreach} </select> </div> <div style="display: none;" id="level2"> <select id="category2" name="category2" class="levels" onChange="chooseCateg(this.form, 2)"> </select> </div> <div style="display: none;" id="level3"> <select id="category3" name="category3" class="levels" onChange="chooseCateg(this.form, 3)"> </select> </div> <div style="display: none;" id="level4"> <select id="category4" name="category4" class="levels" onChange="chooseCateg(this.form, 4)"> </select> </div> <div style="display: none;" id="level5"> <select id="category5" name="category5" class="levels" onChange="chooseCateg(this.form, 5)"> </select> <input type="hidden" name="category" id="category" /> </div> *} <br/><br/> <div class="buttons"><strong><input type="submit" name="Choose_categ" id="Choose_categ" value="{$lng.listings.next_step}" /></strong></div> <div class="clearfix"></div> </form> </div> </div> {* end form_container *} {/if} {* end step1 *} Thank you for your help |