Page 1 of 1

On the fly variables

Posted: Tue Sep 21, 2010 9:07 am
by maxline
Hi, I am working on an inventory ordering system where the client enters an item # and it is dynamically queried against MYSQL to provide the description. What I am up against is how to pass a validation variable along as not all clients have access to all inventory.

q is the item # being passed from the form. I would also like to pass a variable for the $row[4], where I can define the [4], [5], [6] or whatever based on the client ID.
My question is 2 fold
1) pass the value via the URL as, for the sake of this discussion c, yielding:

Code: Select all

$c=$_Get["c"];
But what is the syntax for inserting $c into the $row[$c]????where $c is some value equal to the corresponding # I need to query on.


My PHP code is as follows:

Code: Select all

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'sssss', 'ssss');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("inventory", $con);

$result=mysql_query("select * from info where item_no ='$q'" ); 

$row = mysql_fetch_row($result); 
 if ($row[4] == 'N'){
  echo "<font color='red'><td>&nbsp &nbsp Invalid Entry, Item #"  .$q. "&nbsp is currently not available for you to order, Please verify the item # and try again.</td></font>";
   } 
  elseif ($row[4] == 'Y'){
  echo "<td>&nbsp &nbsp Vendor:" . $row[1] . "</td>";
  echo "<td>&nbsp &nbsp Description:" . $row[3] . "</td>";
  echo "<td>&nbsp &nbsp Cost:" . $row[7] . "</td>";
  } 
  else{echo "<font color='red'><td>&nbsp &nbsp Invalid Entry, Item #"  .$q. "&nbsp is not currently a valid Item #.</td></font>";} 
mysql_close($con);
?> 

Re: On the fly variables

Posted: Tue Sep 21, 2010 2:46 pm
by Jonah Bron
But what is the syntax for inserting $c into the $row[$c]????where $c is some value equal to the corresponding # I need to query on.
Um, this?

Code: Select all

$row[$c] = $c;
I'm not really understanding the rest of your question.

Re: On the fly variables

Posted: Tue Sep 21, 2010 4:10 pm
by maxline
I can hard code the value of $c in php.
What I am needing is to pass a variable.
I have 3 pages.
page #1 form) I can decalre a variable and send it to external Javascript file (this works)

Code: Select all

<?php 
    $v = "red"; 
?> 
 
 
 <script language="javascript" type="text/javascript">
    var v = "<?php echo $v; ?>";
</script>
page#2 javascript file) This is what I can't figure out, i is the id value entered, but I do not know the syntax to continue passing $color or color.

Code: Select all

xmlhttp.open("GET","getinfo.php?v&i="+str,true);
xmlhttp.send();

page #3 php file) I can hard code the $c and it works, but I cant get the $color to come across. What am I missing for $v to get to the .php file?

Code: Select all

<?php
$i=$_GET["i"];
$v=$_GET["v"];
$c="4";
$con = mysql_connect('localhost', 'app', 'YYYYY');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("inventory", $con);

$result=mysql_query("select * from info where item_no ='$i'" ); 

$row = mysql_fetch_row($result); 
 if ($row[$c] == 'N'){
  echo "<font color='red'><td>&nbsp &nbsp Invalid Entry, Item #"  .$i. "&nbsp is currently not available for you to order, Please verify the item # and try again.</td></font>";
   } 
  elseif ($row[$c] == 'Y'){
  echo "<td>&nbsp &nbsp CValue:"  .$c. "&nbsp </td>";
  echo "<td>&nbsp &nbsp VValue:"  .$v. "&nbsp </td>";
  echo "<td>&nbsp &nbsp Vendor:" . $row[1] . "</td>";
  echo "<td>&nbsp &nbsp Description:" . $row[3] . "</td>";
  echo "<td>&nbsp &nbsp Cost:" . $row[7] . "</td>";
  } 
  else{echo "<font color='red'><td>&nbsp &nbsp Invalid Entry, Item #"  .$q. "&nbsp is not currently a valid Item #.</td></font>";
  } 
mysql_close($con);
?>

Re: On the fly variables

Posted: Tue Sep 21, 2010 4:18 pm
by Jonah Bron
Is the javascript in page two calling page 3?

Re: On the fly variables

Posted: Tue Sep 21, 2010 4:59 pm
by maxline
Disregard, I have found "Sessions", that seems to do the trick.
I do have a question regarding sessions however. Can I assign a session variable value to that of input data or does it have to be hard coded?

If I can, May I get a quick example?

Re: On the fly variables

Posted: Tue Sep 21, 2010 5:00 pm
by Jonah Bron
I don't quite understand what you're asking. Do you want to put the session value as the default value of a input element?

Re: On the fly variables

Posted: Tue Sep 21, 2010 10:12 pm
by maxline
What I am trying to do is this:
1) user logon
2) pass that user id along to the form
3) pass that user id along to an external javascript file
4) pass that user id along to a .php script for a MYSQL query
The user ID is crucial for the query so that based on the user id, only certain results are available.
I can do 2-4 using Sessions, but I only know how to hard code the session variable.
I am needing to populate the session variable to = the user id login from #1, so it needs to be fluid, not static