tlavelle Posted August 9, 2007 Share Posted August 9, 2007 I have this page: <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $year_query = "SELECT distinct year FROM userdata"; $industry_query = "SELECT distinct comp_desc FROM comp_desc"; // execute query $year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error()); $industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($year_result) > 0 or mysql_num_rows($industry_result)){ // yes // print them one after another echo"<form method=\"post\" action=\"uservals.php\" name=\"wercbench1\">"; echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">"; echo"<tbody>"; echo" <tr>"; echo" <td>Select the comparison year and group then click next"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"year\">"; while($row = mysql_fetch_row($year_result)) { echo" <option>".$row[0]."</option>"; } echo" </select>"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"industry\">"; while($row = mysql_fetch_row($industry_result)) { echo" <option>".$row[0]."</option>"; } echo" </select>"; echo"</td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>"; echo" <td></td>"; echo" </tr>"; echo"</tbody>"; echo"</table>"; echo"</form>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($year_result); mysql_free_result($industry_result); // close connection mysql_close($connection); ?> </body> </html> And uservals.php shows nothing: <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; $useryear=$_Post['year']; $usergroup=$_Post['industry']; echo "$useryear"; echo $_Post['year']; ?> </body> </html> How do I pass the selected values of the dropdown to the post? Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/ Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 The option needs to have a value: echo"<option value=\"".$row[0]."\">".$row[0]."</option>"; After that, the value will be in $_POST['year']. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-319683 Share on other sites More sharing options...
tlavelle Posted August 9, 2007 Author Share Posted August 9, 2007 Thats not working for me Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-319693 Share on other sites More sharing options...
tlavelle Posted August 9, 2007 Author Share Posted August 9, 2007 if i change it to a get the query string looks like this but I am still not getting the values http://localhost:8080/wercbench/uservals.php?year=2005&industry=Operations+Type%3A++Broken+Case&wercbench1_next=Next Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-319702 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 You should be getting an Undefined variable _Post message. I am pretty sure you need to capitalize the entire word: $_POST, or $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-319713 Share on other sites More sharing options...
Fadion Posted August 9, 2007 Share Posted August 9, 2007 As previosly said u need to capitalize post: $useryear=$_Post['year']; $usergroup=$_Post['industry']; to $useryear=$_POST['year']; $usergroup=$_POST['industry']; As long as ure using not multiple selects, ure fine with your code. Post will return the selected value. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-319728 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 Thanks, that was it...the case. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320292 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 Conceptually, I dont really get how the array population works. while($row = mysql_fetch_row($year_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; does mysql_fetch_row increment the array index until it runs out of rows? how does the array get populated with this. Can someone narrate the logic? Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320295 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 Directly from php.net: Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows. mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0. Every time the while loop loops mysql_fetch_rows gets a new row from the result returned from the sql query. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320308 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 This code: if (mysql_num_rows($metric_result) > 0) { // yes // print them one after another echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td>".$row[3]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } Yields this table: Metric Description Average Best Practice Enter your value here On time receipts 0.92 0.98 On time shipments 0.98 0.99 Fill rate-line 0.96 0.99 Fill rate-order 0.96 0.99 % of orders shipped complete 0.96 0.99 % of overtime hours 0.1 0.04 Days of raw materials on hand 20 10 How do I turn this table into a form where the last field in each row is a text box that users can enter data into? Is it possible to leverage the while loop in some way to dynamically generate a form? Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320327 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 EDIT: didn't read your post properly. Hold on... change this: echo "<td>".$row[3]."</td>"; to this: echo '<td><input type="text" name="myvalue[]" value="' . $row[3] . '" /></td>'; That will now change the forth column into a editable column. In order to submit the data you'll need to warp the table in a form and add a submit button at the end. To do this you'll want to add the following line: Echo '<form action="path/to/script/to/process/form/data.php" method="post">'; Before this line echo "<table cellpadding=10 border=1>"; And change this line: echo "</table>"; to: echo ' <tr> <td colspan="3"> </td> <td align="center"><input type="submit" name="submit" value="Apply"></td> </tr> </table>'; In the script that processes the form data you'll want to use $_POST['myvalue'] to get the data from the form. NOTE: $_POST['myvalue'] holds an array of all the form field, so $_POST['myvalue'][0] gets the first field, $_POST['myvalue'][1] gets second, $_POST['myvalue'][9] gets the 10th etc, Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320328 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 I did this: if (mysql_num_rows($metric_result) > 0) { // yes // print them one after another Echo '<form action="path/to/script/to/process/form/data.php" method="post">'; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . " /></td>"; echo "</tr>"; } echo "<tr><td colspan=/"3/"> </td><td align=/"center/"><input type=/"submit/" name=/"submit/" value=/"Apply/"></td></tr></table>"; } else { // no // print status message echo "No rows found!"; } and am getting this error Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in C:\xampp\htdocs\wercbench\uservals.php on line 59 Another newbie follow up question. Do you need an echo statement per line of html or can you basically say echo "Entire HTLM Source code"?? Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320343 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 also, does using single quotes eliminate the need to escape quotes within a function? Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320344 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 You used forward slashes instead of backslashes on line 22 for escaping quotes. Yes using single quotes as the string delimiter allows to use double quotes (without having to escape them) within strings, however if you use a single quote within the string you'll have to escape it. Also variables wont work within single quotes. You'll have to concatenate the string to use variables within the string. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320349 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 Thanks for all your help. I still am not there. This code produced one row if (mysql_num_rows($metric_result) > 0) { // yes // print them one after another echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . " /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Apply\"></td></tr></table>"; } else { // no // print status message echo "No rows found!"; } It produced a row with the textbox containing this: /></td></tr><tr><td>On time shipments</td><td>0.98</td><td>0.99</td><td><input type= Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320356 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 This seems to produce the desired result. Is this valid? if (mysql_num_rows($metric_result) > 0) { // yes // print them one after another echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; //echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . " /></td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Apply\"></td></tr></table>"; } else { // no // print status message echo "No rows found!"; } Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320362 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 No Change this //echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . " /></td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; to: echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . "\" /></td>"; NOTE: Make sure you change value of the action attribute in the form tag on this line: echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">"; To the script that processes the submited form data. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320365 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 what is the purpose of the . $row[3] . this: echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . "\" /></td>"; and this: echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; Generate the same form Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320368 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 I dunno.. I dont know what row3 is. I don't have the script infront of me. I only use used it because it was already in your code for where to place the input field. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320372 Share on other sites More sharing options...
tlavelle Posted August 10, 2007 Author Share Posted August 10, 2007 More silly questions. What is . $row[3] . It is an array variable for what? Would it represent the 4th field in the current row of the mysql_fetch statement? This is more a theoretical question than an actual question. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320391 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 Yes it'll be the 4th row. Arrays start at 0 and not 1 also the dots before and after the variable are called concatenation operators they allow you to tie strings and variables together. Quote Link to comment https://forums.phpfreaks.com/topic/64146-solved-complete-newbie-qeustion/#findComment-320408 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.