Good day! It is necessary to make a calculator, when checkbox = checked, then goes the addition.

if (!empty($_POST['htmlcss']) && !empty($_POST['SEO']) && !empty($_POST['design'])) { if ($_POST['htmlcss'] == как узнать что checked?) { $calc = ""; } if ($_POST['SEO'] == как узнать что checked?) { $calc .= $_POST['htmlcss'] + $_POST['SEO']; } if ($_POST['design'] = как узнать что checked?) { $calc .= $_POST['htmlcss'] + $_POST['SEO'] + $_POST['design']; }echo "<input type=\"text\" value=\"{$calc}\" />"; }else{ require_once (ENGINE_DIR . '/data/price.php'); echo "<form method=\"post\" action=\"./\">"; echo "Цена на верстку HTML+CSS: <input type=\"checkbox\" id=\"htmlcss\" checked><label for=\"check1\" />{$pricecfg['htmlcss']} руб.</label><br/>"; echo "Цена SEO: <input type=\"checkbox\" id=\"seo\"><label for=\"check1\" />{$pricecfg['SEO']} руб.</label><br/>"; echo "Цена Design: <input type=\"checkbox\" id=\"design\"><label for=\"check1\" />{$pricecfg['design']} руб.</label><br/>"; echo "<input type=\"submit\" value=\"Подсчитать\">"; echo "</form>"; } echo $result; 

That's what I wrote.

How can you implement?

  • one
    my advice to you, download a book on php - johniek_comp
  • if (isset ($ _ POST ['btn'])) {$ sum = 0; foreach ($ _POST ['val'] as $ k => $ v) {$ sum + = $ v; } echo "Total price". $ sum. "rub."; } Wrote differently, everything is great - AlexSirk

3 answers 3

checked is a boolean property. Just for the checkbox, create an Onclick event Onclick and determine whether it is selected or not.

 onClick="if (this.checked) {//}" 

    I caught checkboxes at checkboxes like this:


     <input name='select_task' type='checkbox' checked='checked' value='X'> 

    Then caught value through POST []


     $select_task = @$_POST["select_task"]; 

    Received X.

       require_once (ENGINE_DIR . '/data/price.php'); echo "<form method=post>"; echo "html+css верстка: {$pricecfg['htmlcss']} руб. <input type=checkbox name=\"val[]\" value='{$pricecfg['htmlcss']}'><br/>"; echo "Продвижение сайта: {$pricecfg['SEO']} руб. <input type=checkbox name=\"val[]\" value='{$pricecfg['SEO']}'><br/>"; echo "Уникальный дизайн: {$pricecfg['design']} руб. <input type=checkbox name=\"val[]\" value='{$pricecfg['design']}'><br/>"; echo "Натяжка на CMS: {$pricecfg['cms']} руб. <input type=checkbox name=\"val[]\" value='{$pricecfg['cms']}'><br/>"; echo "<input type=submit name=btn>"; echo "</form>"; if(isset($_POST['btn'])) { $sum = 0; foreach ($_POST['val'] as $k=>$v) { $sum += $v; } echo "Общая цена " . $sum . "руб."; } 

      I decided myself, thank you all.