Mohan
Mohan

Reputation: 109

Avoid empty lines in php checkbox

I have html form wherein 7 check box are there. When all the boxes are checked, it is giving good result. Whereas any body gives 3 or 4 check, it shows the result with empty lines in between. It looks very indecent. Any body can suggest the corrective code for that.

the HTML code for the check box:

CONDITIONS: <br />
<input type="checkbox" name="c1" value="Invoice">Invoice <br />
<input type="checkbox" name="c2" value="LR copy">LR copy <br />
<input type="checkbox" name="c3" value="Test Reports">Test Reports<br />
<input type="checkbox" name="c4" value="Draft"> Draft<br />
<input type="checkbox" name="c5" value="Check-list">Check-List <br />
<input type="text" name="c6" placeholder=" Add any" maxlength="25" size="40"><br />
<input type="text" name="c7"  placeholder=" Add any clause"  maxlength="25" size="40">

the php code for the above check-box:

    <html>
<head>
<style type="text/css">

table { margin: 1em 0 0 0; border-collapse: collapse; }
td {margin: 5px 0 0 0;padding: .4em ;  border: 0px solid; height: 12px; }
tr td:first-child {font-weight: bold; width: 250px; color: black; height: 10px; font-size: 18px;}
tr td:last-child {width: 450px; color: blue;height: 10px; font-size: 18px; }

p {margin: 5px 0 0 20px; width: 700px; line height: 20px; text-align: justify; font-size:105%;
font-family: Arial, "Lucida Sans Unicode", Verdana, sans-serif;}

#trim {margin: 5px 0 0 20px; width: 700px; line height: 20px; text-align: center; font-size:150%;
font-family: Arial, "Lucida Sans Unicode", Verdana, sans-serif;}

.z{ margin: 5px 0 0 20px; width: 700px;}
.z1{ margin: 5px 0 0 20px; width: 700px;}

</style>

</head>
<body>

<div id="trim">
<b> FORMAT  </b>    
<hr size="2" noshade>                      
</div>


<?

$k1 = strtoupper($_REQUEST['k1']);
$k2 = strtoupper($_REQUEST['k2']);
$k3 = strtoupper($_REQUEST['k3']);
$k4 = strtoupper($_REQUEST['k4']);
$k5 = strtoupper($_REQUEST['k5']);
$k6 = strtoupper($_REQUEST['k6']);
$k7 = strtoupper($_REQUEST['k7']);
$k22 = strtoupper($_REQUEST['k22']);
$k23 = strtoupper($_REQUEST['k23']);
$k8 = strtoupper($_REQUEST['k8']);
$k9 = strtoupper($_REQUEST['k9']);
$k10 = strtoupper($_REQUEST['k10']);
$k11 = strtoupper($_REQUEST['k11']);
$k12 = strtoupper($_REQUEST['k12']);
$k13 = strtoupper($_REQUEST['k13']);
$k14 = strtoupper($_REQUEST['k14']);
$k15 = strtoupper($_REQUEST['k15']);
$k16 = strtoupper($_REQUEST['k16']);
$c6 = strtoupper($_REQUEST['c6']);
$c7 = strtoupper($_REQUEST['c7']);
$k19 = strtoupper($_REQUEST['k19']);
$k20 = strtoupper($_REQUEST['k20']);
$k21 = strtoupper($_REQUEST['k21']);
$r1 = strtoupper($_REQUEST['r1']);
$r2 = strtoupper($_REQUEST['r2']);
$r3 = strtoupper($_REQUEST['r3']);
$r4 = strtoupper($_REQUEST['r4']);
$r5 = strtoupper($_REQUEST['r5']);
$r6 = strtoupper($_REQUEST['r6']);
$r7 = strtoupper($_REQUEST['r7']);
$r8 = strtoupper($_REQUEST['r8']);
$r9 = strtoupper($_REQUEST['r9']);
$r10 = strtoupper($_REQUEST['r10']);
$c1 = strtoupper($_REQUEST['c1']);
$c2 = strtoupper($_REQUEST['c2']);
$c3 = strtoupper($_REQUEST['c3']);
$c4 = strtoupper($_REQUEST['c4']);
$c5 = strtoupper($_REQUEST['c5']);

$c8 = strtoupper($_REQUEST['c8']);
$c9 = strtoupper($_REQUEST['c9']);
$c10 = strtoupper($_REQUEST['c10']);
$c11 = strtoupper($_REQUEST['c11']);
$c12 = strtoupper($_REQUEST['c12']);
$c13 = strtoupper($_REQUEST['c13']);
$c14 = strtoupper($_REQUEST['c14']);
$c15 = strtoupper($_REQUEST['c15']);
$c16 = strtoupper($_REQUEST['c16']);
$c17 = strtoupper($_REQUEST['c17']);
$c18 = strtoupper($_REQUEST['c18']);
$c19 = strtoupper($_REQUEST['c19']);
$c20 = strtoupper($_REQUEST['c20']);
$c21 = strtoupper($_REQUEST['c21']);
$c22 = strtoupper($_REQUEST['c22']);
$c23 = strtoupper($_REQUEST['c23']);
$c24 = strtoupper($_REQUEST['c24']);
$c25 = strtoupper($_REQUEST['c25']);
$p1= strtoupper($_REQUEST['p1']);
$p2 = strtoupper($_REQUEST['p2']);

