是否可以在我的表单提交按钮上使用onClick功能?

问题描述:

I store multiple data in a table for enquiry basis.. which is successfully stored in my table. but now i also want to store those same data in Booking basis.

So i used onClick function for storing enquiry data. and now i want to store booking data in table from form submit button.

here is my form submit button code:

<button type="submit" class="booking-btn" onclick="btn_booking_hour();" id="btn_booking_hour">Book Now</button>

Here is my form action:

<?php echo form_open('booking/'.$product->id); ?>

And here my function:

function btn_booking_hour()
    { 

    var date = $('#datehour').val();
    var renter_id = $("#ownerid").val();
    var guests_quantity = $('#no_of_guests').val();
    var property_id = $('#property_id').val(); 
    var selectedIds="";
    var selectedObject = document.getElementsByClassName('hour_slots_available slot_activated');
    for(var i=0;i<selectedObject.length;i++)
      selectedIds+=selectedObject[i].id+",";

    if(selectedIds == ""){

        alert("Please select Hour-Slot(s) before Booking");
    }
    else{

        $.ajax({

    type: 'POST',
    dataType:'json',
    url:baseURL+'site/rentals/ajaxhourshow',

    data:{'selectedHours':selectedIds,'property_id':property_id,'date':date,'guests_quantity':guests_quantity,'renter_id':renter_id},

    success: function(responce){

        var myJSON = JSON.stringify(responce);
        var packet = $.parseJSON(myJSON);

        var  prd_id=packet.property_id;
        var hour_slots=packet.hour_slots;
        window.location.href = baseURL+'booking/'+prd_id;
        }
    });
   }

}

Please suggest me how can i submit a form with data within their onClick function. Thanks..

我将多个数据存储在一个表中以供查询基础..它成功地存储在我的表中。 但现在我也想在预订的基础上存储这些相同的数据。 p>

所以我使用onClick函数存储查询数据。 现在我想从表单提交按钮将预订数据存储在表格中。 p>

这是我的表单提交按钮代码: p>

 &lt;  button type =“submit”class =“booking-btn”onclick =“btn_booking_hour();”  id =“btn_booking_hour”&gt;立即预订&lt; / button&gt; 
  code>  pre> 
 
 

以下是我的表单操作: p>

   &lt;?php echo form_open('booking /'.$ product-&gt; id);  ?&gt; 
  code>  pre> 
 
 

这里是我的函数: p>

  function btn_booking_hour()
 {
 
  var date = $('#datehour')。val(); 
 var renter_id = $(“#ownerid”)。val(); 
 var guests_quantity = $('#no_of_guests')。val(); \  n var property_id = $('#property_id')。val();  
 var selectedIds =“”; 
 var selectedObject = document.getElementsByClassName('hour_slots_available slot_activated'); 
 for(var i = 0; i&lt; selectedObject.length; i ++)
 selectedIds + = selectedObject [i] .id +  “,”; 
 
 if if(selectedIds ==“”){
 
 alert(“请在预订前选择Hour-Slot”); 
} 
 else {
 
 $。  ajax({
 
 type:'POST',
 dataType:'json',
 url:baseURL +'site / rentals / ajaxhourshow',
 
 data:{'selectedHours':selectedIds,'property_id'  :property_id,'date':date,'guests_quantity':guests_quantity,'renter_id':renter_id},
 
成功:函数(responce){
 
 var myJSON = JSON.stringify(responce); 
 var  packet = $ .parseJSON(myJSON); 
 
 var prd_id = packet.property_id; 
 var hour_slots = packet.hour_slots; 
 window.location.href = baseURL +'booking /'+ prd_id; 
} 
  }}; 
} 
 
} 
  code>  pre> 
 
 

请建议我如何在onClick函数中提交包含数据的表单。 谢谢.. p> div>

Not sure if you are using a form, if you are using it you can can use onsubmit handler and return the action. In that case you wont need onclick handler in the button

<form onsubmit = "return btn_booking_hour()">
  // Rest of code
</form>

You can also do like this

$('#form').on('submit',function(e){
 e.preventDefault();
 // rest of the code
})

you can use this code

 function btn_booking_hour(){
// your other code
 document.getElementById('formid').submit();
// formid = form id
 }

or also use jquery

function btn_booking_hour(){
// your other code
 $('#formid').submit();
// formid = form id
 }

i got your point so you can also use it

$(function() {
//hang on event of form with id=myform
$("#myform").submit(function(e) {

    //prevent Default functionality
    e.preventDefault();

    rest of your code

});

});