How can I check if a radio button is selected using JavaScript and PHP -


i have few images should radio button , uses should select , proceed page, isn't working, returns "please mark product".

php code

        $shoppingtable = array(      array("radiovalue" => 001, "url_image" => "/images/a.png", "price" => 19.99, "coins" => 25, "package" => 1),     array("radiovalue" => 002, "url_image" => "/images/b.png", "price" => 34.99, "coins" => 40, "package" => 2),     array("radiovalue" => 003, "url_image" => "/images/c.png", "price" => 49.99, "coins" => 80, "package" => 3),     array("radiovalue" => 004, "url_image" => "/images/d.png", "price" => 89.99, "coins" => 125, "package" => 4),     array("radiovalue" => 005, "url_image" => "/images/e.png", "price" => 124.99, "coins" => 150, "package" => 5), );  echo '<form action="http://'.$_server['http_host'].'/?view=donatecoins&action=check" method="post" onsubmit="return validadados(this);"><input type="hidden" name="method" value="1"/>'; '     foreach($shoppingtable $i => $shop) {         echo '<div id="'.$shop['package'].'" class="product coin">             <span class="title">                 <span class="amount">'.$shop['coins'].'</span>                 <br>                     coins             </span>              <div class="product_coin_image" style="background-image: url('.$shop['url_image'].');"></div>             <div class="price">r$ '.$shop['price'].'</div>             <input type="radio" name="prodvalor_1" id="prodvalor_1" value="'.$shop['radiovalue'].'"/>         </div>';     } echo '</form>'; 

javascript function

function validadados(frm) {     var f;     (i=frm.prodvalor_1.length-1; > -1; i--) {         if (frm.prodvalor_1[i].checked) {             f = true;             break;         }     }      if(!f) {         alert("please mark product.");         return false;     } } 

you can try using isset function of php. check if radio button checked or not. isset function returns true if radio button checked.

 <?php      f=false;     if (isset($prodvalor_1))       {          f=true;          echo "a product checked";       }      if(!f) {       echo "please mark product.";     } ?> 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -