PHP - Select Form Reload
Can someone help me with a code , which can make a select form to reload the page or the form in the moment when a option from it , is been selected.
i try it whit onClick="document.location.reload()"; but doesn't work properly.
thx
Similar TutorialsOk, so I am trying to make a database with PHP and I hit a snag. First off, here is the code in question. Code: [Select] <?php //GET CONFIG include_once('../scripts/config.inc.php'); //SET VARIABLES TO SOMETHING A BIT SHORTER FOR THE DATABASE OPTIONS $host = $config['db_settings']['db_host']; $user = $config['db_settings']['db_user']; $pass = $config['db_settings']['db_pass']; $prefix = $config['db_settings']['db_prefix']; $db = $config['db_settings']['db_name']; $con = mysql_connect($host, $user, $pass); $select_db = mysql_select_db($db); $c_tables = ''; $c_database = "CREATE DATABASE IF NOT EXISTS $db;"; //NOW CONNECT TO THE SERVER if ($con){ echo('Connected Succesfully to the Server <br />'); } else die('Could not connect to database: ' . mysql_error()); //NOW CONNECT TO THE DATABASE if ($select_db){ echo('Selected Database Succesfully <br />'); } //IF DATABASE COULD NOT BE SELECTED THEN TRY TO MAKE IT else if(!$select_db){ echo('Could not select the databse. Trying to create it... <br />'); if(mysql_query($c_database)){ echo('Database has been created...<br />'); if($select_db){ echo('Database Has Been Selected.'); } else die('Can not select database!' . mysql_error()); } else die('Could not created the database!'); } else die ('Could not create or select the database!: ' . mysql_error() . '<br />'); //NOW CREATE THE TABLES mysql_close; ?>The problem is, that if I drop the database to test that it creates the database, it does indeed make it, but after it makes the database it will not select it until I reload the page and intern the script. So this is the output I would get. (The fact that there is no error is what is causing me to be confused) 1st load that makes new database: Quote Connected Succesfully to the Server Could not select the databse. Trying to create it... Database has been created... Can not select database! 2nd load that should not be needed to select the database: Quote Connected Succesfully to the Server Selected Database Succesfully For those willing to run this sample problem, reference the 2 files below. You will need to create an Excel file called blank.xls and put it in the same directory as these 2 files. When you launch indexTest.php, it will load the formTest.html.php page. This form has 3 options on it: 1. Export Excel - will prompt you to download the Excel file you made and should clear the error message on the form 2. Test Flag - tests the functionality of clearing the error message, shows what should happen when the Export Excel option is selected 3. Reset - resets back to initial state I really need the Export Excel option to be allow the form to reload properly. This is a problem that I have been wresting with for awhile and need a solution. Hopefully someone can give me some insight on how to get this to work. indexTest.php <?php $frmErrorLevel=1; $frmErrMsg='Error Level 1 indicated'; if(isset($_POST['action']) && $_POST['action']=='submitted') { if(isset($_POST['ExportCarrier'])) { $download_filename = "blank.xls"; $FileInfo = pathinfo($download_filename); // fix for IE catching or PHP bug issue header("Pragma: public"); header("Expires: 0"); // set expiration time header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // browser must download file from server instead of cache // force download dialog header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-type: application/x-msexcel"); header("Content-Type: application/download"); // use the Content-Disposition header to supply a recommended filename and // force the browser to display the save dialog. header("Content-Disposition: attachment; filename=".$download_filename.";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($download_filename)); @readfile($download_filename); //ob_end_clean(); $frmErrorLevel=0; } if(isset($_POST['Test_Flag']) && $_POST['Test_Flag']=='Test Flag'){ $frmErrorLevel=0; $frmErrMsg=''; } } include ('formTest.html.php'); exit(); ?> formTest.html.php <?php ini_set ("display_errors", "1"); error_reporting(-1); echo "<pre>"; echo "--> Form data (POST) <-- <br>"; print_r ($_POST); echo "--> Form data (GET) <-- <br>"; print_r ($_GET); echo "</pre>"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Export Trucking Data</title> </head> <body> <h1>Export Data</h1> <form action="" method="post"> <?php if($frmErrorLevel>0) { echo '<font color=#CC6600 size=+1>'.$frmErrMsg.'</font><br><br>'; } ?> <table width="650" border="0"> <caption> <font size="+3">Data Export Options</font> </caption> <tr> <td> <?php if($frmErrorLevel==1) print '<img src= "..\..\images\rdx.gif">'; else echo " "; ?> </td> <td width="275"><label> Carrier table data</label></td> <td width="100"><input name="ExportCarrier" type="submit" value="Export Excel"></td> <td> <div align="center"> <td><input type="submit" name="Test Flag" value="Test Flag"></td> <td> <div align="center"> <td><input type="submit" name="Reload Form" value="Reset"></td> </tr> </table> <div> <input type="hidden" name="action" value="submitted" /> </div> </form> </body> </html> Hi all, I have a listbox on a form that gets it's data from a MySQL query, all of which is working fine. I make a selection in my listbox and then submit the form which is reloading itself. However on the reload I would like the listbox to display the selection made by the user, instead it reverts to the first item in the list. I have included the code, if someone could point me in the right direction that would be great. I have searched these forums and tried a few things that are suggested but I still can't get it to work. Code: [Select] <?php include("dbconn.php"); $query = "SELECT location.location_name, printer.model_number, location_printer.serial_number" . " FROM location, printer, location_printer" . " WHERE location.location_id = location_printer.location_id" . " AND printer.printer_id = location_printer.printer_id"; $result = mysql_query($query); echo "<table>"; echo "<th>Location</th>"; echo "<th>Printer</th>"; echo "<th>Serial No</th>"; while(($row = mysql_fetch_array($result))) { echo "<tr>"; echo "<td>".$row['location_name']."</td>"; echo "<td>".$row['model_number']."</td>"; echo "<td>".$row['serial_number']."</td>"; echo "</tr>"; } echo "</table>"; if(isset($_POST['location'])) { $loc = $_POST['location']; echo "<p>Location = <b> $loc </b>"; } ?> <html> <head> <title>Loop Results</title> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <table> <tr> <td> <?php $query = "SELECT * FROM location ORDER BY location_name"; $result = mysql_query($query); $default = $_POST['location']; echo "<p>Location: "; echo "<select name='location'>"; while(($row = mysql_fetch_array($result))) { echo "<option value='{$row['location_id']}"; //Selected value if($row['location_id'] == $default) echo " selected "; echo "'>{$row['location_name']}</option>\n"; } echo "</select>"; ?> </td> </tr> <tr> <td> <input type="submit" /> </td> </tr> </table> </form> </body> </html> The Script:
<h1>Do Add a Message to the MySQL Database</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <textarea name="message"></textarea> <br/> <input type="submit" name="submit"/> </form> <?php // The Connection to the Database // Taken Out ?> <?php // To insert the text data into the MySQL database. if(isset($_POST['submit'])){ $tqs = "INSERT INTO messages (`message`) VALUES ('{$_POST['message']}')"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); } ?> <?php // To select the text data from the MySQL database. $tqs = "SELECT * FROM messages"; $tqr = mysqli_query($dbc, $tqs); // To print out the text data inside of table on the page. echo "<h1>This Is Where the Messages Gets Printed on Screen</h1>"; echo "<table><tr><td>ID</td><td>The Message</td></tr>"; while($row = mysqli_fetch_assoc($tqr)){ echo "<tr><td>" . $row['id'] . "</td><td>" . $row['message'] . "</td></tr>"; } echo "</table>"; ?>1. When I have added text with the form to the MySQL database... 2. ... and I have clicked on "page reload" in Firefox to reload the page... 3. ... then the before submitted text gets submitted again to the MySQL database. So basically, add text with the form to the MySQL database, reload the page in Firefox, and the before added text will get submitted to the MySQL database again. My Question Is: What is the proper way to avoid this? Edited by glassfish, 06 October 2014 - 10:18 AM. Hello, first time poster.. I've looked the web over for a long time and can't figure this one out. - Below is basic code that successfully checks MySQL for a match and displays result. I was debugging and forced the "height" and "width" to be 24 and 36 to make sure that wasn't the problem. That's good.. - I'd like to give the user ability to select width and height from a form.. and have it do an onchange this.form.submit so the form can be changing as fields are altered (thus the onchange interaction) - In a normal coding environment I've done this numerous times with no "Page cannot be displayed" problems. It would simply change one select-option value at a time til they get down the form and click submit... but in WordPress I'm having trouble making even ONE single onchange work! - I've implemented the plugins they offer which allows you to "copy+paste" your php code directly into their wysiwyg editor. That works with basic tests like my first bullet point above. - I've copied and pasted the wordpress url (including the little ?page_id=123) into the form "action" url... that didn't work... tried forcing it into an <option value=""> tag.. didn't work. I'm just not sure. I've obviously put xx's in place of private info.. Why does this form give me Page Cannot Be Displayed in WordPress every time? It won't do anything no matter how simple.. using onchange.. Code.. $con = mysql_connect("xxxx.xxxxxxx.com","xxxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxx", $con); $myprodwidth=24; $myprodheight=36; $result = mysql_query("SELECT * FROM product_sizes WHERE prodwidth='$myprodwidth' and prodheight='$myprodheight'"); while($row = mysql_fetch_array($result)) { echo $row['prodprice']; } mysql_close($con); <form method="post" action=""> <select name="myheight" onchange="this.form.submit();"> <option selected="selected" value="">select height</option> <option value="xxxxxxxxx.com/wordpress/?page_id=199&height=36">36</option> <option value="xxxxxxxxx.com/wordpress/?page_id=199&height=36">48</option> </select> Hi, sorry for being a newbie, but I need help setting up a dynamic form. The form will be a reservation type form. I have a single table, which I will post below. What I am looking to do is choose a month from a select box and then have it populate another select box with the available dates. Here is my table info from mysql... Code: [Select] CREATE TABLE `daterange` ( `RID` int(5) NOT NULL auto_increment, `DEND` date NOT NULL, `MONTH` varchar(50) NOT NULL, `DATE` varchar(50) NOT NULL, `SITE` varchar(50) NOT NULL, `PRICE` varchar(10) NOT NULL, `STATUS` varchar(1) NOT NULL default 'A', `FNAME` varchar(50) NOT NULL, `LNAME` varchar(50) NOT NULL, `ADDR1` varchar(50) NOT NULL, `ADDR2` varchar(50) NOT NULL, `CITY` varchar(50) NOT NULL, `STATE` varchar(2) NOT NULL, `ZIP` varchar(5) NOT NULL, `PHONE1` varchar(3) NOT NULL, `PHONE2` varchar(3) NOT NULL, `PHONE3` varchar(4) NOT NULL, `EMAIL` varchar(50) NOT NULL, PRIMARY KEY (`RID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=83 ; Here is a sample mysql dump... Code: [Select] INSERT INTO `daterange` VALUES(1, '2011-05-15', 'May 2011', '5/15-5/22/11', 'Carterville Pond/Adirondack', '$2625.00', 'N', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `daterange` VALUES(2, '2011-05-22', 'May 2011', '5/22-5/29/11', 'Carterville Pond/Adirondack', '$2625.00', 'A', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `daterange` VALUES(3, '2011-05-29', 'May 2011', '5/29-6/5/11', 'Carterville Pond/Adirondack', '$2625.00', 'A', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); Finally, here is a php form that I started to develop but it only has a single select box, which works but I wanted another select box that will filter the list down a little... Code: [Select] <? // script to display all the Available Reservations in the daterange table // connection information $hostName = "my_host"; $userName = "my_user"; $password = "my_pass"; $dbName = "my_db"; // make connection to database mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); mysql_select_db($dbName) or die("Unable to select database $dbName"); // Select all the fields in all the records of the daterange table $query = "SELECT * FROM daterange WHERE DEND > DATE(NOW()) AND STATUS='A' ORDER BY RID, DATE, SITE"; $result = mysql_query($query); // Determine the number of reservation dates $number = mysql_numrows($result); // Create drop-down menu of reservation dates print "<font size=\"3\" face=\"Arial\"><b>Select Reservation Date:</b> <form action=\"test.php\" method=\"post\"> <select name=\"RID\"> <option value=\"\">Choose One</option>"; for ($i=0; $i<$number; $i++) { $RID = mysql_result($result,$i,"RID"); $DATE = mysql_result($result,$i,"DATE"); $SITE = mysql_result($result,$i, "SITE"); $PRICE = mysql_result($result,$i, "PRICE"); print "<option value=\"$RID\">$DATE, $SITE, $PRICE</option>"; } print "</select><p align=left><label><font size=\"3\" face=\"Arial\">First Name: <input type=\"text\" name=\"FNAME\" size=\"50\" maxlength=\"50\" tabindex=\"1\"<br>"; print "<p align=left><label>Last Name: <input type=\"text\" name=\"LNAME\" size=\"50\" maxlength=\"50\" tabindex=\"2\"<br>"; print "<p align=left><label>Address Line 1: <input type=\"text\" name=\"ADDR1\" size=\"50\" maxlength=\"50\" tabindex=\"3\"<br>"; print "<p align=left><label>Address Line 2: <input type=\"text\" name=\"ADDR2\" size=\"50\" maxlength=\"50\" tabindex=\"4\"<br>"; print "<p align=left><label>City: <input type=\"text\" name=\"CITY\" size=\"50\" maxlength=\"50\" tabindex=\"5\"<br>"; print "<p align=left><label>State (abbrev.): <input type=\"text\" name=\"STATE\" size=\"2\" maxlength=\"2\" tabindex=\"6\"<br>"; print "<p align=left><label>Zip Code: <input type=\"text\" name=\"ZIP\" size=\"5\" maxlength=\"5\" tabindex=\"7\"<br>"; print "<p align=left><label>Contact Phone Number: (<input type=\"text\" name=\"PHONE1\" size=\"3\" maxlength=\"3\" tabindex=\"8\""; print "<label>)<input type=\"text\" name=\"PHONE2\" size=\"3\" maxlength=\"3\" tabindex=\"9\""; print "<label>-<input type=\"text\" name=\"PHONE3\" size=\"4\" maxlength=\"4\" tabindex=\"10\"<br>"; print "<p align=left><label>Email: <input type=\"text\" name=\"EMAIL\" size=\"50\" maxlength=\"50\" tabindex=\"11\"<br>"; print "<p align=left><label>Payment Method: <select name=\"PM\"> <option value=\"\">Choose One</option> <option value=\"Visa\">Visa</option> <option value=\"Mastercard\">Mastercard</option> <option value=\"Discover\">Discover</option>"; print "</select><p align=left><label>Credit Card Number: <input type=\"text\" name=\"C1\" size=\"4\" maxlength=\"4\" tabindex=\"12\""; print "<label> <input type=\"text\" name=\"C2\" size=\"4\" maxlength=\"4\" tabindex=\"13\""; print "<label> <input type=\"text\" name=\"C3\" size=\"4\" maxlength=\"4\" tabindex=\"14\""; print "<label> <input type=\"text\" name=\"C4\" size=\"4\" maxlength=\"4\" tabindex=\"15\"<br>"; print "<p align=left><label>Expiration Month: <select name=\"EXM\"> <option value=\"\">Choose One</option> <option value=\"January\">January</option> <option value=\"February\">February</option> <option value=\"March\">March</option> <option value=\"April\">April</option> <option value=\"May\">May</option> <option value=\"June\">June</option> <option value=\"July\">July</option> <option value=\"August\">August</option> <option value=\"September\">September</option> <option value=\"October\">October</option> <option value=\"November\">November</option> <option value=\"December\">December</option>"; print "</select><p align=left><label> Expiration Year: <select name=\"EXY\"> <option value=\"\">Choose One</option> <option value=\"2011\">2011</option> <option value=\"2012\">2012</option> <option value=\"2013\">2013</option> <option value=\"2014\">2014</option> <option value=\"2015\">2015</option> <option value=\"2016\">2016</option> <option value=\"2017\">2017</option> <option value=\"2018\">2018</option> <option value=\"2019\">2019</option> <option value=\"2020\">2020</option>"; print "</select><p align=left><label>Security Code (3 or 4 digits): <input type=\"text\" name=\"CSC\" size=\"4\" maxlength=\"4\" tabindex=\"16\"<br>"; print "<p align=left><input type=\"submit\" value=\"Book Now!\" name=\"submit\"></form>"; print " <input type=\"reset\" value=\"reset\" name=\"reset\"></form>"; // Close the database connection mysql_close(); ?> Sorry this is so long, any help would be appreciated...... -Bob Hello everyone and thank you for taking the time to read this. I am very new to coding and I am helping a friend with a website. I'm trying to create a form with a <select> dropdown and filling the options with items from a database, there are very man items so I'm not sure 100% how to go about this, I have some code but I know it's wrong I'm just looking for a nudge in the right direction, any help is appreciated the code can be found below: Code: [Select] <div style="float:left;"> <select> $team = $sql->query("SELECT * FROM teams WHERE arenaid ='$platid'"); <option value="<?echo $teams>"></option> </select> </div> Again, thank you for taking the time to read this and thank you also for any help you can provide. Hey guys i am new around here and i need some help with php. So basically i want to make a submit form and i found a problem for myself here's some images to understand what i am talking about. so here's what i've made at first. This is meant for new data. <p><label>Status :</label> <select name="status" > <option value=""></option> <option value="Ongoing">Ongoing</option> <option value="Completed">Completed</option> </select> basically what I want is when I edit the data, that selection returns the value that was stored in the database rather than just null value. See image below to understand what i am trying to do. i dont really get how to do it with select. Please help me and thanks before. Sorry if my question was already asked before. Tried to search but didnt really found what i am looking for. Hello
I'm using a small script (that I found online) to dynamically add fields to an order form.
It works well with normal text fields but I would like to use it with a select box fields (as it draws data from MySQL).
My problem is, that when the field is 'cloned' it contains the product id and not the product name - which is what the user needs to see.
<?php echo '<form method="post" name="orderform">'; // lots of form fields... echo '<div id="itemRows">'; echo 'Item quantity: <input type="text" name="add_qty" size="4" />'; $prod = "SELECT prod_id, product FROM products ORDER BY product ASC"; $prodRes = $mysqli->query($prod); echo 'Item name : <select name="add_name">'; while ($p = $prodRes->fetch_assoc()) { echo'<option value="'.$p['prod_id'].'">'.$p['product'].'</option>'; } echo '</select>'; echo '<input onclick="addRow(this.form);" type="button" value="Add row" /> (Won't be saved until you click on "Add row")'; if(isset($product['qty']) && isset($product['name'])) { ?> <p id="oldRow<?=$product['id']?>"> Item quantity: <input type="text" name="qty<?=$product['id']?>" size="4" value="<?=$product['qty']?>" /> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['add_name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete </p> </div> <?php } ?> </div> <input type="submit" value="Submit new order" name="addOrder"> </fieldset> </div> </form> <script type="text/javascript"> var rowNum = 0; function addRow(frm) { rowNum ++; var row = '<p id="rowNum'+rowNum+'">Item quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>'; jQuery('#itemRows').append(row); frm.add_qty.value = ''; frm.add_name.value = ''; } function removeRow(rnum) { jQuery('#rowNum'+rnum).remove(); } </script>I know I could use the $p['product'] value in the select box field, but when it is submiitted - it loses its $p['prod_id'] value which I need to input back into the MySQL database. I did try echo'<option value="'.$p['prod_id'].':'.$p['product'].'">'.$p['product'].'</option>';and then explode... foreach($_POST['name'] as $key => $value) { $items[] = current(explode(":", $value)); }...but clearly, that is a dreradful solution and looks awful to the user. Any thoughts, please? Thank you. Edited by tHud, 14 July 2014 - 09:31 AM. I want to update a value by setting cookie. I use this code: Code: [Select] <?php if (isset($_POST['ChangeOrdering'])) { setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000); } echo $_COOKIE["Ordering"]; ?> <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>"> Reorder messages: <select name="ChangeOrdering"> <option value="DateAdded ASC">Oldest first</option> <option value="DateAdded DESC">Newest first</option> <option value="Title ASC">By Title, A-Z</option> <option value="Title DESC">By Title, Z-A</option> </select> <input type="submit" value=" Save Settings " /> </form> Problem is that echo will return the previous cookie set, not the last value. As a matter of fact, my problem is about updating the cookie. It seems that the browser load the cookie before it is updated by the POST value. Hi i would like to submit the following code to my database as a mutiple selection box. when i submit i just get the last selected value for example if i hold ctrl and press 1 2 3 4 i am only receiving 4 on my database. any ideas? thanks in advance for any help! Code: [Select] Hours Of Availability:<br> <select name="availability" multiple="multiple" size="9"> <option value="Monday - Period 1">Monday - Period 1</option> <option value="Monday - Period 2">Monday - Period 2</option> <option value="Monday - Period 3">Monday - Period 3</option> <option value="Monday - Period 4">Monday - Period 4</option> <option value="Monday - Period 5">Monday - Period 5</option> <option value="Monday - Period 6">Monday - Period 6</option> <option value="Monday - Period 7">Monday - Period 7</option> <option value="Monday - Period 8">Monday - Period 8</option> <option value="Monday - Period 9">Monday - Period 9</option> </select> <select name="availability" multiple="multiple" size="9"> <option value="Tuesday - Period 1">Tuesday - Period 1</option> <option value="Tuesday - Period 2">Tuesday - Period 2</option> <option value="Tuesday - Period 3">Tuesday - Period 3</option> <option value="Tuesday - Period 4">Tuesday - Period 4</option> <option value="Tuesday - Period 5">Tuesday - Period 5</option> <option value="Tuesday - Period 6">Tuesday - Period 6</option> <option value="Tuesday - Period 7">Tuesday - Period 7</option> <option value="Tuesday - Period 8">Tuesday - Period 8</option> <option value="Tuesday - Period 9">Tuesday - Period 9</option> </select> <select name="availability" multiple="multiple" size="9"> <option value="Wednesday - Period 1">Wednesday - Period 1</option> <option value="Wednesday - Period 2">Wednesday - Period 2</option> <option value="Wednesday - Period 3">Wednesday - Period 3</option> <option value="Wednesday - Period 4">Wednesday - Period 4</option> <option value="Wednesday - Period 5">Wednesday - Period 5</option> <option value="Wednesday - Period 6">Wednesday - Period 6</option> <option value="Wednesday - Period 7">Wednesday - Period 7</option> <option value="Wednesday - Period 8">Wednesday - Period 8</option> <option value="Wednesday - Period 9">Wednesday - Period 9</option> </select> <select name="availability" multiple="multiple" size="9"> <option value="Thursday - Period 1">Thursday - Period 1</option> <option value="Thursday - Period 2">Thursday - Period 2</option> <option value="Thursday - Period 3">Thursday - Period 3</option> <option value="Thursday - Period 4">Thursday - Period 4</option> <option value="Thursday - Period 5">Thursday - Period 5</option> <option value="Thursday - Period 6">Thursday - Period 6</option> <option value="Thursday - Period 7">Thursday - Period 7</option> <option value="Thursday - Period 8">Thursday - Period 8</option> <option value="Thursday - Period 9">Thursday - Period 9</option> </select> <select name="availability" multiple="multiple" size="9"> <option value="Friday - Period 1">Friday - Period 1</option> <option value="Friday - Period 2">Friday - Period 2</option> <option value="Friday - Period 3">Friday - Period 3</option> <option value="Friday - Period 4">Friday - Period 4</option> <option value="Friday - Period 5">Friday - Period 5</option> <option value="Friday - Period 6">Friday - Period 6</option> <option value="Friday - Period 7">Friday - Period 7</option> <option value="Friday - Period 8">Friday - Period 8</option> <option value="Friday - Period 9">Friday - Period 9</option> </select> <select name="availability" multiple="multiple" size="9"> <option value="Saturday - Period 1">Saturday - Period 1</option> <option value="Saturday - Period 2">Saturday - Period 2</option> <option value="Saturday - Period 3">Saturday - Period 3</option> <option value="Saturday - Period 4">Saturday - Period 4</option> <option value="Saturday - Period 5">Saturday - Period 5</option> <option value="Saturday - Period 6">Saturday - Period 6</option> <option value="Saturday - Period 7">Saturday - Period 7</option> <option value="Saturday - Period 8">Saturday - Period 8</option> <option value="Saturday - Period 9">Saturday - Period 9</option> </select> <br>Press + Hold Ctrl For Multiple</p> I have a column in my db called 'Sold' which is set to Binary. I need a dropdown in my form, that converst the binary into 'True', 'False' for the options, and also selects the one that is set in the db. This is for an edit form, so will then need to adapt it for a add form. This is my code, which doesnt seem to be working <?php $id = $_GET["id"]; $sqlSold = "SELECT Sold FROM Products WHERE idProducts = $id;"; $products = [ 'True' => true, 'False' => false ]; ?> <select name="Sold"> <option selected="selected">Choose one</option> <?php foreach($products as $item){ echo "<option value='strtolower($item)'>$item</option>"; } ?> </select>
**Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.** I have a basic form that's meant to search flat files on our server. The "search engine" I created as two select lists: one for the file names and one for the customer site files come from. For a reason I can't figure out, whatever option I select from the second select list is never captured when I hit Submit. However, whatever option I select from the first select list is always captured. What am I missing? I am sure it's starting right at me.... Any hints welcome. Thank you. Here's my code: Code: [Select] <HTML> <head><title>SEARCH TOOL - PROTOTYPE</title></head> <body><h1>SEARCH TOOL - PROTOTYPE</h1> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <fieldset> <legend>Filename (one item)</legend><select name="DBFilename" id="DBFilename" size="0"> <?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());} mysql_select_db("dev", $con) or die(mysql_error()); $result = mysql_query("select distinct filename from search_test"); while ($row = mysql_fetch_array($result)) { ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php } mysql_close($con); ?> </select></fieldset> <fieldset> <legend>Site (one item)</legend><select name="DBSite" id="DBSite"> <?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());} mysql_select_db("dev", $con) or die(mysql_error()); $result = mysql_query("select distinct site from search_test"); while ($row = mysql_fetch_array($result)) { ?> <option value="<?php echo $row['site']; ?>"><?php echo $row['site']; ?></option> <?php } mysql_close($con); ?> </select></fieldset> <input type="submit" name="submit" value="submit" > <input type="button" value="Reset Form" onClick="this.form.reset();return false;" /> </form> </body> </HTML> <?php if (isset($_POST['submit'])) { if (!empty($_POST['DBFilename'])) {doFileSearch();} elseif (!empty($_POST['DBSite'])) {doSite();} } function doFileSearch() { $mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot"; $dir = opendir($mydir); $DBFilename = $_POST['DBFilename']; $con = mysql_connect("localhost", "user", "pass"); if (!$con) {die('Could not connect: ' . mysql_error());} mysql_select_db("dev", $con) or die("Couldn't select the database."); $getfilename = mysql_query("select filename from search_test where filename='" . $DBFilename . "'") or die(mysql_error()); echo "<table><tbody><tr><td>Results.</td></tr>"; while ($row = mysql_fetch_array($getfilename)) { $filename = $row['filename']; echo '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>'; } echo "</table></body>"; } function doSite() { $mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot"; $dir = opendir($mydir); $DBSite = $_POST['DBSite']; $con = mysql_connect("localhost", "user", "pass"); if (!$con) {die('Could not connect: ' . mysql_error());} mysql_select_db("dev", $con) or die("Couldn't select the database."); $getfilename = mysql_query("select distinct filename from search_test where site='" . $DBSite . "'") or die(mysql_error()); echo "<table><tbody><tr><td>Results.</td></tr>"; while ($row = mysql_fetch_array($getfilename)) { $filename = $row['filename']; echo '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>'; } echo "</table></body>"; } ?> I need my select dropdown to show the relevant option. I am using a GET action to pass the id of an article to another page (edit article page) with this i am selecting that specific record and passing it's data into form object in order to be modified and updated. The only issue is my category list is a dropdown and i need it to display the relivent category for that specific record. hope that makes sence I have a simple database table that has the names of countries listed with just 2 fields ID & country.
I want to use this in a form to choose the country and provide statistics regarding that country.
I know how to get the data from MySQL, but I don't know how I can use this in the form as a selection?
How do I get the options to iterate all of the countries so that when the form is displayed a combo type box is displayed with a dropdown list?
I have not shown any code as I am not sure if this is possible.
Please can anyone advise if this is possible and if so some example code?
This is a continuation of a previous post. I am trying to get the names of countries that are in a database table. It's not working! All I get is a Submit button, but no data. This is what I understand from the code that Psycho suggested: I am unfamiliar with the function and the use of the FOREACH constuct. looking at the PHP manual there are two syntaxes. It appears that Psyhco has used the second form where the current element's key is assigned to $key variable for each itteration. foreach (array_expression as $key => $value The 2 variables he used are $id and $label I assume that the line: $optionsHTML = ''; is the array built from the expression: $optionsHTML .= "{$label}\n"; Because I was not familiar with the PDO method, but have used mysqli, I had to rewrite the data retrival part which I believe is OK. I guess that the line : $countryOptions = buildSelectOptions($countries); is used in conjunction with the function to build the array. The HTML part that Psycho wrote puts the variable into the Form format for a selection list. Why is the $optionsHTML also inclosed in the option tags? Have I got the HTML part correct or is it the data extraction part that is incorrect? Here is the code: <? include("AddStats_admin_connect.php"); //connect to database doDB(); //Function to build select options based on passed array function buildSelectOptions($options) { $optionsHTML = ''; foreach($options as $id => $label) { $optionsHTML .= "<option value='{$id}'>{$label}</option>\n"; } return $optionsHTML; } //Run query to get the ID and Name from the table //Then populate into an array $clist_sql = "SELECT CID, Country FROM Countries"; $clist_res= mysqli_query($mysqli, $clist_sql) or die(mysqli_error($mysqli)); if (mysqli_num_rows($clist_res) < 1) { //this Country not exist $display_block = "<p><em>You have selected an invalid Country.<br/> Please try again.</em></p>"; } $countries = array(); while($Ctry_info = mysqli_fetch_array($clist_res)) { $countries[$Ctry_info['CID'] = $Ctry_info['Country']]; } $countryOptions = buildSelectOptions($countries); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Stats</title> <link rel="stylesheet" href="stylesheets/style.css" /> <!--[if IE]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <? echo $countryOptions; ?> </br></br></br></br></br></br></br></br> <!Later in the HTML for the page <form action="Ctrystats.php" method="post"> <option name="country" value=<? echo $countryOptions;?>Country</option></br></br> <input type="submit" value="Submit Choice"> </form></p> </body> </html>I think I am nearly there so I would appreciate some help to finish this coding. Note: If you make your questions easy to read, then you have better chances of a quality answer. Use [ code ] ] tags. Hi, I have a very large form that has been created with a select statement. When i am submitting the form and running the sql insert, i obviously cant list out all of the fields like you would for a small form. How would one go about submitting a form with a large number of fields. The code below is one of the lines of the table <tr> <td style="width:70%;"> <span value="1">Item</span> </td> <td class="text-center"> <input class="eqQty text-center up" type="text"> </td> </tr> I am sure that the answer is faily simple but googling has not been friendly to me on this one. Kind Regards I had this working, but when I try and get fancy and use AJAX the data doesn't display. I think this is a PHP problem though. My code for the select form including AJAX code Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB"> <head> <title>AJAX Example</title> <link rel="stylesheet" type="text/css" href="Form.css" media="screen" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("tr:odd").addClass("odd"); }); </script> <script type="text/javascript"> function showPlayers(str) { var xmlhttp; if (str=="") { document.getElementById("DataDisplay").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); else {// code for IE6, IE5 } xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("DataDisplay").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","Query.php?category_id="+str,true); xmlhttp.send(); } </script> </head> <body> <h1">AJAX Example</h1> <?php #connect to MySQL $conn = @mysql_connect( "localhost","username","pw") or die( "You did not successfully connect to the DB!" ); #select the specified database $rs = @mysql_SELECT_DB ("MyDB", $conn ) or die ( "Error connecting to the database test!"); ?> <form name="sports" id="sports"> <legend>Select a Sport</legend> <select name="category_id" onChange="showPlayers(this.value)"> <option value="">Select a Sport:</option> <?php $sql = "SELECT category_id, sport FROM sports ". "ORDER BY sport"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"".$row['category_id']."\">".$row['sport']."</option>\n "; } ?> </select> </form> <br /> <div id="DataDisplay"></div> </body> </html> Query.php <?php #get the id $id=$_GET["category_id"]; #connect to MySQL $conn = @mysql_connect( "localhost","username","pw") or die( "Error connecting to MySQL" ); #select the specified database $rs = @mysql_SELECT_DB ("MyDB", $conn ) or die ( "Could not select that particular Database"); #$id="category_id"; #create the query $sql ="SELECT * FROM sports INNER JOIN players ON sports.category_id = players.category_id WHERE players.category_id = '".$id."'"; echo $sql; #execute the query $rs = mysql_query($sql,$conn); #start the table code echo "<table><tr><th>Category ID</th><th>Sport</th><th>First Name</th><th>Last Name</th></tr>"; #write the data while( $row = mysql_fetch_array( $rs) ) { echo ("<tr><td>"); echo ($row["category_id"] ); echo ("</td>"); echo ("<td>"); echo ($row["sport"]); echo ("</td>"); echo ("<td>"); echo ($row["first_name"]); echo ("</td>"); echo ("<td>"); echo ($row["last_name"]); echo ("</td></tr>"); } echo "</tr></table>"; mysql_close($conn); ?> I think the problem is either with this part in the AJAX Code: [Select] xmlhttp.open("GET","Query.php?category_id="+str,true); or most likely in my Query.php code when I wasn't using AJAX and using POST it worked fine, but adding the AJAX stuff and GET it doesn't work. When I echo out the SQL the result is Code: [Select] SELECT * FROM sports INNER JOIN players ON sports.category_id = players.category_id WHERE players.category_id = '' so the category_id is not being selected properly and that is the primary key/foreign key in the MySQL table which connects the JOIN. I have a div that displays the contents of a mysql table. When the users adds to the table/presses a button I want it to fade out, reload, and fade back in with the new content there. The current PHP code is in its own div called 'stream'... <script> function buttonFunction() { v=$("#txtstatus"); $.post('../action/poststatus.php',{status:v.val()},function(d){ $('div#stream').fadeOut('slow'); $('div#stream').load; $('div#stream').fadeIn('slow'); }); } </script>This fades out and back in but with nothing new loaded. Please help. Thanks, Hi is there any way we can prevent suppose <textarea></textarea> when page reload it refresh and set default text i wanted to know is there any way to prevent certain things not to get refreshed is there any method in php which prevent to reload this ! <?php echo "<textarea > Enter your favorite quote!</textarea> \n" ; Code: [Select] if (isset($_GET['edit']) && $_GET['edit'] == 'textupdate'){ } <a href= \"{$_SERVER['PHP_SELF']}?page=1&edit=textupdate \" >Click</a>?> |