Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assigning value from query result

Status
Not open for further replies.

canajun

Programmer
Sep 28, 2002
57
CA
OK.. I have this following script which draws info from a table, and populates a drop down box.

I need to add a value "txtEgroup" to pass to the file in the action "update_purchase.php?typ=new".

The value for txtEgroup is derived from the table exp_item field exp_group.

Hope this makes sense.. this might just be a mod to the javascript "putPrice".. or not..

Any suggestions?

Code:
<html>
<head>
<title>Enter Purchases</title>
<script language="JavaScript">
function validate(m)
{
  var ename=document.frmExpense.expName.value;
  var dt = new Date(document.frmExpense.txtDate.value);
  if(document.frmExpense.expName.value=="")
  {
    alert("Enter Purchase Name");
    document.frmExpense.ename.focus();
    return false;
  }
  else if(document.frmExpense.amount.value=="")  
  {
    alert("Enter Purchase Amount");
    document.frmExpense.amount.focus();
    return false;
  }
  else if(document.frmExpense.txtDate.value=="")
  {
    alert("Enter Date");
    document.frmExpense.txtDate.focus();
    return false;
  }
  else
    return true;
}
</script>
<!--Numeric Validation-->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\-\_\{\}\`\~\=\|\!\abcdefghijklmnopqrstuvwxyz\ABCDEFGHIJKLMNOPQRSTUVWXYZ]/;
function OnlyNumeric(val) {
var strPass = val.value;
var strLength = strPass.length;
var i;
for(i=0;i<strLength;i++)
{
	
	var lchar = strPass.charAt(i);
	if(lchar.search(mikExp) != -1) 
	{
	val.value='';
	break;
	}
}
}
//  End -->
</script>
<SCRIPT language=javascript src="scripts/calendar.js"></SCRIPT>
<link rel="stylesheet" href="scripts/surylake.css" media="screen"></LINK>
<!--Afdd (/)Date-->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\!\-\abcdefghijklmnopqrstuvwxyz\ABCDEFGHIJKLMNOPQRSTUVWXYZ]/;
function dodacheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
   if (strLength==2){
		val.value = val.value + '/'
   }
   if (strLength==5){
		val.value = val.value + '/'
   }
}
//  End -->
function putPrice(m)
{
 document.frmExpense.amount.value=m.value;
 var i;
 for(i=0;i<m.length;i++)
 {
  if(m.options(i).selected==true)
    document.frmExpense.txtExpname.value=m.options(i).text;
 }
}
</script>
<!--Afdd (/) Date-->
</head>
<body bgcolor="#47A0F9">
<?php
include("conobj.php");

 $query="select exp_name,exp_price,exp_group from exp_item order by exp_name";
 $result=mysql_query($query,$db_connection);
include("header.php");
?>
<center>
                  <TABLE class=main cellSpacing=0 cellPadding=5 width="100%" align=center>
                    <TBODY>

                    <TR>
                      <TD vAlign=top>
                        <CENTER class=main><B>Enter Purchase</B></CENTER><BR>
                       <FORM action=update_purchase.php?typ=new method=post name=frmExpense onsubmit=return validate(this)>
                     
                        <input type=hidden name='r_id' value='<?php print $id; ?>'>
                        <TABLE class=main cellSpacing=1 cellPadding=5
                        align=center bgColor=#000000>
                          <TBODY>
                          <?php
                          if($upd=='Y')
                             print("<tr bgColor=#ffffff><td colspan=4 align=center>Last Purchase Updated</td></tr>");
                          ?>
                          
                          <TR bgColor=#ffffff>
                            <TD class=main align=right><B>Purchase:</B></TD>
                            <TD>
                            <select name=expName onclick="putPrice(this);">
                            <?php
                            print ("<OPTION value=0>Item</OPTION>");
                              while ($row=mysql_fetch_object($result))
                              {                            
                              print"<option value=".$row->exp_price .">".$row->exp_name ."</option>";
                              }
                            ?>
                            </select>
                            <!--INPUT class=search maxLength=32
                            name=ename-->*</TD>
                            <TD class=main align=right><B>Amount:</B></TD>
                            <TD>$ <INPUT class=search maxLength=8 size=8 
                              name=amount onKeyUp="OnlyNumeric(this);">*
                              <input type=hidden name=txtExpname></TD></TR>
                          <TR bgColor=#ffffff>
                            <TD class=main align=right><B>Entered:</B></TD>
                            <? $td=date("Y-m-d");
							$egroup="$row->exp_group";
                             ?>
                            <td>      <INPUT onFocus="" value=<?php Print"$td"; ?> name=txtDate maxlength=10 size=10><input name=txtEgroup type=text value=<?php print "$egroup"; ?>>
						</td>
			<td class=main align=right><b>Quantity:</b></td>
                        <td align=right><select name="quantity" size="1">
    <option value="1" selected>1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
  </select>&nbsp;&nbsp;&nbsp;</td>
						

                          </TR>
</TBODY></TABLE><BR>
                        <CENTER><INPUT class=button type=submit value="Enter Purchase >>" name=submit>

 </FORM></TD></TR></table>


As always, thanks for any help you can give this "newbie
 
I tried that, but was not able to get the db value of "exp_group" to the pass thru variable of "txtEgroup".

Any suggestion how to do that?

Thanks
 
I'm not sure you get the concept of putting values into hidden fields.

Using jpadies suggestion just create a hidden field named txtgroup, and look for that in your other form.

Alternatively, you can append the value directly to the URL
"update_purchase.php?typ=new&txtgroup=".$valuefromDB

Wherewver it is you are using the value.

Ifthat's not what you want, you might want to be a little more specific as to how you want to address the value and where.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Code:
<html>
<head>
<title>Enter Purchases</title>
<script language="JavaScript">
function validate(m)
{
  var ename=document.frmExpense.expName.value;
  var dt = new Date(document.frmExpense.txtDate.value);
  if(document.frmExpense.expName.value=="")
  {
    alert("Enter Purchase Name");
    document.frmExpense.ename.focus();
    return false;
  }
  else if(document.frmExpense.amount.value=="")  
  {
    alert("Enter Purchase Amount");
    document.frmExpense.amount.focus();
    return false;
  }
  else if(document.frmExpense.txtDate.value=="")
  {
    alert("Enter Date");
    document.frmExpense.txtDate.focus();
    return false;
  }
  else
    return true;
}
</script>
<!--Numeric Validation-->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\-\_\{\}\`\~\=\|\!\abcdefghijklmnopqrstuvwxyz\ABCDEFGHIJKLMNOPQRSTUVWXYZ]/;
function OnlyNumeric(val) {
var strPass = val.value;
var strLength = strPass.length;
var i;
for(i=0;i<strLength;i++)
{
    
    var lchar = strPass.charAt(i);
    if(lchar.search(mikExp) != -1) 
    {
    val.value='';
    break;
    }
}
}
//  End -->
</script>
<SCRIPT language=javascript src="scripts/calendar.js"></SCRIPT>
<link rel="stylesheet" href="scripts/surylake.css" media="screen"></LINK>
<!--Afdd (/)Date-->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\!\-\abcdefghijklmnopqrstuvwxyz\ABCDEFGHIJKLMNOPQRSTUVWXYZ]/;
function dodacheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
   if (strLength==2){
        val.value = val.value + '/'
   }
   if (strLength==5){
        val.value = val.value + '/'
   }
}
//  End -->
function putPrice(m)
{
 document.frmExpense.amount.value=m.value;
 var i;
 for(i=0;i<m.length;i++)
 {
  if(m.options(i).selected==true)
    document.frmExpense.txtExpname.value=m.options(i).text;
 }
}
</script>
<!--Afdd (/) Date-->
</head>
<body bgcolor="#47A0F9">
<?php
include("conobj.php");

 $query="select exp_name,exp_price,exp_group from exp_item order by exp_name";
 $result=mysql_query($query,$db_connection);
include("header.php");
?>
<center>
                  <TABLE class=main cellSpacing=0 cellPadding=5 width="100%" align=center>
                    <TBODY>

                    <TR>
                      <TD vAlign=top>
                        <CENTER class=main><B>Enter Purchase</B></CENTER><BR>
                       <FORM action=update_purchase.php?typ=new method=post name=frmExpense onsubmit=return validate(this)>
                     
                        <input type=hidden name='r_id' value='<?php print $id; ?>'>
                        <TABLE class=main cellSpacing=1 cellPadding=5
                        align=center bgColor=#000000>
                          <TBODY>
                          <?php
                          if($upd=='Y')
                             print("<tr bgColor=#ffffff><td colspan=4 align=center>Last Purchase Updated</td></tr>");
                          ?>
                          
                          <TR bgColor=#ffffff>
                            <TD class=main align=right><B>Purchase:</B></TD>
                            <TD>
                            <select name=expName onclick="putPrice(this);">
                            <?php
                            print ("<OPTION value=0>Item</OPTION>");
                              while ($row=mysql_fetch_object($result))
                              {                            
                              print"<option value=".$row->exp_price .">".$row->exp_name ."</option>";
                              }
                            ?>
                            </select>
                            <!--INPUT class=search maxLength=32
                            name=ename-->*</TD>
                            <TD class=main align=right><B>Amount:</B></TD>
                            <TD>$ <INPUT class=search maxLength=8 size=8 
                              name=amount onKeyUp="OnlyNumeric(this);">*
                              <input type=hidden name=txtExpname><input type=hidden name=txtEgroup></TD></TR>
                          <TR bgColor=#ffffff>
                            <TD class=main align=right><B>Entered:</B></TD>
                            <? $td=date("Y-m-d");
                             ?>
                            <td>      <INPUT onFocus="" value=<?php Print"$td"; ?> name=txtDate maxlength=10 size=10></td>
            <td class=main align=right><b>Quantity:</b></td>
                        <td align=right><select name="quantity" size="1">
    <option value="1" selected>1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
  </select>&nbsp;&nbsp;&nbsp;</td>
                        

                          </TR>
</TBODY></TABLE><BR>
                        <CENTER><INPUT class=button type=submit value="Enter Purchase >>" name=submit>

 </FORM></TD></TR></table>

OK, I will admit I might not be grasping the concept..

I have put in the above code, a hidden field referencing txtEgroup.

It would appear that the hidden field txtExpname is assigned the value of $row->exp_name from the line in the java function putPrice "if(m.options(i).selected==true)
document.frmExpense.txtExpname.value=m.options(i).text;"

Is this not correct? If so, how would I perform a similar function to assign the value of $row->exp_group to txtEgroup, when is is not included in the <options> tag?


 
You are mixing up JS with PHP. They work on different planes.

I would assign the value to the hidden filed just using PHP, no JS. something like:

Code:
<input type=hidden name=txtgroup value=<?echo $row->txtEgroup $?>>







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i would also put the value inside double quotes in the html element.
 
Ohh... right, forgot.

Code:
<input type=hidden name=txtgroup value=[red]"[/red]<?echo $row->txtEgroup; ?>[red]"[/red]>

Thanx for the heads up jpadie.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I have tried your suggestion(s), all without luck, but have come up with an alternative solution that solves my problems..

Again,

Thanks very much for helping out!
 
If you have:
foo.php
blah.php

And you wish to transfer a variable, you can:
A) Define a hidden field, as suggested with name="variable_name"
B) Store the value in a session
C) Send the value in a querystring

You might have to use $_POST['variable_name'] or $_GET['variable_name'] to retrieve it!

eg. On foo.php you can have:
<input type="hidden" name="varUserID" value="<?=$_row['uid']?>" />

Then to extract that on blah.php:
<?php
echo $_POST['varUserID'];
echo $_GET['varUserID'];
?>

POST vs GET depends on what you define in the form.
I'm not a big fan of visible querystring which reveals more than you want the user to know.

ps. if your application has back -> forth, I would use session.. Just remember to check if session is set, before setting it, so you dont overwrite a value if they go back in theire browser.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top