Jump to content

Recommended Posts

Hello,

 

How do i pass a variable to another webpage? Try as I might I cannot figure it our and make it happen.

 

I have a small database (MySQL) I have created and I am trying to write a routine to update various fields, as and when I need to.

 

The database is defined as such:

CREATE TABLE `consumer` (

  `id` mediumint(5) NOT NULL AUTO_INCREMENT,

  `cat` varchar(15) NOT NULL DEFAULT '',

  `commodity` varchar(30) DEFAULT '',

  `lowprice` int(11) NOT NULL DEFAULT '0',

  `highprice` int(11) NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;

 

I need to be able to update the lowprice and highprice fields, I can select the record I want to update, but cannot get the value of the field "id" to pass over to the next page.

 

I am using this code:

<FORM NAME="getcategory" METHOD="GET" ACTION="update_low.php">

 

<select name="commodity">

<option value="Appliances Brand">Appliances Brand</option>

<option value="Baby Food Line">Baby Food Line</option>

<option value="Brewery">Brewery</option>

</select>

 

<br><INPUT TYPE="submit" VALUE=" Send Request "> </P>

</FORM>

 

to select the record to edit. How do I pass the contents of the id field to the update_low.php webpage, and so be able to update the correct record with an update query?

 

Hope this is clear. Not very good at asking techie questions, I know what I want to do myself.

 

Thanks for looking in.

 

Suzanne  :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/131524-passing-a-variable-to-the-next-webpage/
Share on other sites

Ok so we tried the first option, goggled for SESSIONS. Found several sites, tried one, eventually found out I need session_start(); on each page. Why do Web Tutorials not give you the whole picture?

 

Anyway, it would appear my variables are being stored, but, it seems they are not being transfered to my next page. If I use Print_r ($_SESSION); to see what is there on the second page, all I get is Array ( [commodity] => [id] => ) which to me looks empty when it should say something like Furniture and 11.

 

What am I doing wrong? I am trying to work this out, but it is screwing my brains at the moment.

 

Suzanne  :(

There's no way for me to tell you what's wrong with your code unless you post it.  Basic example of using sessions goes like this:

 

page1.php

<?php
   session_start();
   $_SESSION['somevar'] = 'something';

   // example to get to next page  
   echo "<a href='page2.php'>Page 2</a>";
?>

 

page2.php

<?php
   session_start();
   echo $_SESSION['somevar']; // output: something
?>

 

 

But since you say you are using print_r on your 2nd page and it's showing your session vars (albeit empty vars), then looks like you have that part down.  Which means you probably need to go back to page1 and take a look at what you're assigning to those session vars.  Example:

 

$_SESSION['blah'] = $blah;

 

Well...is $blah holding what you expect? Again, I can't really be more specific than that without you posting code.

 

 

Hi CV,

 

Thanks for your patience.

 

I think it is inexperience on my part at writing php code. I imagine if you looked at my code you would cringe, and that I think is where my problem is, sloppy coding maybe.

 

If I stop 'page1' from going to 'page2' then the SESSION variables get written properly.

 

I checked this by taking out the page name of 'page2' from the ACTION part in the form. It stays on 'page1', and my SESSION variables are printed on the screen exactly as I expect. If I then put the ACTION part back into the form, and make my selection on the form, 'page1' then goes to 'page2' before the SESSION variables have chance to be written, which is why they are empty on 'page2'.

 

I tried using a temporary database to hold the value I want on 'page2'. This works if I keep 'page1' from going to 'page2', the value corresponding to my selection on my form is written to a field on a temporary database, let 'page1' go to 'page2' and the value doesn't change in the temporary database field, because, again, it doesn't get chance to be written there.

 

Does this make sense? My programming efforts are to blame, but i don't know how to fix them.

 

Suzanne.

Well thanks for your help guys, I guess my web page is doomed to be forever unwritten and functioning.

 

As far as I can see everything happens on a webpage 'all at the same time' from top to bottom, I don't really know properly how PHP works, I can only go on my previous experience and that means using BASIC many years ago, but of course PHP doesn't work like that, execute a bit, then do something dependent on the input from the user, execute a bit more of the 'program', etc....

 

I think this is why I cannot work out why I lose my variables, I am desparately trying to understand how this all works.

 

Suzanne.

This is the first page. After you pick yourself up off the floor from laughing.......

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php 
session_start();

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<?php
include'metatags.php';
?>
<title>Suzanne's Share Chart</title>
<link rel="stylesheet" href="2col_leftNav.css" type="text/css" />
<style type="text/css">
<!--
.style2 {font-size: medium}
.style3 {color: #FF0000}
-->
</style>
</head>
<!-- The structure of this file is exactly the same as 2col_rightNav.html;
     the only difference between the two is the stylesheet they use -->
<body>
<div id="masthead">
  <h1 class="style3" id="siteName">Suzanne's Share Chart</h1>
<?php
//include'navbar-affiliates.php';
?>
<!-- end masthead -->
<div id="content">
  <div id="breadCrumb"> <a href="index.php">Home</a> / Update Low</div>
  <h2 id="pageName">Update A Price</h2>
  <div class="feature"> <h3>Suzanne's Share Chart</h3>
  </div>
<div class="feature"> 
<?php
//connecting to the server and then to the database

$host = "localhost";
$username = "root"; 
$password = "4396974";
$database = "shares";
//$table1 = "consumer";

mysql_connect ($host, $username, $password) or die ('Unable to connect to the database server : ' . mysql_error());
mysql_select_db ($database) or die ('Unable to connect to the database because : ' . mysql_error());

// $result = mysql_query("SELECT * FROM $table1 ");
//   $numrows = mysql_num_rows($result);

?>

<FORM NAME="getcategory" METHOD="GET" ACTION="update_low.php">

<select name="commodity">
<option value="Appliances Brand">Appliances Brand</option>
<option value="Baby Food Line">Baby Food Line</option>
<option value="Brewery">Brewery</option>
<option value="Canned Goods Line">Canned Goods Line</option>
<option value="Caviar Farm">Caviar Farm</option>
<option value="Cereals Brand">Cereals Brand</option>
<option value="Clothing Store Chain">Clothing Store Chain</option>
<option value="Coffee Shop Chain">Coffee Shop Chain</option>
<option value="Cosmetics Line">Cosmetics Line</option>
<option value="Designer Clothing Brand">Designer Clothing Brand</option>
<option value="Designer Handbag Brand">Designer Handbag Brand</option>
<option value="Fast Food Restaurant Chain">Fast Food Restaurant Chain</option>
<option value="Footwear Brand">Footwear Brand</option>
<option value="Jewelry Store Chain">Jewelry Store Chain</option>
<option value="Luxury Automobiles Line">Luxury Automobiles Line</option>
<option value="Luxury Furniture Line">Luxury Furniture Line</option>
<option value="Snacks Brand">Snacks Brand</option>
<option value="Soft Drinks Brand">Soft Drinks Brand</option>
<option value="Toys Line">Toys Line</option>
<option value="Wedding Dress Brand">Wedding Dress Brand</option>
<option value="Winery">Winery</option>
</select>

<br><INPUT TYPE="submit" VALUE=" Send Request "> </P> 
</FORM>


<?php
$category="consumer";

import_request_variables(gp, "formval_");
$commodity = $formval_commodity;

//print "category >".$category."< ";

$result = 0;

$searchstring = "SELECT *
FROM $category
WHERE `commodity` = '$commodity' 
ORDER BY lowprice ASC";
//print "<br>Search string ".$searchstring;
$result = mysql_query($searchstring);
$numrows = mysql_num_rows($result);
//print "Found $numrows entries for category.... $category\n";
// print  "<br>Result string ".$result;

//	echo "<br /><TABLE width=60% bgcolor=#FDEDF9 BORDER=1 cellpadding=1 valign=top>";
//echo "<TR><TD width=20%><B><u>id</u></B><TD width=20%><B><u>category</u></B><TD width=20%><B><u>commodity</u></B><TD width=20%><B><u>low price</u></B><TD width=20%><B><u>high price</u></B>";
//echo "<TR><TD width=30%><B><u>commodity</u></B><TD width=15%><B><u>low price</u></B><TD width=15%><B><u>high price</u></B>";

while ($myrow = mysql_fetch_array($result))
{
$id = $myrow['id'];
$cat = $myrow['cat'];
$comm = $myrow['commodity'];
$lowprice = $myrow['lowprice'];
$highprice = $myrow['highprice'];

//	print "<TR><TD>".$id."<TD>".$cat."<TD>".$comm."<TD>".$lowprice."<TD>".$highprice;
//	print "<TR><TD>".$comm."<TD>".$lowprice."<TD>".$highprice;
}


//echo "</TABLE>";

$_SESSION["commodity"] = $comm;
$_SESSION["id"] = $id;

//echo $_SESSION["commodity"];
//echo $_SESSION["id"];

?>

  </div>
  <div class="story">
    <h3> </h3>
<?php
//include'paypal.php';
?>
  </div>
</div>
<!--end content -->
<div id="navBar">
<?php
include'navbar-search.php';
?>
<?php
//include'navbar-left.php';
?>
<?php
//include'navbar-headlines.php';
?>
</div>
<!--end navbar -->
<?php
include'navbar-bottom.php';
?>
</body>
</html>

 

this is the page it goes to

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php 
session_start(); 

Print_r ($_SESSION);

echo $_SESSION["commodity"];
echo $_SESSION["id"];

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<?php
include'metatags.php';
?>
<title>Suzanne's Share Chart</title>
<link rel="stylesheet" href="2col_leftNav.css" type="text/css" />
<style type="text/css">
<!--
.style3 {color: #FF0000}
-->
</style>
</head>
<!-- The structure of this file is exactly the same as 2col_rightNav.html;
     the only difference between the two is the stylesheet they use -->
<body>
<div id="masthead">
  <h1 class="style3" id="siteName">Suzanne's Share Chart</h1>
<?php
//include'navbar-affiliates.php';
?>
<!-- end masthead -->
<div id="content">
  <div id="breadCrumb"> <a href="index.php">Home</a> / Update Low</div>
  <h2 id="pageName">Update A Price</h2>
  <div class="feature"> <h3>Suzanne's Share Chart</h3>
  </div>
<div class="feature"> 


<FORM NAME="getprice" METHOD="GET" ACTION="">



<strong>Enter New Low Price </strong><br />
<INPUT TYPE="text" SIZE="30" NAME="newprice">
<br>

<br><INPUT TYPE="submit" VALUE=" Update "> </P> 
</FORM>

<?php
//connecting to the server and then to the database
$host = "localhost";
$username = "root"; 
$password = "4396974";
$database = "shares";
mysql_connect ($host, $username, $password) or die ('Unable to connect to the database server : ' . mysql_error());
mysql_select_db ($database) or die ('Unable to connect to the database because : ' . mysql_error());
?>

<?php
import_request_variables(gp, "formval_");
$newprice = $formval_newprice;
$result = 0;

$searchstring = "UPDATE `shares`.`consumer` SET `lowprice` = $newprice WHERE `consumer`.`id` = $id"; 

//print "<br>Search string ".$searchstring;
$result = mysql_query($searchstring);
$numrows = mysql_num_rows($result);
echo "<br /><TABLE width=60% bgcolor=#FDEDF9 BORDER=1 cellpadding=1 valign=top>";
echo "<TR><TD width=20%><B><u>id</u></B><TD width=20%><B><u>category</u></B><TD width=20%><B><u>commodity</u></B><TD width=20%><B><u>low price</u></B><TD width=20%><B><u>high price</u></B>";
//echo "<TR><TD width=30%><B><u>commodity</u></B><TD width=15%><B><u>low price</u></B><TD width=15%><B><u>high price</u></B>";

while ($myrow = mysql_fetch_array($result))
{
$id = $myrow['id'];
$cat = $myrow['cat'];
$comm = $myrow['commodity'];
$lowprice = $myrow['lowprice'];
$highprice = $myrow['highprice'];

print "<TR><TD>".$id."<TD>".$cat."<TD>".$comm."<TD>".$lowprice."<TD>".$highprice;
}
echo "</TABLE>";
?>
  </div>
  <div class="story">
    <h3> </h3>
<?php
//include'paypal.php';
?>
  </div>
</div>
<!--end content -->
<div id="navBar">
<?php
include'navbar-search.php';
?>
<?php
//include'navbar-left.php';
?>
<?php
//include'navbar-headlines.php';
?>
</div>
<!--end navbar -->
<?php
include'navbar-bottom.php';
?>
</body>
</html>

 

 

CV, those are my two pages, hope you can figure it out. I know my coding isn't the best, but I am willing to learn.

 

Suzanne.

  • 1 month later...

Move session_start(); to the top of the page (on both pages) You're outputting html before starting the session

 

**EDIT**

 

IF you had

 

ini_set('display_errors', 'On');

 

and

 

error_reporting(E_ALL | E_STRICT);

 

set you would find an error :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.