Advertisement

05.27.2008 at 08:57AM PDT, ID: 23435280
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Populating Text Fields After Submitting Form

Asked by macrolific in PHP and Databases, PHP Scripting Language, MySQL Server

Tags:

The php script below displays the first 4 fields of a MySQL database table in an html table.  It also provides the following within an html form:

1) 4 checkboxes, for choosing which columns (DB fields) to display
2) 1 drop-down menu, for sorting by column (DB field)
3) 2 text input fields (start/end dates), for limiting the number of rows by specifying a range of dates (uses strtotime, which parses various date formats)

There's only 1 problem remaining.  After choosing the display criteria with the checkboxes, drop-down menu and text fields, and then hitting the submit button, the table comes back as specified, as do the checkboxes and drop-down menu, but the text fields are now blank.

How can my script be modified so that after choosing the display criteria and submitting the form, the text fields come back with the selected dates filled in?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
<?php
 
$DB_SERVER=""; // specify IP or domain of your db server
$DB_USERNAME="";
$DB_PASSWORD="";
$DB_NAME=""; // specify name of your database
 
//set the default state for the checkboxes
$checkedFields = array();
$checkedFields['invoice_num'] = " checked='checked' ";
$checkedFields['firstname'] = " checked='checked' ";
$checkedFields['lastname'] = " checked='checked' ";
$checkedFields['transaction_date'] = " checked='checked' ";
 
if( isset($_REQUEST['fieldTracker']) )
{
	//determine the "unchecked" ones 
	if( empty($_REQUEST['invoice_num']) )
		$checkedFields['invoice_num']="";
	if( empty($_REQUEST['firstname']) )
		$checkedFields['firstname']="";
	if( empty($_REQUEST['lastname']) )
		$checkedFields['lastname']="";
	if( empty($_REQUEST['transaction_date']) )
		$checkedFields['transaction_date']="";
}
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
 "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div align="center">
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="select" id="select">
    <input type="checkbox" name="invoice_num" value="1" <?=$checkedFields['invoice_num']?> />Invoice
    <input type="checkbox" name="firstname" value="1" <?=$checkedFields['firstname']?> />First Name
    <input type="checkbox" name="lastname" value="1" <?=$checkedFields['lastname']?> />Last Name
    <input type="checkbox" name="transaction_date" value="1" <?=$checkedFields['transaction_date']?> />Date <br>
    Sort By
    <select name="Orderby" id="Orderby">
      <option value="invoice_num"<?php if(isset($_REQUEST['Orderby']) && $_REQUEST['Orderby'] == 'invoice_num') : ?> selected="selected"<?php endif; ?>>Invoice Number</option>
      <option value="firstname"<?php if(isset($_REQUEST['Orderby']) && $_REQUEST['Orderby'] == 'firstname') : ?> selected="selected"<?php endif; ?>>First Name</option>
      <option value="lastname"<?php if(isset($_REQUEST['Orderby']) && $_REQUEST['Orderby'] == 'lastname') : ?> selected="selected"<?php endif; ?>>Last Name</option>
      <option value="transaction_date"<?php if(isset($_REQUEST['Orderby']) && $_REQUEST['Orderby'] == 'transaction_date') : ?> selected="selected"<?php endif; ?>>Date</option>
    </select>
    <br>
From: <input name="date_start">
To: <input name="date_end"><br>
    <input name="fieldTracker" type="submit" value="submit" />
  </form>
</div>
<?php
 
if ( !($link = mysql_connect($DB_SERVER, $DB_USERNAME, $DB_PASSWORD)) ) {
    echo 'Could not connect to mysql: ' . mysql_error();
    exit;
}
if (!mysql_select_db($DB_NAME, $link)) {
    echo 'Could not select database: ' . mysql_error();
    exit;
}
$sql = 'SELECT * FROM tbl_authnet_data';
      $sql = $sql . " WHERE transaction_date < '" . date('Y-m-d H:i:s' , strtotime($_POST['date_end'])) . "' AND transaction_date > '" . date('Y-m-d H:i:s' , strtotime($_POST['date_start'])) . "'";
 
if (isset($_REQUEST['Orderby']))
{
      $sql = $sql . " Order By ". $_REQUEST['Orderby'];
}
$result = mysql_query($sql, $link) or die("Query Error: " . mysql_error());
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
$colspan=0;
print "<table align='center' border='0' cellpadding='2' bgcolor='#CCCCCC'>";
print "<tr style='color: white;'>";
if( !empty($checkedFields['invoice_num']) )
{
	++$colspan;
	print "<th>Invoice</th>";
}
if( !empty($checkedFields['firstname']) )
{
	++$colspan;
	print "<th>1st Name</th>";
}
if( !empty($checkedFields['lastname']) )
{
	++$colspan;
	print "<th>Last Name</th>";
}
if( !empty($checkedFields['transaction_date']) )
{
	++$colspan;
	print "<th>Date</th>";
}
 
print "</tr>";
print "<tr><td colspan='{$colspan}'></td></tr>";
 
// Define colors for alternating rows
$color1 = "#fffaf0";
$color2 = "#ffffff";
$row_count = 0;
 
while ($row = mysql_fetch_assoc($result))
{
	$row_color = ($row_count % 2) ? $color1 : $color2;
		print "<tr>";
		foreach ($row as $field => $value)
		{
		if( isset($checkedFields[$field]) && !empty($checkedFields[$field]) )
           	print "<td nowrap='nowrap' align='center' bgcolor='{$row_color}'><font size='2'>{$value}</font></td>";
		}
		print "</tr>";
		$row_count++;
}
print "</table>";
 
?>
</body>
</html>
 
 
[+][-]05.27.2008 at 09:18AM PDT, ID: 21653183

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 09:20AM PDT, ID: 21653201

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 09:45AM PDT, ID: 21653422

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 02:00PM PDT, ID: 21655647

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language, MySQL Server
Tags: PHP, MySQL, HTML
Sign Up Now!
Solution Provided By: paulop1975
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.27.2008 at 03:03PM PDT, ID: 21656143

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 03:08PM PDT, ID: 21656192

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080924-EE-VQP-39 / EE_QW_2_20070628