Jump to content

Session or a 2nd call to the data base


cspitzig

Recommended Posts

Hi

 

I am creating a form with data from the database and a user input field.

I then send the info to a confirmation page

I have tried to bundle the data into Session array to send to the confirmation page, I can't get the data into the session array, although the session does test true

 

So instead I tried to do a second call to the database on the confirmation page but just end up creating a loop on my results.

 

Can someone give me some pointers please

 

order page


//call the database
require ('db.php');

//# Set the SQL statement
$sql = "SELECT * FROM assign ORDER BY id";

# Connect to database service and get link ID
$dblink = mysql_connect($hostname, $user, $pwd) or die ("Error: No connection to MySQL server\n");

# Connect to database
mysql_select_db($dbname,$dblink) or die ("Error: MySQL database not selected\n");

# Send SQL statement
$result = mysql_query($sql, $dblink) or die ("SQL query failed: $sql");

# Get number of rows (records) returned by SQL statement
$rows = mysql_num_rows($result);

//# If there is at least one row ($rows > 0) , display in table
if ($rows > 0) {
# Display the SQL query if $debug is on
if ($debug) {
	echo "\n<p><font color=\"red\"><tt>$sql</tt></font></p>\n\n";
}

echo "<form action=\"confirmation.php\" method=\"post\">";

# Build the table header
echo "<table class=\"table\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
echo "<tr>\n";
echo "\t<th>Stock ID</th>\n";
echo "\t<th class=\"item\">Item</th>\n";
echo "\t<th class=\"cost\">Cost</th>\n";
echo "\t<th class=\"amount\">Amount<br />Required</th>\n";
echo "</tr>\n\n";

# Populate the table
echo "<!-- Begin MySQL data presentation -->\n";

//	 create the test finish comment

    while ($widget = mysql_fetch_array($result)) {
        echo "<tr>\n";
        echo "\t<td class=\"item\">".$widget["id"]."</td>\n"; 
        echo "\t<td class=\"item\">".$widget["item"]."</td>\n";
        echo "\t<td class=\"cost\">".$widget["cost"]." each</td>\n"; 
        echo "\t<td class=\"amount\"><input type=\"text\" name=\"f_amount[".$widget["id"]."]\" size=\"6\"></td>\n";
        echo "</tr>\n";
   }

# Close the database link
mysql_close($dblink);

# Close the table and post the summary
echo "<!-- End MySQL data presentation -->\n\n";
echo "</table>\n";
echo "<br /><br />";
  	echo "<input type=\"submit\" value=\"Submit\" name=\"B1\" /><input type=\"reset\" value=\"Reset\" name=\"B2\" />";
echo "</form>";
} 
else {

# If no results from SQL query, send a "sorry" page
echo "<p>No records were found</p>\n";
if ($debug) {
	echo "<p>$sql</p>\n";
}
}

 

confirmation page

       
// Build the table header
echo "<table class=\"table\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" 
    summary=\"This table is used to complete an online order\">\n";
echo "<tr>\n";
echo "\t<th>Quantity</th>\n";
echo "\t<th class=\"item\">Item</th>\n";
echo "\t<th class=\"cost\">Price</th>\n";
echo "\t<th class=\"amount\">Cost</th>\n";
echo "</tr>\n\n";

//create the test finish comment

		if (isset($_POST["f_amount"])) {
		    foreach($_POST["f_amount"] as $index => $field) { 
      				if (!empty($field)){
    	echo "<tr>\n";
        echo "\t<td class=\"item\">$field</td>\n"; 
        echo "\t<td class=\"item\">".$widget["item"]."</td>\n";
        echo "\t<td class=\"cost\">".$widget["cost"]." each</td>\n"; 
        echo "\t<td class=\"amount\">calculated</td>\n";
        echo "</tr>\n";
       	}
   		}
}
       echo "</table>\n";
       print "<br />";
       //Display details
       // echo "You ordered $field items of Widget ID#$index<br>";
}
?>

 

any help would be really appreciated!

 

Link to comment
Share on other sites

Heya,

 

One thing you might consider trying is: on the confirmation page, have a form with a series of hidden input fields containing the data from the previous form. You can do this like so:

 


<form action="finaldestination.php" method="post">
    <input type="hidden" name="item" value="<?=$_POST['item']?>" />
    <input type="hidden" name="cost" value="<?=$_POST['cost']?>" />
     ...
    <input type="submit" name="confirm" value="confirm" />
    <input type="submit" name="cancel" value="cancel" />
</form>

 

Hopefully that helps.

 

EDIT:

 

If you don't want to add all those hidden inputs, or you have many of them, you can serialize/base64_encode them into one hidden input. Note, that I use base64_encode because serialized data may break out of the HTML. There might be a better encoding function, but my mind is elsewhere. Like so:

 


<?php
$data = base64_encode(serialize($_POST));
?>

<form action="finaldestination.php" method="post">
    <input type="hidden" name="data" value="<?=$data?>" />
     ...
    <input type="submit" name="confirm" value="confirm" />
    <input type="submit" name="cancel" value="cancel" />
</form>

 

And in finaldestination.php, extract it with:

 

$data = unserialize(base64_decode($_POST['data']));

Link to comment
Share on other sites

I just found out that I have to use sticky forms on my initial page,

 

would this work for a post?

 

[.code]

echo "\t<td class=\"item\"><input type="text" name="id" size="20" value="' . $_POST['.$widget['id].'] . '" /></td>\n";

[./code]

 

if I can get the syntax right?

Link to comment
Share on other sites

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.