0

I have a form like the one below which is posted to same page, and the user can dynamically add more textbox with jquery.

        <h1> Add Your Order </h1>
    <form method="post" action="">
        <p id="add_field"><a href="#"><span> Click to Add </span></a></p>
        <div id="container">
        <input type="text" class="pid" id="' + counter + '" name="Product[]"  />
        <input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
        </div><br />
        <input type= "submit" Name="submit_order" value="Submit"> 
    </form>

everything work fine but if someone add more textbox and leave some textbox as empty then it going to submit. this is my problem i don't want to submit empty textboxes in my table and i want server side solution for this.

Here is my full code with php

<body>
    <?php
    if ( isset($_POST['submit_order']) ) {
        if ( !empty($_POST['Product'])  && !empty($_POST['Quantity']) ) {
            $product = ($_POST['Product']);
            $quantity = ($_POST['Quantity']);
            foreach ($product as $id => $value) {
            $products = ($product[$id]);
            $quantitys = ($quantity[$id]);
            $query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
            }
        }
    echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
    mysql_close();
    }
    ?>
    <?php
    if (!isset($_POST['submit_order'])) { 
    ?>
    <h1> Add Your Order </h1>
    <form method="post" action="">
        <p id="add_field"><a href="#"><span> Click to Add </span></a></p>
            <div id="container">
            <input type="text" class="pid" id="' + counter + '" name="Product[]"  />
            <input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
            </div><br />
        <input type= "submit" Name="submit_order" value="Submit"> 
    </form>
    <?php } 
    ?>
</body>
3
  • Why don't you just ignore the empty ones and process the rest? Commented Nov 1, 2014 at 20:35
  • i use !empty() function and process rest but every filed submit in my table . Commented Nov 1, 2014 at 20:40
  • If you want to prevent it from submitting you have to do it client-side. PHP can't prevent it from subnmitting. You can put required in the <input> elements, and modern browsers won't allow them to be submitted. Commented Nov 1, 2014 at 20:41

3 Answers 3

2

You can use array_filter to get the elements of the arrays that are not empty. If the number of non-empty elements is different from the original array sizes, the user left some fields blank.

$filled_product = array_filter($product);
$filled_quantity = array_filter($quantity);
if (count($filled_product) < count($product) || count($filled_quantity) < count($quantity)) {
    // Report error because of unfilled fields
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should work:

foreach ($_POST[Product] as $key => $value):
    if (empty($value)):
        unset($_POST[Product][$key]);
    endif;
endforeach;

foreach ($_POST[Quantity] as $key => $value):
    if (empty($value)):
        unset($_POST[Quantity][$key]);
    endif;
endforeach;

Comments

0

Thank you everyone for helping me, unfortunately no one give me a complete solution for this question but @Wranorn give me a idea and i change my code and yes this solve my question

here is my solution for this

    <body>
    <?php
    if ( isset($_POST['submit_order']) ) {

        $product = ($_POST['Product']);
        $quantity = ($_POST['Quantity']);

        foreach ($product as $id => $value) {
            $products = ($product[$id]);
            $quantitys = ($quantity[$id]);

            if (!empty($products) && !empty($quantitys)) {
            $query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
            }
        }
    echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
    mysql_close();
    }
    ?>
    <?php
    if (!isset($_POST['submit_order'])) { 
    ?>
    <h1> Add Your Order </h1>
    <form method="post" action="">
        <p id="add_field"><a href="#"><span> Click to Add </span></a></p>
            <div id="container">
            <input type="text" class="pid" id="' + counter + '" name="Product[]"  />
            <input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
            </div><br />
        <input type= "submit" Name="submit_order" value="Submit"> 
    </form>
    <?php } 
    ?>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.