0

Hello Friends I am having issue with sizes option on my page
this is the form of sizes

<!-- Sizes And Quantity [Onclick Modal]-->
    <div class="form-group col-md-3">
        <label>Sizes & Quntity<span class="required">*</span>:</label>
        <button class="btn btn-default form-control btn-info" onclick="jQuery('#sizesModal').modal('toggle'); return false;">Sizes & Quantity</button>
    </div>
    <!-- Sizes & Quantity Preview -->
    <div class="form-group col-md-3">
        <label for="sizes">Size & Quantity Preview:</label>
        <input type="text" name="sizes" id="sizes" class="form-control" value="<?= ((isset($_POST['sizes']))?sanitize($_POST['sizes']):''); ?>" disabled/>
    </div>

and this is modal it is using when click on button

<!-- Modal for sizes & quantity -->
<div class="modal fade" id="sizesModal" tabindex="-1" role="dialog" aria-labelledby="sizesModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="sizesModalLabel">Size & Quantity</h4>
      </div>
      <div class="modal-body">
        <div class="container-fluid">
            <?php for($i=1; $i <= 12; $i++): ?>
                <!-- Size Edit -->
                <div class="form-group col-md-4">
                    <label for="size<?= $i; ?>">Size</label>
                    <input type="text" class="form-control" id="size<?= $i; ?>" name="size<?= $i; ?>" value="<?= ((!empty($sArray[$i-1]))?$sArray[$i-1]:''); ?>" />
                </div>
                <!-- Quantity Edit -->
                <div class="form-group col-md-2">
                    <label for="qty<?= $i; ?>">Quantity</label>
                    <input type="number" class="form-control" id="qty<?= $i; ?>" name="qty<?= $i; ?>" value="<?= ((!empty($qArray[$i-1]))?$qArray[$i-1]:''); ?>" min="0"/>
                </div>
            <?php endfor; ?> 
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" onclick="updateSizes();jQuery('#sizesModal').modal('toggle');return false;">Save changes</button>
      </div>
    </div>
  </div>
</div>

And this is function to update sizes

function updateSizes(){
        var sizeString ='';
        for (var i= 1; i<=12; i++){
            if(jQuery('#size'+i).val() != ''){
                sizeString += jQuery('#size'+i).val()+':'+jQuery('#qty'+i).val()+',' ;
            }
        }
        jQuery('#sizes').val(sizeString);
    }

My issues are
1. When the form is submited and if it gives error the sizes field shouldn't be black it should have already entered values like every feild holds them it should do the same it shouldn't erase them so that user don't need to re-enter them
2.Whenever i use 'sizes' in php it gives me error as sizes is undefined.

This is PHP where i am using it its on the same page

if ($_POST) {
if (!empty($_POST['sizes'])) {
                $sizeString = sanitize($_POST['sizes']);
                $sizeString = rtrim($sizeString,',');
                echo $sizeString;
                $sizesArray = explode(',', $sizeString);
                $sArray = array();
                $qArray = array();
                foreach ($sizesArray as $ss) {
                    $s = explode(':', $ss);
                    $sArray[] = $s[0];
                    $qArray[] = $s[1];
                }
            }
            else {
                $sizesArray = array();
            }

$required = array('//Some Arrays' 'sizes');
            foreach ($required as $field) {
                if ($_POST[$field] == '') {
                    $errors[] = 'All Fields with <span class="required">*</span> are required.';
                    break;
                }
            }

Please help i can't find anything which could cause this error.

2
Contributors
8
Replies
37
Views
1 Year
Discussion Span
Last Post by cereal
1

Hi,

which library are you using? In the UI I only see dialog() not modal(). Build an example page with full requirements, so that we can test, or share the libraries you're using. A part that you could check the values sent to the form through console.log(jQuery('#sizes').val()); in browser side and var_dump($_POST);, on PHP side, to see what you get from the form request.

Edited by cereal

0

The library i am using is bootstrap library for CSS and jquey for Javascript.and this is result of

var_dump($_POST);
array(6) { ["title"]=> string(5) "Shirt" ["brand"]=> string(1) "1" ["parent"]=> string(1) "1" ["price"]=> string(5) "56.99" ["list_price"]=> string(5) "39.99" ["description"]=> string(38) "This is a nice product try it out. " } 

and even after console.log value entered in sizes module showed up in it but after submission of form they vanish which means they were submitted please explain why is there error if fuction is working and sizes too.

0

Thank you for your help now i have new problems data isn't getting uploaded to database

$insertSql = "INSERT INTO `products` (`title`, `price`, `list_price`, `brand`, `categories`, `sizes`, `image`, `description`) VALUES ('$title', '$price', '$list_price', '$brand', '$categories', '$sizes', '$dbpath', '$description')";
                $insertQuery= $db->query($insertSql);
                var_dump($insertQuery);
                // header('Location: products.php');    

This is the sql statement i appreciate if someone could help me here without me creating a new question.

0

And $sizes will be an array, correct? From your previous code I see you set two different variables: $sArray or an empty $sizesArray. You can save an array into a table column, but you have to convert it to a string:

  • through $data = implode(',', $sizes); to generate a CSV list;
  • or to a JSON string through $data = json_encode($sizes);;
  • or use $data = serialize($sizes);.

An alternative is the dynamic column available in MariaDB, a MySQL fork, which allows you to store data as JSON in a blob column type, for example:

<?php

    $conn = require 'pdo.php';
    $sizes = [123, 456, 789];
    $quantities = [17, 23];

    $stmt = $conn->prepare('INSERT INTO `test` SET `features` = COLUMN_CREATE(?, ?, ?, ?)');
    $stmt->execute(['sizes', implode(',', $sizes), 'quantities', implode(',', $quantities)]);

Which then looks like:

SELECT COLUMN_JSON(`features`) AS `features` FROM `test`;
+----------------------------------------------+
| features                                     |
+----------------------------------------------+
| {"sizes":"123,456,789","quantities":"17,23"} |
+----------------------------------------------+
1 row in set (0.00 sec)

And allows a bit more flexibility when storing variable attributes like above. Docs: https://mariadb.com/kb/en/mariadb/dynamic-columns/

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.