• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Passing variables from file to file

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
htannoury
Asked:
htannoury
  • 2
  • 2
1 Solution
 
BatalfCommented:
$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
 
BatalfCommented:
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
 
htannouryAuthor Commented:
Thank you.
It works very well.
0
 
htannouryAuthor Commented:
Thank you.
It works very well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now