PHP - Need Help With Dynamic Select Form
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 Similar TutorialsI'm having trouble getting the dynamic data from my <select> menus to write into my MYSQL database. Can anyone see what I'm doing wrong here? First post btw The output looks like this, which is obviously wrong: Code: [Select] <html> <head> </head> <link rel="stylesheet" type="text/css" href="./css/newuser.css" /> <body> <?php session_start(); require 'default.inc.php'; ?> <?php if (isset($_POST['amount'])): $host = 'localhost'; $user = 'user'; $pass = 'password'; $conn = mysql_connect($host, $user, $pass); if (!$conn) { exit('<p>Unable to connect to the database server</p>'); } if (!@mysql_select_db('spikesusers')) { exit('<p>Unable to locate the database</p>'); } $locationname = $_POST['donor']; $donorid = mysql_query("SELECT id FROM donors WHERE locationname='$locationname'"); $amount = $_POST['amount']; $year = $_POST['year']; $type = $_POST['type']; $typeid = mysql_query("SELECT id FROM donationtype WHERE type='$type'"); $player = $_POST['player']; //$playerid = mysql_query("SELECT id FROM players WHERE $sql = "INSERT INTO donations SET donorid='$donorid', amount='$amount', yearofdonation='$year', typeid='$typeid'"; mysql_query($sql); ?> <div class='standard'> <h1>Donation Management</h1> <?php if ($sql) { echo "New donation added "; echo "<p></p>"; echo "<a href=managedonations.php>Back to donation management</a>"; exit(); } else { echo "Error adding new donation"; echo "<a href=adddonation.php>Try again</a>"; exit(); } ?></div> <?php else: $host = 'localhost'; $user = 'user'; $pass = 'pass'; $conn = mysql_connect($host, $user, $pass); if (!$conn) { exit('<p>Unable to connect to the database server</p>'); } if (!@mysql_select_db('spikesusers')) { exit('<p>Unable to locate the database</p>'); } $donor=@mysql_query('SELECT id, locationname FROM donors'); ?> <div class='standard'> <h1>Donation Management</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Donor: <select class="text" name="donor"> <option value=new>Add a new donor...</option> <?php while ($donors=mysql_fetch_array($donor)) { $donorname=$donors['locationname']; echo "<option value='<?php echo $donorname;?>'>$donorname</option>"; //echo "<option value='hi'>hi</option>"; } ?> </option> </select> </label><br /> <label>Amount: <input class="text" type="text" name="amount" class="text" /></label><br /> <label>Year of donation: <select class="text" name="year"> <option value='2011'>2011</option> <option value='2010'>2010</option> <option value='2009'>2009</option> </select> </label><br /> <?php $player=@mysql_query('SELECT id, firstname, lastname FROM players'); ?> <label>Player: <select class="text" name="player"> <option value="player" selected="selected"></option> <?php while ($players=mysql_fetch_array($player)) { $playerfirstname=$players['firstname']; $playerlastname=$players['lastname']; echo "<option value=player>$playerfirstname $playerlastname</option>"; } ?> </select> </label><br /> <?php $type=@mysql_query('SELECT id, type FROM donationtype'); ?> <label>Donation type: <select class="text" name="type"> <?php while ($types=mysql_fetch_array($type)) { $donationtype=$types['type']; echo "<option value=type>$donationtype</option>"; } ?> </select> </label><br /> <input type="submit" value="SUBMIT" class="buttons"/> <input type="button" name="Cancel" value="CANCEL" onclick="window.location = 'managedonations.php'" class="buttons"/> </form> </div> <?php endif; ?> </body> </html> I want to create 2 select menus - one static (Menu 1) and the other (Menu 2) populated according to the selection made in Menu 1. Data will will be pulled from a CSV file. Menu 2 will not have any values in it until an option from Menu 1 has been selected. Then, the options available will be limited based on the Item 1 selection. PHP must be used to read the CSV file and to capture the data (using arrays) I met this problem before and my approach then was to call a function using the onChange event for Menu1 ... <script type = "text/javascript">function update_select(obj){ window.location.href="./call_stats_main.php?choice=" + obj.value;} </script> onchange="update_select(this) This would reload the page passing a parameter to the URL (?choice=) I could then use $_GET['choice'] to capture the selected value and dynamically populate Menu 2 according to this value. It is the onchange event that triggers the population of menu 2. I would like to know IF THERE IS A BETTER WAY, other than using ajax ? Steven M Brand new to the forum, and have only been working with HTML, PHP, & MySQL for about 3 months. I've run into an issue I can't seem to figure out how to work around. I want to have two select boxes, that are dependent on one another. For Example: First box lists types of games: RPG LARP Board Game Miniatures CCG The second box displays game names of the type selected above. I have a MySQL table setup for the second box. To be honest I kinda already have it working... with one small but MAJOR glitch. I select the first box and the page submits using onchange='this.form.submit()'. When the page reloads the first select box reverts back to it's default setting, but the second box does correctly filter & show the content based on the selection that was made in the first box. My Question: How do I set this up so that the first box displays the user selected setting while also passing the $_POST information to the second box? My Code: <table> <tr> <td width=50%> Game Type: </td> <td width=50%> <form name='game_type' action='event_submit.php' method='post'> <select name='game_type' align=center onchange='this.form.submit()' style="width:150px;margin:5px 0 5px 0;"> <option value=''>Chose a Game Type</option> <option value='RPG'>RPG</option> <option value='LARP'>LARP</option> <option value='Board Game'>Board Game</option> <option value='CCG'>Collectable Cards & Games</option> <option value='Miniatures'>Miniatures</option> </select> </form> </td> </tr> <tr> <td width=50%> Game System: </td> <td width=50%> <?php $game_type = $_POST['game_type']; echo "$game_type"; if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("DrowCon", $con); $result = mysql_query("SELECT * FROM Games WHERE Type='$game_type' ORDER BY System ASC"); echo "<form name='game_system' action='event_submit_form.php' method='post'>"; echo "<select name='game_system' align=center style='width:150px;margin:5px 0 5px 0;'>"; echo "<option value=''>Chose a Game System</option>"; while($row_game = mysql_fetch_array($result)) { echo "<option value=''>".$row_game['System']."</option>"; } echo "</form>"; mysql_close($con); ?> </td> </tr> </table> I have a table in a database that I need to filter upon using dependent select boxes. The database was poorly designed and would be difficult to break into separate tables without affecting code written in other places. I have found some good code and understanding of how to implement the dependent select boxes if you have two tables with one column functioning as a lookup value between the two. I have provided a short bit of sql of what the table looks like with some column names changed. I would need to first sort by lastname and have firstname dependent on the selection of lastname. Any help would be greatly appreciated. Code: [Select] create table `user` ( `id` int(3) Not Null Auto_increment, `firstname` varchar (60), `lastname` varchar (24), `age` double , `hometown` varchar (75), `job` varchar (75), `birthdate` date ); insert into `user` (`id`, `firstname`, `lastname`, `age`, `hometown`, `job`, `birthdate`) values ('1','Peter','Griffin','41','Quahog','Brewery','1960-01-01'), ('2','Lois','Griffin','40','Newport','Piano Teacher','1961-08-11'), ('3','Joseph','Swanson','39','Quahog','Police Officer','1962-07-23'), ('4','Glenn','Quagmire','41','Quahog','Pilot','1960-02-28'), ('5','Megan','Griffin','16','Quahog','Student','1984-04-24'), ('6','Stewie','Griffin','2','Quahog','Dictator','2008-03-03'); Dear Support, Here is my code: <?php if ( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { $name = isset($_POST['name]) ? htmlspecialchars($_POST['name']) : ""; $area = isset($_POST['area']) ? htmlspecialchars($_POST['area']) : ""; $choice = isset($_POST['choice ']) ? htmlspecialchars($_POST['choice ']) : ""; //Do Others } ?> <form class="customersForm" method="post"> <div class=""> <p>Name<br /> <select style="width:120px;" name="name"> <?php $sql = "select * from `name_info` order by `name`"; $query = mysql_query($sql); $dd = '<option>Name</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->name.'</option>'; } echo $dd; ?> </select> </p> </div> <div class=""> <p>Area<br /> <select style="width:120px;" area="area"> <?php $sql = "select * from `area_info` order by `area`"; $query = mysql_query($sql); $dd = '<option>Area</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->area.'</option>'; } echo $dd; ?> </select> </p> </div> <div class=""> <p>Choice<br /> <select style="width:120px;" choice="choice"> <?php $sql = "select * from `choice_info` where `name`='".$name."' and `area`='".$area."' order by `choice`"; $query = mysql_query($sql); $dd = '<option>Choice</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->choice.'</option>'; } echo $dd; ?> </select> </p> </div> ......//Others input fields ...some text fields ... </ form> I have the above code, where I would like to show the choice Drop-down box, depends on $name and $area match of the choice_info table. $name field and $area field needs to have any dependency and straight from the MySQL table. But, choice needs to depend on $name & $area. How can I do it without much using Javacript? I do not mind to use JavaScript, but prefer to minimal. By the way, I will have some other input fields too. It's an input form but, one drop-down box will be depends on two others choice. Thanks for your help. Hi, I had a form which contains, 8 select boxes. In those 8 select boxes, 3 of the boxes are interlinked, like, I am able to generate data in other 2 select boxes, if one box is been selected. i.e. based upon selection of one select box, and i am able to generate data in other select box. So, 3 select boxes are interlinked. And remaining 5 selects are optional, if the user selects, then we need to generate data based upon selection of the user. Here the main issue is how to grab data in the post back form, like, how to capture the number of selects boxes selected and what are those selected, and based upon that we need to generate data. Here one more important thing is that, in the report generation form(postback form), I need to show the data in a table. So , in table I had 9 columns and these would be common to whatever the selection made, but, only the requeired data would be changed based upon the selection of the remaining 5 optional boxes. Hope you got me!!!! 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 all, it's been a while since I wrote in PHP but a University project has forced me to touch up on my knowledge of it. I'm trying to re-create an old form making system I made a few years ago from memory. But it's not working. I know it's something very obvious but I just need another eye to take a look for me. I get the following error message: Warning: Invalid argument supplied for foreach() in /home/birdneto/public_html/rebelfrogv2/p_createaccount.php on line 169 So I know it's do with my show_form(); function. Can anyone help? Code: [Select] <div id="page-body-special"> <?php // List page details $page_header = "Create your account"; $page_desc = "Enter your details below to create an account. Creating an account comes with great benefits! You can create a wishlist, sign up for our mailing list and purchase items. Make sure you enter all your information correctly."; // List form details $form_name = "createaccount"; // List all the field names here $field_email_address = "email_address"; $field_email_address_confirm = "confirm_email_address"; $field_password = "password"; $field_password_confirm = "confirm_password"; $field_house_number = "house_number"; $field_street_name = "street_name"; $field_city = "city"; $field_county = "county"; $field_postcode = "postcode"; $field_phone_number = "phone_number"; $field_newsletter = "newsletter"; $field_submit = "submit"; // Define all the field meta data (field name, maxlength, minlength, field type, [field options], [field description]) $fields = array( array($field_email_address, 80, 8, "text", $_POST[$field_email_address]), array($field_email_address_confirm, 80, 8, "text", $_POST[$field_email_address_confirm]), array($field_password, 16, 8, "password", $_POST[$field_password]), array($field_password_confirm, 16, 8, "password", $_POST[$field_password_confirm]), array($field_house_number, 2, 4, "password", $_POST[$field_house_number]), array($field_street_name, 16, 8, "password", $_POST[$field_street_name]), array($field_city, 16, 8, "password", $_POST[$field_city]), array($field_county, 16, 8, "password", $_POST[$field_county]), array($field_postcode, 16, 8, "password", $_POST[$field_postcode]), array($field_phone_number, 16, 8, "password", $_POST[$field_phone_number]), array($field_newsletter, 0, 0, "checkbox", $_POST[$field_newsletter], 0, "Would you like to sign up to our newsletter?"), array($field_submit, 0, 0, "submit", "Create Account") ); // Check if form has been submitted or not. All fields must be declared here with an if statement if($_POST['submit']) { // Set no_errors and show_form to inital 0 $no_errors = 1; function show_errors($field) { if($field == $field_email_address) { if(strlen($_POST[$field_email_address]) < $fields[0][2] and strlen($_POST[$field_email_address]) > $fields[0][1]) { $no_errors = 0; $error = "Your email address must be between " . $fields[0][2] . " and " . $fields[0][1] . " characters"; } } if($field == $field_email_address_confirm) { if($_POST[$field_email_address_confirm] != $_POST[$field_email_address]) { $no_errors = 0; $error = "Your emails do not match each other"; } } if($field == $field_password) { if(strlen($_POST[$field_password]) < $fields[2][2] and strlen($_POST[$field_password]) > $fields[2][1]) { $no_errors = 0; $error = "Your password must be between " . $fields[2][2] . " and " . $fields[2][1] . " characters"; } } if($field == $field_password_confirm) { if($_POST[$field_password_confirm] != $_POST[$field_password_confirm]) { $no_errors = 0; $error = "Your passwords do not match"; } } if($field == $field_house_number) { if(strlen($_POST[$field_house_number]) < $fields[4][2] and strlen($_POST[$field_house_number]) > $fields[4][1]) { $no_errors = 0; $error = "Your house number must be between " . $fields[4][2] . " and " . $fields[4][1] . " numbers"; } } if($field == $field_street_name) { if(strlen($_POST[$field_street_name]) < $fields[5][2] and strlen($_POST[$field_street_name]) > $fields[5][1]) { $no_errors = 0; $error = "Your street name must be between " . $fields[5][2] . " and " . $fields[5][1] . " characters"; } } if($field == $field_city) { if(strlen($_POST[$field_city]) < $fields[6][2] and strlen($_POST[$field_city]) > $fields[6][1]) { $no_errors = 0; $error = "Your city must be between " . $fields[6][2] . " and " . $fields[6][1] . " characters"; } } if($field == $field_county) { if(strlen($_POST[$field_county]) < $fields[7][2] and strlen($_POST[$field_county]) > $fields[7][1]) { $no_errors = 0; $error = "Your county must be between " . $fields[7][2] . " and " . $fields[7][1] . " characters"; } } if($field == $field_postcode) { if(strlen($_POST[$field_postcode]) < $fields[8][2] and strlen($_POST[$field_postcode]) > $fields[8][1]) { $no_errors = 0; $error = "Your postcode must be between " . $fields[8][2] . " and " . $fields[8][1] . " characters"; } } if($field == $field_phone_number) { if(strlen($_POST[$field_phone_number]) < $fields[7][2]) { $no_errors = 0; $error = "Your phone number must be under " . $fields[7][2] . " numbers"; } } return $error; } if($no_errors == 0) { show_form(); } else { echo "<p>Form posted</p>"; echo $br . $br; } } else { show_form(); } function show_form() { // Show the page header echo $br . '<form method="post" action="./?p=' . $page . '" name="' . $form_name . '">'; echo $br . $br; echo '<table id="form-' . $form_name . '">'; echo $br; // List all fields in form format foreach($fields as $meta) { // Explode the array and implode it, changes commas to spaces, then makes all words have capital letters $label_exp = explode("_", $meta[0]); $label_imp = ucwords(implode(" ", $label_exp)); // Is the field type a submission? if($meta[3] == "submit") { echo ' <tr><td colspan="2"><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" value="' . $meta[4] . '" /></td></tr>'; echo $br; } // Is the field type a select? elseif($meta[3] == "select") { $options = explode(",", $meta[5]); echo ' <tr><td><label>' . $label_imp . '</label></td><td><select type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '">'; echo $br; foreach($options as $option) { function is_selected() { if($option == $_POST[$meta[0]]) { echo 'selected="selected"'; } } echo(' <option ' . is_selected($option) . '>' . $option . '</option>'); echo $br; } echo ' </select></td></tr>'; echo $br; } // Is the field type a checkbox? elseif($meta[3] == "checkbox") { echo ' <tr><td><label>' . $meta[6] . '</label></td><td><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" /></td></tr>'; echo $br; } // Is the field type anything else? else { echo ' <tr><td><label>' . $label_imp . '</label></td><td><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" maxlength="' . $meta[1] . '" value="' . $meta[4] . '" /></td></tr>'; echo $br; } } // Show the page footer echo '</table>'; echo $br . $br; echo '</form>'; echo $br . $br; } echo $br . "<h1>" . $page_header . "</h1>"; echo $br . $br; echo "<p>" . $page_desc . "</p>"; echo $br . $br; echo '<div class="clear"></div>'; echo $br . $br; ?> </div> Okay, so I have a page with a heap of fields and buttons on it (and want to keep it like that really) inside one form. Layout Box 1 Button 1 Box 2 Button 2 Box 3 Button 3 Box 4 Button 4 etc... Box 1 is called 'amount1$id' Box 2 is called 'amount.$id' where $id is the $id of the data being displayed etc So a sample form may look like Box: "amount22" Button: "Pay22" Box: "amount25" Button: "Pay25" Box: "amount32" Button: "Pay32" Box: "amount39" Button: "Pay39" Box: "amount420" Button: "Pay420" Its working fine up to that point. My problem now is that I can't work out how to get that id number from the form once submitted. If I could get the button name somehow I could do it, although I don't know the button or box names when writing the processing script. I could use a loop and check if button1 to buttonX was posted etc, although if there is a large number of entries in the database, then that starts to become a problem. Thoughts? Hello,
I have made a simple form, which displays an input first, you have to enter a number, then a dynamic form show up with a dynamic part where the number and name of fields vary depending on the number entered first.
In the action page, i have a problem, when i try to display the data in each field, i don't succeed, i get a message
Notice: Array to string conversion
My code is the following :
<h1>Training Reports</h1> <hr /> <form action="" method="post"> Type the number of SEALs : <input type="number" name="nofseals"/> <input type="submit" name="submitn" value="Submit" /> </form> <?php if(isset($_POST['submitn'])){ $numberofseals=$_POST['nofseals']; $numberoffields = 0; echo "<form name=\"report\" action=\"reporta.php\" method=\"post\" style=\"text-align:left;\"> <div style=\"margin-left: 50%;\">Training # <input type=\"number\" name=\"number\"/></div> <br><br> <input type=\"number\" name=\"nos\" value=\"$numberofseals\" style=\"display: none;\"/> <input type=\"number\" name=\"nof\" value=\"$numberoffields\" style=\"display: none;\"/> <h3>Date</h3> <input type=\"date\" name=\"date\" placeholder=\"Date\"/> <h3>Training Type</h3> <textarea name=\"type\" rows=\"2\" placeholder=\"Training Type...\" ></textarea> <h3>Training Duration</h3> <input type=\"time\" name=\"duration\" placeholder=\"Training Duration\"/> <h3>Training Details</h3> <textarea name=\"details\" rows=\"8\" placeholder=\"Training Details...\" ></textarea> <h3>Screenshots</h3> <textarea name=\"ss\" rows=\"1\" placeholder=\"Screenshots links...\" ></textarea> <h2>Participants Marks and Comments</h2>"; while ($numberoffields < $numberofseals) { $numberoffields++; echo "<h3>SEAL $numberoffields : <input type=\"text\" name=\"n[$numberoffields]\" placeholder=\"SEAL Name\"/></h3> <div style=\"margin-left: 20px;\"> Behavior :<input type=\"text\" name=\"b[$numberoffields]\" placeholder=\"Behavior Mark\"/><br> Skills: <input type=\"text\" name=\"m[$numberoffields]\" placeholder=\"Skills Mark\"/><br> Comments: <textarea name=\"c[$numberoffields]\" rows=\"1\" placeholder=\"Comments\"></textarea></div> <br><br>";} echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";} ?>Action page : <?php if(isset($_POST['submit'])){ $noofseals=$_POST['nos']; $number=$_POST['number']; $date=$_POST['date']; $type=$_POST['type']; $details=$_POST['details']; $duration=$_POST['duration']; $ss=$_POST['ss']; $numero=0; $c{$numero}=$_POST['c']; $m{$numero}=$_POST['m']; $b{$numero}=$_POST['b']; $n{$numero}=$_POST['n']; echo "[center][color=#2D3C0F][size=20pt][glow=black,2,100][font=georgia]TRAINING REPORT #[/font][/glow][/size][/color][/center] [b][color=navy]Date:[/color][/b]$date [b][color=navy]Type of training:[/color][/b]$type [b][color=navy]How long it lasted:[/color][/b]$duration [b][color=navy]Training details:[/color][/b]$details [b]Screenshots:(optional)[/b]$ss [b][color=navy]SEALs perfomance[/color][/b] [list]"; while($numero < $noofseals) { $numero++; echo "[li][b][color=blue]Seal [$nn]:[/color][/b] [b]Behavior:[/b] [b]Skills:[/b] [b]Comments:[/b] [/li]"; }} echo"[/list]"; ?>
Hi All, $userClient = "VARIABLE OF WHO IS LOGGED IN"; <form action="actions/assign.php" method="post"> <?php $sql = "SELECT * FROM Engineers where Engineer_Company = '".addslashes($userClient)."' and userType = '5'"; $result = mysqli_query($db, $sql); while($row = mysqli_fetch_assoc($result)) { $userSID = $row["userID"]; echo '<tr> <td>'.$row["userName"].' <input type="hidden" class="form-control" id="usersID[]" name="usersID'.$row["userID"].'" value="'.$row["userID"].'"> <input type="hidden" class="form-control" id="date" name="date" value="'.date('d-m-Y').'"> </td> <td>'; $sql1 = "SELECT * FROM job_orders where companyID = '".addslashes($userClient)."'"; $result1 = mysqli_query($db, $sql1); echo "<select style='width: 250px;' id='job$userSID' name='job$userSID'>"; echo '<option value="">-- Please Select --</option>'; while($row1 = mysqli_fetch_assoc($result1)) { echo '<option value="'.$row1["jobID"].'">'.$row1["jobtitle"].'</option>'; } echo "</select>"; echo '</td> <td>'; $query = $db->query("SELECT * FROM Engineer_Company WHERE Company_ID = '".addslashes($userClient)."'"); $rowCount = $query->num_rows; if($rowCount > 0){ echo ' <select name="company'.$userSID.'" id="company'.$userSID.'" style="width: 250px;"> <option value="">-- Select Company --</option>'; while($row = $query->fetch_assoc()){ echo '<option value="'.$row['Engineer_Company_ID'].'" class="form-control form-control-sm">'.$row['Engineer_Company_Name'].'</option>'; } echo '</select>'; } echo'</td> <td> <div class="form-group"> <select id="site'.$userSID.'" name="site'.$userSID.'" style="width: 250px;"> <option value="">-- Select Site --</option> </select> </div> '; echo'</td> </tr>'; echo ' <script type="text/javascript"> $(document).ready(function(){ $("#company'.$userSID.'").on("change",function(){ var diag_id = $(this).val(); if(diag_id){ $.ajax({ type:"POST", url:"ajax-client-side.php", data:"diag_id="+diag_id, success:function(html){ $("#site'.$userSID.'").html(html); } }); } }); }); </script>'; } ?> </tbody> </table> <input class="btn btn-dark float-right" type="submit" value="Allocate"> </form>
Hi, i am creating a web application, and i would like some help/advise on how to tackle a problem. I need to make an image i have into a contact form, with four fields, which basically posts information into a database. I am not sure how i would go about doing this as i will need to make each of the fields dynamic to post the data. I have attached the image to make my point a bit clearer. thanks Hi - I have a form which is populated with values from a DB. This is done by looping through the DB values using a foreach. It works fine. I populate my form with those values. However, I want to be able to amend those values, and then submit the new values back to the DB with 1 single submit button. I don't want a separate submit button for each row of my form. The problem is that because the form is built with a foreach, as the it loops through the variables on each pass of the DB, only the final row of DB are present in the form variables. Question: My 'foreach' approach must be faulty. What is the mechanism or approach I need to use to update the values from the whole form ?? MANY THANKS for all your help !! First off thank you for opening my issue and taking the time out to try and help. I have created a php page that will display a list of people from a mysql database. What i would like to do is have a button that will update a field in the database when pressed. My question is if i make the fields into a form will it only update that one person? So I know a little about PHP but I am no expert by any means. But I have a project that I am working on for a fantasy football league and need some help. My users pick players from a list and then their selections are put into a database. So more than one user is likely to pick the same player. Then I need to score the players based off their games for the week. So I have code that gets the Distinct PlayerID and creates a form to update the player score (see code below), but I have no idea how to process the form. It's a little more complicated then the forms I've used before because the MySQL query would need to UPDATE all the rows for each individual PlayerID. Am I making any sense? Anyway, here is the code. If anyone has suggestions on how to process this form or a better way of doing it then please let me know. <? print '<form id="form1" name="form1" method="post" action="update_player.php">'; // Connecting, selecting database $link = mysql_connect('localhost','user','pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } //Query $query=mysql_query("select DISTINCT(PlayerID), PlayerName, Team From fantasy4.temp ORDER BY Team;") or die ('Could not connect: ' . mysql_error()); print' <center> <table align=center border=0 cellpadding=0 cellspacing=2 width=350> <tr align=center> <td width=50 align=center><b>Player ID </b></td> <td width=50 align=center><b>Team</b></td> <td width=200 align=center><b>Player Name</b></td> <td width=50 align=center><b>Score</b></td> <tr><td colspan="10" bgcolor="black" height="1"></td></tr> '; while($row=mysql_fetch_array($query)){ if($color == 1) { print '<tr bgcolor=#dDdDdD> <td align=center> ' . $row['PlayerID'] . ' </td> <td align=center> ' . $row['Team'] . ' </td> <td align=center> ' . $row['PlayerName'] . ' </td> <td align=center> <input name="' . $row['PlayerID'] . '" type="text" id="' . $row['PlayerID'] . '" size="5" maxlength="5" /> </td> </tr>'; $color=0; } else { print '<tr> <td align=center> ' . $row['PlayerID'] . ' </td> <td align=center> ' . $row['Team'] . ' </td> <td align=center> ' . $row['PlayerName'] . ' </td> <td align=center> <input name="' . $row['PlayerID'] . '" type="text" id="' . $row['PlayerID'] . '" size="5" maxlength="5" /> </td> </tr>'; $color=1; } } print '</table>'; print '<input type="submit" name="button" id="button" value="Update Player Scores" /></form>'; ?> This is what I tried that did not work <? // Connecting, selecting database $link = mysql_connect('localhost','user','pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } //Query $query=mysql_query("select DISTINCT(PlayerID) From fantasy4.temp;") or die ('Could not connect: ' . mysql_error()); while($row=mysql_fetch_array($query)){ $PlayerID = $_POST[$row['PlayerID']]; } while($score = array($_POST['$PlayerID'])){ //Insert Query $query2=mysql_query("UPDATE fantasy4.temp set Score='$score' where PlayerID='$PlayerID'") or die ('Yikes could not connect: ' .mysql_error()); $result = @mysql_query($query2); } //Check whether the query was successful or not if($result) { header("location: register-success.php"); exit(); }else { die("Query failed - " .mysql_error()); } ?> I'm praying there's a function somewhere out there that: generates a html form based on the fields of a particular table within a DB (namely mySQL) generates the appropriate form input based on the fields type or notes (ie. for field: avatar_img, it knows to generate a file input) uses the POST method to submit generates and submits SQL insert query to database In addition to that, a similar function that generates a form that allows the editing of records. It doesn't take too long to write these each time myself, but if there's a function that you provide the formname, database driver, table name, whether your form is to insert or edit, that would be quite nifty i think. ie. generateForm(addUser, mySQL, tUsers, insert) Seen any such thing? Many thanks. Hi all. I am currently trying to build a site that will allow users to manage a self build house project. This is my first php MySQL project but I am getting to grips with it ok. I am currently building this for my own benefit at the moment as I am about to undertake a self build. I have a few questions to put to you guys. This is where I am at the moment I have a database table named materials which contains a list of materials , quantities, unit of measurement, id. My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage. So, heres question 1. I want to take the data and use it to generate a form which will be completed by several merchants.... so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price. i.e <?php mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // results while ($row = mysql_fetch_array($query)) { echo "<br />" .$row ['id']. " " .$row ['material']. " " .$row['quantity']. " " .$row['unit']. "<br />";} echo mysql_error(); ?> I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list. Heres where I get stuck.... as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input. I am not very good at describing my problems but I am hoping that someone can help me out from the information given. Thats the first question. i like to get issues sorted in small bites. the second is the next big bite, I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had a price yet. I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great.. I have the test site local but can upload it it to the server if it will help you see what im trying to do. Any help that anyone can give will be greatly appreciated. Hi - I'm trying to speed up part of my business. I sell photos taken on site...I dump said photos in a directory and my little app pulls all images from a given directory and displays them on a page. I can get the filename etc displayed on screen, added to the image attributes, whatever. I'd like to have a checkbox next to each image, and the user checks a few and it'll send an email to me so I can fill their order. Looks like this: http://www.limitedwave.com/reflex/ I can use a sendmail like I've tried, but it can take like 10 minutes to actually send from the server which is lame. I'd be cool with just dropping the data in my database, then retrieving selections by user's name, but I don't know how to set up a form/insert page that will insert from a somewhat random set of checkboxes. Thoughts? Thanks. Hi, I have a dynamic form which uses javascript to add rows on the fly. The name of the element on my test page is txtRow1, txtRow2, etc; the number is added when I add a new row. I am trying to figure out how I will extract the data once the form had been POSTED. I have tried Code: [Select] $tags = $_POST['txtRow']; foreach ($tags as $t) { echo "$t<br />"; } but that shows nothing. I would be grateful for any assistance. Thanks Hello All, I am building a web based data entry project for my University Recycling Department 1) In the first level the user will enter the number of "rows" that he has to enter it can be any number form 4- 10 . Each row will contain "Vendor Name","Date","Net Recycled Weight" 2) Depending on the number of rows entered by the user I am generating a table structure that has 2.1) A <select> containing vendor names(I am querying for this and then storing the result in an Associative Array and then parsing it for select in every way) 2.2) A text box for the date 2.3) A text box for the Net Recycled Weight ------------------------------------------------ Here is the logic I have implemented so far <html> <head></head> <body> <form name = "form1" method = "post"> <select>HERE THE USER CAN SELECT THE NUMBER OF ROWS HE WANTS TO ENTER</select> <input type = "submit" value = "submit1" name = "submit1"> </form> <body> </html> <?php if(submit1 has been clicked ) { //HERE I AM GENERATING A DYNAMIC FORM BASED ON THE NUMBER OF ROWS COUNT <form name = "form2" method = "post"> echo <table> echo <tr> echo<td>VENDOR</td> echo<td>DATE</td> echo<td>NET RECYCLED WEIGHT</td> echo </tr> for(IT ITERATES TILL I GENERATE THE NUMBER OF ROWS THAT HAS TO BE ENTERED) { I AM GENERATING ROWS HERE } echo <input type = "submit" name = "submit2" value = "submit2"> echo</table> </form> HOW CAN I ACCESS THE POST METHOD OF FORM 2 } ?> MY PROBLEM --------------- I WANT TO KNOW THE FOLLOWING 1) It this the correct way to do it 2) how can I access the data entered in each dynamic row as I have to validate it and then enter the data in MySql 3) I am not sure how I will access the submit button event of the dynamically generated form I hope I can get some help Thanks, Marisha |