if(empty($k2)){$kk="  "; }else{$kk="(Code: $k2)";}


$html = "<p> <div class='z1'>
<b><u> PROJECT DETAILS </u></b>
<table>
<tr><td> Project Name: </td>  <td > $k1 &nbsp; &nbsp; $kk</td></tr>
<tr><td> Purchase Order no:</td> <td > $k3 &nbsp; &nbsp;Dtd: $k4</td></tr>
<tr><td>Total Order Value:</td>  <td >$k6 </td></tr>
<tr><td> Materials Ordered </td> <td >$k5 </td></tr>
<tr><td> Supplier Details: </td> <td >$k22  <br /> $k23</td></tr>
</table>
<br /><b><u> DISPATCH CONDITIONS </u></b>
<table>
<tr><td> Shipment from: </td> <td > $k11</td></tr>
<tr><td> Shipment to: </td> <td >$k12 </td></tr>
<tr><td> Dispatch starting date: </td> <td >$k9 </td></tr>
<tr><td> Last date of dispatch: </td> <td >$k10 </td></tr>
<tr><td> Part-Shipment: </td> <td >$r1 </td></tr>
<tr><td> Part-Transshipment: </td> <td > $r2</td></tr>
<tr><td> Transport Charges: </td> <td > $r4</td></tr>
<tr><td> Insurance Charges: </td> <td > $r3</td></tr>
<tr><td> Basis of price: </td> <td > $r5</td></tr>
</table>

<br /><b><u> BANK DETAILS </u></b>
<table>
<tr><td> L/C amount: </td> <td > $k8 <br /> Rs. $k30 </td></tr>
<tr><td> Usance period: </td> <td > $r9  Days from the date of dispatch.</td></tr>
<tr><td> Usance interest: </td> <td>$k15 Days to Supplier A/c. and  $k16 Days to Buyer A/c.</td></tr>
<tr><td> Usance interest charges:  </td> <td >Not more than $k14 % p.a </td></tr>
<tr><td> L/C Charges: </td> <td > Opening charges to $r7 and discount charges to $r8</td></tr>
<tr><td> Negotiation to be done: </td> <td > within $r10  Days from the date of dispatch.</td></tr>
<tr><td> Place of Expiry: </td> <td >  $k21</td></tr>
</table>

<br /><b><u> DOCUMENTS REQUIRED:</u></b>
<ul><li>$c1 
</li><li>$c2 
</li><li>$c3
</li><li>$c4 
</li><li>$c5 
</li><li>$c6
</li><li>$c7 
</li></ul>
<br /><b><u> L/C CONDITIONS </u></b>
<ul><li>
$c8 </li><li>
$c9 </li><li>
$c10</li><li>
$c11 </li><li>
$c12</li><li>
$c13 </li><li>
$c22 </li><li>
$c23 </li></ul>

<br /><b><u> ADDITIONAL CONDITIONS </u></b>
<ul><li>
$c14 </li><li>
$c15 </li><li>
$c16 </li><li>
$c17 </li><li>
$c18 </li><li>
$c19 </li><li>
$c20 </li><li>
$c21 </li><li>
$c24 </li><li>
$c25 </li><li>
$k19 </li><li>
$k20 </li></ul>

</div></p>";

$html .= "<div class='z'> <hr /> Place: <b>".$p1."</b></div>";
$html .= "<div class='z'> Date:  $p2 <div style='float:right;font-size: 12pt'><i> (Authorized Signature ) </i></div></div>";


print($html);

?>

</body>
</html>

Upvotes: 0

Views: 134

Answers (2)

Hardy
Hardy

Reputation: 5631

You are always echoing all the lines. when some values are empty, there will be just empty line (list item). So you have to check that the values exists. Little example:

    <?

    $html = '<p>CONDITIONS:</p><ul>';

    function echoRow(&$html, $val) {

      if (isset($val) && !empty($val))
        $html .= '<li>' . $val . '</li>';  
    }


    echoRow($c1);
    echoRow($c2);
    echoRow($c3);
    echoRow($c4);
    echoRow($c5);
    echoRow($c6);
    echoRow($c7);

    $html .= '</ul>';

    print($html);

?>

Upvotes: 1

user1111
user1111

Reputation: 11

This is because when a checkbox is not checked, then its value is not send to server. For example if C1 is not checked, then it is not send in $_POST values. What you can do is check if each of checkbox is set and display only those whose value exits. If you want you can show alternative message for checkbox that is not set. You can do something like this

    $html="<ul>";

    if(isset($c1))
          $html.="<li>$c1</li>"
    else 
    {
     //do nothing or show alternative message 
     $html.="<li>Invoice not selected</li>"
    }

// do same for $c2....$c7
$html.="</ul>";
print($html);

Upvotes: 0

Related Questions