0

My code is:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert Item - SomuFinance</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script type="text/javascript" src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="addItemContainer">
        <h1>Insert Item</h1>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="leftAligned">
                <?php
                    $save_vals = FALSE;
                    if(!empty($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $save_vals = $_POST['save_vals'];
                    }
                ?>
                <div class="inp">
                    <label for="shop">Shop : </label>
                    <input type="text" id="shop" name="shop" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $shop;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="category">Category : </label>
                    <input type="text" id="category" name="category" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $category;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="item">Item : </label>
                    <input type="text" id="item" name="item" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $item;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="qnty">Quantity : </label>
                    <input type="text" id="qnty" name="qnty" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $qnty;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="unit">Unit : </label>
                    <input type="text" id="unit" name="unit" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $unit;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="price_based_on">Price based on : </label>
                    <select name="price_based_on" id="price_based_on">
                        <option value="kilos" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='kilos'){echo 'selected';}} ?>>Kilos</option>
                        <option value="packet" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='packet'){echo 'selected';}} ?>>Packet</option>
                        <option value="bottle" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='bottle'){echo 'selected';}} ?>>Bottle</option>
                        <option value="box" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='box'){echo 'selected';}} ?>>Box</option>
                        <option value="piece" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='piece'){echo 'selected';}} ?>>Piece</option>
                    </select>
                </div> <br>
                <div class="inp">
                    <label for="mrp">MRP (₹) : </label>
                    <input type="text" id="mrp" name="mrp" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $mrp;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="sellers_price">Seller's Price (₹) : </label>
                    <input type="text" id="sellers_price" name="sellers_price" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $sellers_price;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="last_updated_on">Last Updated on : </label>
                    <input type="date" id="last_updated_on" name="last_updated_on" value="<?php date_default_timezone_set('Asia/Kolkata'); if((!empty($_POST['submit']))&&($save_vals)){echo $last_updated_on;} else echo date("Y-m-d") ?>">
                </div>
            </div>
            <div class="inp">
                <input id="insertButton" type="submit" name="submit" value="Insert">
            </div>
            <div id="message">
                <?php
                    if(isset($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $result=null;

                        $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                    or die("Error Connecting to Database");

                        $query = "INSERT INTO grocery VALUES ('0', '$shop', '$category', '$item', '$qnty', '$unit', '$price_based_on', '$mrp', '$sellers_price', '$last_updated_on')";

                        if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                        {
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
                        }

                        if($result)
                        {
                            echo '<span class="success">Item Inserted Successfully!</span>';
                            $_POST['save_vals']=FALSE;  
                        }
                        else
                        {
                            echo '<span class="failure">Failed to insert Item.</span>';
                            $_POST['save_vals']=TRUE;
                        }
                    }
                ?>
                <script>
                $(document).ready(function(){
                    $("#message").fadeIn(400);
                });
                </script>
            </div>
        </form>
    </div>
</body>
</html>

I'm trying to determine whether to load the previously posted values if the operation was unsuccessful - otherwise not. If i were to simply set the value of $save_vals at the end of the script, they wouldn't exist at the beginning the next time the page is loaded. So I tried manually setting $_POST['save_vals'] so that it is available the next time the page is loaded. But I get the error:

Notice: Undefined index: save_vals in E:\wamp\www\SomuFinance\insertItem.php on line 27

Line 27 is : $save_vals = $_POST['save_vals']; What am I doing wrong? How do I ensure that the previous values are loaded ONLY IF the insertion was unsuccessful?

EDIT : While the use of a hidden input type solves the undefined problem, how do I ensure that the form is refilled ONLY when the insertion failed the last time?

Here's my updated code after using an input of hidden type:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert Item - SomuFinance</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script type="text/javascript" src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="addItemContainer">
        <h1>Insert Item</h1>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="leftAligned">
                <?php
                    $save_vals = FALSE;
                    if(!empty($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $save_vals = $_POST['save_vals'];
                    }
                ?>
                <div class="inp">
                    <label for="shop">Shop : </label>
                    <input type="text" id="shop" name="shop" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $shop;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="category">Category : </label>
                    <input type="text" id="category" name="category" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $category;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="item">Item : </label>
                    <input type="text" id="item" name="item" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $item;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="qnty">Quantity : </label>
                    <input type="text" id="qnty" name="qnty" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $qnty;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="unit">Unit : </label>
                    <input type="text" id="unit" name="unit" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $unit;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="price_based_on">Price based on : </label>
                    <select name="price_based_on" id="price_based_on">
                        <option value="kilos" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='kilos'){echo 'selected';}} ?>>Kilos</option>
                        <option value="packet" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='packet'){echo 'selected';}} ?>>Packet</option>
                        <option value="bottle" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='bottle'){echo 'selected';}} ?>>Bottle</option>
                        <option value="box" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='box'){echo 'selected';}} ?>>Box</option>
                        <option value="piece" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='piece'){echo 'selected';}} ?>>Piece</option>
                    </select>
                </div> <br>
                <div class="inp">
                    <label for="mrp">MRP (₹) : </label>
                    <input type="text" id="mrp" name="mrp" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $mrp;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="sellers_price">Seller's Price (₹) : </label>
                    <input type="text" id="sellers_price" name="sellers_price" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $sellers_price;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="last_updated_on">Last Updated on : </label>
                    <input type="date" id="last_updated_on" name="last_updated_on" value="<?php date_default_timezone_set('Asia/Kolkata'); if((!empty($_POST['submit']))&&($save_vals)){echo $last_updated_on;} else echo date("Y-m-d") ?>">
                </div>
                <input type="hidden" id="save_vals" name="save_vals">
            </div>
            <div class="inp">
                <input id="insertButton" type="submit" name="submit" value="Insert">
            </div>
            <div id="message">
                <?php
                    if(isset($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $result=null;

                        $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                    or die("Error Connecting to Database");

                        $query = "INSERT INTO grocery VALUES ('0', '$shop', '$category', '$item', '$qnty', '$unit', '$price_based_on', '$mrp', '$sellers_price', '$last_updated_on')";

                        if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                        {
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
                        }

                        if($result)
                        {
                            echo '<span class="success">Item Inserted Successfully!</span>';
                            $_POST['save_vals']=FALSE;  
                        }
                        else
                        {
                            echo '<span class="failure">Failed to insert Item.</span>';
                            $_POST['save_vals']=TRUE;
                        }
                    }
                ?>
                <script>
                $(document).ready(function(){
                    $("#message").fadeIn(400);
                });
                </script>
            </div>
        </form>
    </div>
</body>
</html>
Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35
  • 2
    You need to set `save_vals` as an `input` element with a `hidden` type. The `$_POST` is populated with the `input` elements of the form. The indices come from the `name`s of the elements. – chris85 Dec 15 '16 at 04:55
  • 1
    You don't have any `name` attribute with the text `some_vals`, – Mr. Alien Dec 15 '16 at 04:55
  • you haven't set a name into your input – Beginner Dec 15 '16 at 04:57
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85 Dec 15 '16 at 04:57
  • @chris85 What am I doing wrong? How do I ensure that the previous values are loaded ONLY IF the insertion was unsuccessful? Even the use of a hidden input doesn't solve this. This part of the question remains unanswered. Please help. – Somenath Sinha Dec 15 '16 at 05:02
  • @chris85 How would you suggest I do this so that the desired effect is reached? – Somenath Sinha Dec 15 '16 at 05:05
  • Use a `SESSION` then you wont need the `save_vals` either. You can have states saved, `failed` and `submitted`. Once `submitted` don't display it anymore, if `failed` output `POST` values and the form again. – chris85 Dec 15 '16 at 05:07
  • @chris85 would you please show me an example in an actual answer so that I can upvote it? – Somenath Sinha Dec 15 '16 at 05:08
  • I can't currently, or I could but I cant test it so I'd prefer not to. If theres nothing posted tomorrow I'll throw something up. You should be using parameterized queries, this is open to SQL injections. With the `php_self` you also are open to XSS injections. – chris85 Dec 15 '16 at 05:12

1 Answers1

0

$_POST isn't persistent between loads, the same as $save_vals, it only contains elements submitted by the form. So you would either need to add an input to the form with <input type="hidden" name="save_vals" value="<?php echo $save_vars; ?>"> or use session variables e.g. $_SESSION['save_vars']

jdow
  • 316
  • 2
  • 9