javascript - When I click any button in my view page the form is submitted -


hi using codeigniter in view page have abutton add table row dynamically , have form submit button. when click add button form gets submitted. how stop form submitting when add button clicked , how make form submit when actual submit button clicked.

<html>  <head>   <meta charset="utf-8">   <link href="<?php echo base_url(); ?>assets/css/elfanto_css.css" rel="stylesheet">   <link href="<?php echo base_url(); ?>assets/css/style.css" rel="stylesheet">   <link href="<?php echo base_url(); ?>assets/css/demo.css" rel="stylesheet">   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script type='text/javascript' src='//code.jquery.com/jquery-compat-git.js'></script>   <script type='text/javascript'> var baseurl='<?php echo base_url(); ?>'; $(function() {     $('#test').on('change', 'input[name="pid[]"]', function() {         var indexofthechangedrow = $(this).closest("tr").index();         get_values(this.value, indexofthechangedrow);     }); });   function get_values(val,rowindex) {  $.ajax({     url: baseurl + 'admin/billing/getdetails',     type: 'post',     data: {         val: val     },     success: function (indexofthechangedrow, response) {         if (response != "") {             var json = jquery.parsejson(response),                 rowtoupdate = $("#test tr:nth-child(" + (indexofthechangedrow + 1) + ")");              // add changed row context restrict search classes 1 row             $('.description', rowtoupdate).val(json.description);             $('.type', rowtoupdate).val(json.type);             $('.qty_used', rowtoupdate).val(json.qty_used);              $('.qty_prch', rowtoupdate).val(json.qty_prch);          }     }.bind(window, rowindex),     error: function (response) {         alert("error");     } }); } function loadgrandtotal() {     var sum = 0;     $('.subtot').each(function () {         var prodprice = number($(this).text());         sum = sum + prodprice;     });     $("#totalrst").text(sum.tofixed(2)); }   function displayresult() { <?php        $attributes = array('class' => 'form-horizontal', 'id' => '');       $options_employee = array('' => "select");       foreach ($employee $row)       {         $options_employee[$row['first_name']] = $row['first_name'];       }     $dropdown = form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');        ?>      var complex = <?php echo json_encode($dropdown); ?>;      var row = document.getelementbyid("test").insertrow(-1);     row.innerhtml = '<td><div>'+complex+'</div></td><td><input type="text" name="start_time[]" value="" style="width:35px;"/></td><td><input type="text" name="pid[]" style="width:35px;"/></td><td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td><td><input type="text" class="type" value="" style="width:45px;"/></td><td><input type="text" class="qty_prch" value="" style="width:45px;"/></td><td><input type="text" class="qty_used" value="" style="width:45px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td class="subtot"><input type="text" value="" style="width:70px;"/></td>'; } </script> </head> <body bgcolor="#cccccc">             <p><img src="../assets/img/ticket_page/client.svg">   <input type="text" value=""/></p>                      <!-- content here -->                         <?php        $attributes = array('class' => 'form-horizontal', 'id' => '');       $options_employee = array('' => "select");       foreach ($employee $row)       {         $options_employee[$row['first_name']] = $row['first_name'];       }  echo form_open('admin/employee/add');  /*i open form here*/       ?>             <div id="form">               <!-- div form starts here.its add table  -->             <table id="test">             <thead>             <tr>             <td style="width:80px;"><img src="../assets/img/ticket_page/employee.svg"></td>             <td style="width:35px;"><img src="../assets/img/ticket_page/start_time.svg"></td>             <td style="width:35px;"><img src="../assets/img/ticket_page/id.svg"></td>             <td style="width:145px;"><img src="../assets/img/ticket_page/description.svg"></td>             <td style="width:45px;"><img src="../assets/img/ticket_page/type.svg"></td>             <td style="width:45px;">  <img src="../assets/img/ticket_page/qty_prch.svg"></td>             <td style="width:45px;">   <img src="..assets/img/ticket_page/qty_used.svg"></td>             <td style="width:70px;"> <img src="../assets/img/ticket_page/price.svg"></td>             <td style="width:70px;"> <img src="../assets/img/ticket_page/discount.svg"></td>             <td style="width:70px;"> <img src="../assets/img/ticket_page/tax.svg"></td>             <td style="width:70px;"> <img src="../assets/img/ticket_page/total_01.svg"></td>             </tr>             </thead>             <tbody>             <tr>             <td><?php echo form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');?></td>             <td><input type="text" name="start_time[]" value="" style="width:35px;"/></td>             <td><input type="text" name="pid[]"  value="" style="width:35px;"/></td>             <td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td>             <td><input type="text" name="type[]" class="type" style="width:45px;"/></td>             <td><input type="text" name="qty_prch[]" class="qty_prch" style="width:45px;"/></td>             <td><input type="text" name="qty_used[]" class="qty_used" style="width:45px;"/></td>             <td><input type="text" name="price[]" class="price" style="width:70px;"/></td>             <td><input type="text" name="discount[]" class="discount" style="width:70px;"/></td>             <td><input type="text" name="tax[]" class="tax" style="width:70px;"/></td>             <td class="subtot"><input type="text" name="total[]" class="total" style="width:70px;"/></td>              </tr>             </tbody>                </table>              <div id="add_row">         <button onclick="displayresult()" class="add_r"></button>                 </div>   <button id="btn" type="submit">save</button>/*this actual submit button*/             <button id="btn" type="reset">cancel</button>            <?php echo form_close(); ?>  /*here close form*/            </body> </html> 

can me? need submit form when click submit button. please me solve it.

give type attribute button,that'll keep them triggering submit action when exception occurs in event handler.

you're using html5 button element. remember reason it's button has default behavior submit type, stated in w3 specification.check this out.

<button type="button" onclick="displayresult()" class="add_r"></button> 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -