Jump to content

[SOLVED] Hidden fields dont work????


sum_guy

Recommended Posts

Hi, I have used a while loop to loop through my database to display products. I want to place a "buy" button in the loop so when clicked the details would be stored into a session array. I want to use hidden fields but it doesn't work. This is the code I have so far please help.

 

 

<?php

session_start();

/**

* @author Grant Kinsman

* @copyright 2009

*/

$dbhost = 'localhost';

$dbuser = 'root';

$dbpwd = '****';

$conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting');

$dbname = 'karensKards';

mysql_select_db($dbname);

$result = mysql_query("SELECT * FROM birthdays");

 

if ($_POST['submit']) {

  $_SESSION['total'] = $_SESSION['total'] + $_POST['submit'];

$_SESSION['prod']++;

$_SESSION['basket'] = $_POST['submit'];

}

 

?>

<html>

<head>

<title>Mums Cards</title>

<link rel="stylesheet" href="style.css" style="text/css" />

</head>

 

<body>

<div id="titleBar">

<p class="shopCar"><b>Cart</b><br>

Items - <?php echo $_SESSION['prod']; ?><br>

Total - £<?php echo $_SESSION['total']; ?>

<a href="checkout.php" style="padding-left=0px">Checkout</a></p>

</div>

<div id="menu">

<a href="index.php">Home</a>

<a href="products.php">Products</a>

</div>

<div id="main">

<div style="overflow:auto;height:630">

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<?php

while ($row = mysql_fetch_array($result)) {

echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;'

onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125'><br>";

echo $row['imgPath']. "<br>";

echo "Price:£" .$row['price']. "<br>";

echo "Details:" .$row['details'];

 

echo "<input type='submit' name='submit' value='buy' style='float:right'></div>";

}

 

?>

</form>

</div>

</div>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/
Share on other sites

<?php

session_start();

/**

* @author Grant Kinsman

* @copyright 2009

*/

$dbhost = 'localhost';

$dbuser = 'root';

$dbpwd = 'wanker';

$conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting');

$dbname = 'karensKards';

mysql_select_db($dbname);

$result = mysql_query("SELECT * FROM birthdays");

 

if ($_POST['submit']) {

  $_SESSION['total'] = $_SESSION['total'] + $_POST['price'];

$_SESSION['prod']++;

 

if ($_SESSION['cart'] == "") {

$_SESSION['basket'] = array($numba => array(

"quantity" => 1,

"image" => $_POST['imgPath'],

"price" => $_POST['price'],

"info" => $_POST['details']));

} else {

$_SESSION['basket'][$numba]= array(

  'quantity' => 1,

  "image" => $_POST['imgPath'],

  "price" => $_POST['price'],

  "info" => $_POST['details']);

}

}

 

?>

<html>

<head>

<title>Mums Cards</title>

<link rel="stylesheet" href="style.css" style="text/css" />

</head>

 

<body>

<div id="titleBar">

<p class="shopCar"><b>Cart</b><br>

Items - <?php echo $_SESSION['prod']; ?><br>

Total - £<?php echo $_SESSION['total']; ?>

<a href="checkout.php" style="padding-left=0px">Checkout</a></p>

</div>

<div id="menu">

<a href="index.php">Home</a>

<a href="products.php">Products</a>

</div>

<div id="main">

<div style="overflow:auto;height:630">

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<?php

while ($row = mysql_fetch_array($result)) {

echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;'

onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125' alt='" .$row['imgPath']. "'><br>";

echo $row['imgPath']. "<br>";

echo "Price:£" .$row['price']. "<br>";

echo "Details:" .$row['details'];

echo "<input type='hidden' name='image' value='" .$row['imgPath']. "'>";

echo "<input type='hidden' name='price' value='" .$row['price']. "'>";

echo "<input type='hidden' name='details' value='" .$row['details']. "'>";

echo "<input type='submit' name='submit' value='buy' style='float:right'></div>";

}

 

?>

</form>

</div>

</div>

</body>

</html>

Check the below code i have added an row_id field which will pinpoint you wish row was selected, clicked and needs update'ing

 

<?php
session_start();
/**
* @author Grant Kinsman
* @copyright 2009
*/
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'wanker';
$conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting');
$dbname = 'karensKards';
mysql_select_db($dbname);
$result = mysql_query('SELECT * FROM birthdays');

if (isset($_POST['submit'])) {
    $row_id = $_POST['row_id'];//now update this specific row
    $_SESSION['total'] += $_POST['price'];
    $_SESSION['prod']++;
    if (empty($_SESSION['cart'])) {
        $_SESSION['basket'] = array($numba => array(
            'quantity' => 1,
            'image' => $_POST['imgPath'],
            'price' => $_POST['price'],
            'info' => $_POST['details']));
    } else {
        $_SESSION['basket'][$numba]= array(
            'quantity' => 1,
            'image' => $_POST['imgPath'],
            'price' => $_POST['price'],
            'info' => $_POST['details']);
    }
}

?>
<html>
    <head>
        <title>Mums Cards</title>
        <link rel="stylesheet" href="style.css" style="text/css" />
    </head>

    <body>
        <div id="titleBar">
            <p class="shopCar"><b>Cart</b><br>
	Items - <?php echo $_SESSION['prod']; ?><br>
	Total - £<?php echo $_SESSION['total']; ?>
                <a href="checkout.php" style="padding-left=0px">Checkout</a></p>
        </div>
        <div id="menu">
            <a href="index.php">Home</a>
            <a href="products.php">Products</a>
        </div>
        <div id="main">
            <div style="overflow:auto;height:630">
                <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
                    <?php
                    while ($row = mysql_fetch_array($result)) {
                        echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;'
		onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125' alt='" .$row['imgPath']. "'><br>";
                        echo $row['imgPath']. "<br>";
                        echo "Price:£" .$row['price']. "<br>";
                        echo "Details:" .$row['details'];
                        echo "<input type='hidden' name='row_id' value='" .$row['id']. "'>"; // added, modify if necessary to the proper column name
                        echo "<input type='hidden' name='image' value='" .$row['imgPath']. "'>";
                        echo "<input type='hidden' name='price' value='" .$row['price']. "'>";
                        echo "<input type='hidden' name='details' value='" .$row['details']. "'>";
                        echo "<input type='submit' name='submit' value='buy' style='float:right'></div>";
                    }

                    ?>
                </form>
            </div>
        </div>
    </body>
</html>

No that still doesn't work. The hidden fields will bring back a value of 4 which is the amount of items within the table not the price. When I change the submit value to "$row['price']" it works but I want the value to be "buy". I need the hidden fields to update the session cart. Please advise.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.