Solved

PHP Variable to prefill text box

Posted on 2008-10-12
4
2,398 Views
Last Modified: 2013-12-13
I want to be able to take a textbox and prefill it with the variable from mysql query.

I want to use the variable from this section; and prefill a textbox
$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'");

Textbox;
<input type="text" name="sales" value="<?php echo $ifs; ?>">
      Ship Date:

How do I do this??

Thanks,
Jeff
<?php
// Turn off all error reporting
error_reporting(0);
 
include("include/phpgrid.php");
 
 
 
 
$hostName = "localhost";
$userName = "jmichels";
$password = "hug344";
$dbName	  = "thstew_orders";
?>
<html>
<head>
<title>Invoice</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?php
 
$idresult = mysql_query("SELECT id
FROM oetable_backup WHERE upper($field)='$find'");
 
$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'");
 
 
 
 
 
$dg = new C_DataGrid($hostName, $userName, $password, $dbName);
 
// We preform a bit of filtering
$find = mysql_real_escape_string($_POST['find']);
$field = mysql_real_escape_string($_POST['field']);
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
 
 
$dg -> set_gridpath     ("include/");
$dg -> set_sql          ("SELECT
entry_date,territory,principle,customer,po,part_no,qty,initial_sale,due_to_ship,ship_date,qty_shipped,comm,invoice,comm_due,type_ind,id
FROM oetable_backup WHERE upper($field)='$find'");
 
 
 
$dg -> set_sql_table    ("oetable_backup");
 
// change column titles
//$dg -> set_col_title    ("EmployeeId", "Employee ID");
//$dg -> set_col_title    ("LastName", "Last Name");
//$dg -> set_col_title    ("FirstName", "First Name");
 
// set background and mouse over color
$dg -> set_alt_bgcolor  ("#ffffff, #e9eff2");
$dg -> set_onmouseover  ("lightyellow");
 
// hide a column
$dg -> set_col_hidden   ("Notes");
 
// display URL as hyperlink
$dg -> set_col_link    ("ReportsTo",                         
						"/query.php?EmployeeId=",                         
						"EmployeeId",                         
						"target='_new'"); 
 
// set page size
$dg -> set_page_size(10);
 
// sum up columns
$dg -> set_col_sum_enabled(true);
$dg -> set_col_sum      ("initial_sale");
$dg -> set_col_sum      ("final_sale");
$dg -> set_col_sum      ("qty");
$dg -> set_col_sum      ("comm");
 
$dg -> set_theme("sweet");
 
// make the datagrid editable
$dg -> set_allow_actions(true);
 
$dg -> set_action_type ("V,E");
 
// turn on inline-editing with Ajax enabled
//$dg -> set_inlineedit_enabled(true, true);
 
//$dg -> set_ok_showcredit(true);
$dg -> set_allow_export(true);
$dg -> set_toolbar_enabled(FALSE);
 
$dg -> debug();
 
$dg -> display();
echo "<b>Searched For:</b> " .$ifs;
?>
 
 
<h2 align="center"> </h2>
<h2 align="center"> </h2>
<h2 align="center"><strong><font color="#006600"> </font></strong></h2>
<h2 align="center"><strong><font color="#006600"> </font></strong></h2>
<h2 align="center"><strong><font color="#006600"><img src="http://www.thstewart.com/images/invoice.gif" alt="" title="">TH Stewart Co.</font></strong><strong><font color="#006600"><font size="3"></font></font></strong></h2>
<div align="center">
<form action="new.php" method="post">
<div> </div>
<div align="left">
  <p align="center">Final Sale:
 
 
      <input type="text" name="sales" value="<?php echo $ifs; ?>">
      Ship Date:
        <input type="text" name="sdate"> 
        Qty Shipped:
        <input type="text" name="qtys"> 
        ID:
        <input type="text" name="tid">
        
        </p>
 
  <p align="center"> Invoice#:
		    <input type="text" name="seek">
   
        
 
        <br>
        <br>
</p>
  </div>
<div> </div>
<div> </div>
<div>
<input type="hidden" value="yes" name="searching">
<input type="submit" value="Invoice" name="search"></div></form></div>

Open in new window

0
Comment
Question by:jseaman12
  • 2
4 Comments
 
LVL 1

Expert Comment

by:jodibean28
ID: 22697790
This should work...

$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'"); 
 
if ($row = mysql_fetch_assoc($ifs))
     $finalsale = $row[0];
 
<input type="text" name="sales" value="<?=$finalsale?>"> 

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22698021
Jeff: First of all, turn on ALL error reporting.  You can't write error free code without seeing the errors!

Next, understand that mysql_query() returns a resource or FALSE on error.  You MUST TEST the return from these functions.  In your post above, your resource name is $ifs

Then once you know the query worked, you MUST TEST to see if there is anything in the result set.

If there is anything in the result set, you might find that you need to iterate over the result set (multiple results occurred that satisfied the query).  However since you are looking for the "upper" you may expect one result.  

Best practices, and best of luck going forward! ~Ray

Be careful of the variable names - final_sale is not the same as finalsale.
// TESTING WHETHER THE QUERY WORKED OR FAILED
if (!$ifs = mysql_query("SELECT final_sale FROM oetable_backup WHERE upper($field)='$find'")) { die mysql_error(); }
 
// TESTING WHETHER THE RESULT SET EXISTS
if (!mysql_num_rows($ifs)) { die('NOTHING IN RESULT SET'); }
 
// GETTING THE RELEVANT DATA FROM THE RESULT SET INTO A LOCAL VARIABLE
if (!$row = mysql_fetch_assoc($ifs)) { die('ERROR FETCHING RESULT SET'); }
$finalsale = $row["final_sale"]; // NOTE finalsale !== final_sale
 
// WRITING THE FORM INPUT STATEMENT INTO THE HTML
echo "<input type=\"text\" name=\"sales\" value=\"$finalsale\" /> \n";

Open in new window

0
 

Author Closing Comment

by:jseaman12
ID: 31505449
It worked. I just had to place my php code in the front and end because that part of the page is HTML. But I been trying to figure this out for months. This is the best news. Thanks a lot...

<?php
  echo "<input type=\"text\" name=\"sales\" value=\"$finalsale\" /> \n";
  ?>
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22703146
Great!  Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question