Solved

Passing variables from file to file

Posted on 2002-06-24
4
191 Views
Last Modified: 2011-09-20
I am using PHP4 on Windows 2000.
I have two php files. The first is test1.php in which i read the contents of a table in MSSQL server. on the name of each book, i have made a link in such a way when clicking on this book name i obtained the second file which is form.php " it is a form to fill by customer" but I want to pass the value of the variable "book" from the first file to the second:
I have written the link:
<a href="form.php?Book=<?php echo "$Book"?>"><?php
echo $Book ;?></a>
i have used a test in the form file to see what is the value of the variable book. but it is always null. How can i solve this?
 I want the value of this variable to be passed to this file so i can use it in another third file.
Is the way of writing the link with the question mark accompanied with the variable name=value is sufficient or there is another thing to do?

The contents of test1.php:
<html>
<head><title>TEST1</title></head>
<body>
<?php
$connection=mssql_connect("localhost", "sa","password")
or die("couldn't make connection to $connection");
$db=mssql_select_db("UAB", $connection)
or die("Couldn'y select database");
$sql = "SELECT * from Books";
$sql_result = mssql_query($sql,$connection)
      or die("Couldn't execute query.");
?>
<p><font size=5 color=blue>Click on the book you want to buy</font></p>
<table border=1>
<tr>
<th>BOOK TITLE</th>
<th>SUBJECT</th>
<th>PRICE</th>
</tr>
<?php
while ($row = mssql_fetch_array($sql_result)) {
      $Book= $row["title"];
      $subject = $row["descrip"];
      $price = $row["price"];
echo "<tr>";
echo "<td>";
?>
<p>
<a href="form.php?Book=<?php echo "$Book"?>"><?php
echo $Book ;?></a>
<?php echo" </td>
<td>$subject</td>
<td align=center><b><font color=green>$price</font></b></td></tr>
";
}
echo "</TABLE>";
mssql_free_result($sql_result);
mssql_close($connection);
?>
</body></html>

The second file form.php is:
<html>
<head>
<title>Books Form</title>
</head>
<?php
<body><form action="continue.php" method="post" name="form" id="form">
<h1>Fill in the fields below to proceed.</h1><p>
<font color=red>*</font> <font size=4>denotes a required field</font></p>
<div align="left">
  <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
      <td width="23%"><font size=5>First Name</font><font color=red>*</font></td>
      <td width="27%"><font size=5><input type="text" name="first" id="first"></font></td>
    <td width="21%"><font size="5"></font></td>
      <td width="24%"></td></tr>
    <tr>
      <td width="23%"><font size=5>
Last Name</font><font color=red>*</font></td>
      <td width="27%"><font size=5><input type="text" name="last" id="last">
        </font></td>
 <td width="21%"><font size="5">Building</font></td>
      <td width="24%"><font size=5><input type="text" name="building" id="building"></font></td>
    </tr>
    <tr>
      <td width="23%"><font size=5>
Phone Number</font><font color=red>*</font></td>
      <td width="27%"><font size=5><input type="text" name="phone" id="phone">
        </font></td>
 <td width="21%"><font size="5">Street</font></td>
      <td width="24%"><font size=5><input type="text" name="street" id="street"></font></td>  
</tr>
    <tr>
      <td width="23%"><font size=5>
Mobile Number</font></td>
      <td width="27%"><font size=5><input type="text" name="mobile" id="mobile">
        </font></td>
<td width="21%"><font size="5">City</font><font color=red>*</font></td>
      <td width="24%"><font size=5><input type="text" name="city" id="city"></font></td>
</tr>
    <tr>
      <td width="23%"><font size="5">Fax</font></td>
      <td width="27%"><input type="text" name="T1" size="20"></td>
<td width="21%"><font size="5">Country</font><font color=red>*</font></td>
      <td width="24%"><font size=5><input type="text" name="country" id="country"></font></td>
</tr>
    <tr>
      <td width="23%"><font size="5">E-mail</font><font color=red>*</font></td>
      <td width="27%"><font size=5><input type="text" name="mail" id="mail">
        </font></td>
<td width="21%"><font size="5"></font></td>
      <td width="24%"><font size=5></font></td>
</tr>

  </table>
</div>
<br>
<font size=5>
<div align=center><input type="submit" name="submit" value="Continue" id="submit">
&nbsp;&nbsp;
<input type="reset" name="reset" id="reset">
</div>
<br>
</font></form>
</body></html>
0
Comment
Question by:htannoury
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7103208
$Book is null in your first file or in the second file after you clicked the link???

If it's the first file, it's obviously something wrong in your SQL-query. If it's in the second file, maybe you have a new version of php where variables you have the access the $HTTP_GET_VARS array to get the varable.

In your second file, try to put in this code at the top:

         global $HTTP_POST_VARS;
         global $HTTP_GET_VARS;

         reset ($HTTP_POST_VARS);
         while (list($key, $value) = each ($HTTP_POST_VARS)) {

              if($key=="frameID" && $frameID){}else
              {
                 $$key = $value;                
              }
         }
         reset ($HTTP_GET_VARS);
         while (list($key, $value) = each ($HTTP_GET_VARS)) {

              if($key=="frameID" && $frameID){}else
              {
                 $$key = $value;                
              }
         }
0
 
LVL 32

Accepted Solution

by:
Batalf earned 400 total points
ID: 7103210
What this code does is that it loops through the assosiative arrays $HTTP_GET_VARS and $HTTP_POST_VARS and creates a variable $Book out of $HTTP_GET_VARS["Book"];

An other solution for you is to replace all occurences of $Book in your second file with $HTTP_GET_VARS["Book"]

0
 

Author Comment

by:htannoury
ID: 7103303
Thank you.
It works very well.
0
 

Author Comment

by:htannoury
ID: 7103328
Thank you.
It works very well.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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…

830 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