?
Solved

PHP Variable to prefill text box

Posted on 2008-10-12
4
Medium Priority
?
2,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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