Solved

Passing variables from file to file

Posted on 2002-06-24
4
182 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now