0

i want to insert only entered textbox value when I clicked Submit button the Textboxes which are empty also inserted .How can I prevent empty textbox are not to be inserted.Please get me over this and also i want insert multiple textbox values into single cell.

 <html>
    <head>
    <title>

    </title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <form method="post" action="updatevalues.php">
    <div> 
       <div class="firstdiv">
        <table >
        <thead>
        <th> Monthly</th>
        <th>Enter values</th>
        </thead>
        <tr>
        <td>F1:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F2:</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F3</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F4</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F5:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F6:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F7:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F8:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F9:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F10:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F11:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F12:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F13:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F14:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F15:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        </table>
       </div>
       <div class="seconddiv">
       <table class="table">
       <thead>
        <th>Quaterly</th>
        <th>Enter Values </th>
       </thead>
       <tbody>
        <tr>
        <td>
        Q1:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q2:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q3:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q4:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        </tbody>
       </table>
       </div>
       <div class="thirddiv">
       <table class="table" >
       <thead>
       <th>
       Annualy
       </th>
       <th>Enter values
       </th>
       </thead>
        <tbody>
            <tr>
            <td>A1</td>
            <td><input type="text" name="annualy[]">
            </tr>
        </tbody>

       </table>
       </div>
       <center><input type="submit" name="submit"></center>
    </div>
    </form>
    </body>
    </html>

updatevalues.php

<?php
$server="localhost";
$user="root";
$pswd="";
$dbname="desig";
$conn=mysql_connect($server,$user,$pswd);
mysql_select_db($dbname,$conn);

if(isset($_POST['monthly'])||isset($_POST['quaterly'])||isset($_POST['annualy']))
{
    $monthly=array();
    $textboxmonthly=trim($_POST['monthly']);
    //for($i=0;$i<sizeof($textboxmonthly);$i++)
    //{
        //$get=array();
        //$get[$i]=$textboxmonthly;
        if(!$textboxmonthly==null)
        {
           $monthly=implode(",",$textboxmonthly);

        }
    //}


    $quaterly=array();
    $textboxquaterly=trim($_POST['quaterly']);
    //for($j=0;$j<sizeof($textboxquaterly);$j++)
    //{
        //$getquaterly=array();
        //$getquaterly[$j]=$textboxquaterly;
        if(!$textboxquaterly==null)
        {
         $quaterly=implode(",",$textboxquaterly);

        }
    //}


    $annualy=array();
    $textboxannualy=trim($_POST['annualy']) ;
    //for($k=0;$k<sizeof($textboxannualy);$k++)
    //{//$getannualy=array();
        //$getannualy[$k]=$textboxannualy;
        if(!$textboxannualy==null)
        {
            $annualy=implode(",",$textboxannualy);
        }
    //}


    echo $monthly;
    echo $quaterly;
    echo $annualy;
    $insert="insert into workupdate(Empname,Designation,Monthly,Quaterly,Annualy)values('user','juniordeveloper','$monthly','$quaterly','$annualy')";
    $get=mysql_query($insert,$conn);

           echo "success";

  }   

 else 
 {
        echo "failed";

        }   


?>
arjun
  • 57
  • 6
  • Possible duplicate of [How to prevent submitting the HTML form's input field value if it empty](http://stackoverflow.com/questions/8029532/how-to-prevent-submitting-the-html-forms-input-field-value-if-it-empty) – Masivuye Cokile May 03 '17 at 11:39
  • your code will not execute because trim expects the param to be a `string` NOT and `array` – Ahmad Hajjar May 03 '17 at 11:39
  • http://stackoverflow.com/questions/13663426/prevent-form-submit-with-empty-input – Masivuye Cokile May 03 '17 at 11:39
  • http://stackoverflow.com/questions/11192477/how-to-stop-form-submit-if-empty-fields – Masivuye Cokile May 03 '17 at 11:40
  • http://stackoverflow.com/questions/11287257/stopping-empty-form-submission-php – Masivuye Cokile May 03 '17 at 11:40
  • Please explain the requirements in detail. the code is wrong and therefor doesnt explain its self. must values be present for all three time periods? If not what should go in the db fields for those? If yes, is there a minimum required number of values for each period? Perhaps all the fields should be filled in? The form only allows a single anual figure, why is it an array? – Steve May 03 '17 at 11:52

1 Answers1

0

Use this :-

isset($_POST['monthly'][0])?trim($_POST['monthly'][0]):null;
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34