使用2个按钮调用2个不同的js功能

时间:2017-06-07 18:06:43

标签: javascript php jquery

我正在尝试在表单上使用2个按钮,一个用于保存已编辑的表单,另一个用于通过名为mailhandler.php的函数保存并通过电子邮件向用户发送包装消息。单击任一按钮时,它只会重置表单并且不会保存或发送电子邮件。有3个文件,index.php,webapp.js和data.php。 Data.php查询所有内容并且工作正常,问题似乎与我的JS有关。除了电子邮件之外,还有两个函数save()和email()是相同的,其中还包含emailFunction()函数。任何帮助将不胜感激。 index.php文件如下所示:

<button type="submit" id="btn1" onClick="save()">Save Report</button>
<button type="submit" id="btn2" onClick="email()">Save and E-Mail Report</button>    

和webapp.js是这样的:

function emailFunction() {
window.open("MailHandler.php");
}
$(document).ready(function(){
// On page load: datatable
var table_records = $('#table_records').dataTable({
"ajax": "data.php?job=get_records",
"columns": [
{ "data": "c_customer_number" },
{ "data": "c_customer_name",   "sClass": "c_customer_name" },
{ "data": "functions",      "sClass": "functions" }
],
"aoColumnDefs": [
  { "bSortable": false, "aTargets": [-1] }
],
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"oLanguage": {
  "oPaginate": {
    "sFirst":       " ",
    "sPrevious":    " ",
    "sNext":        " ",
    "sLast":        " ",
  },
  "sLengthMenu":    "Records per page: _MENU_",
  "sInfo":          "Total of _TOTAL_ records (showing _START_ to _END_)",
  "sInfoFiltered":  "(filtered from _MAX_ total records)"
}
  });
var form_record = $('#form_record');
form_record.validate();
// Show message
// Save button
$(document).on('click', '.function_edit a', function(e){
e.preventDefault();
// Get information from database
show_loading_message();
var id      = $(this).data('id');
var request = $.ajax({
  url:          'data.php?job=get_record',
  cache:        false,
  data:         'id=' + id,
  dataType:     'json',
  contentType:  'application/json; charset=utf-8',
  type:         'get'
});
request.done(function(output){
  if (output.result == 'success'){

    $('.lightbox_content h2').text('Edit Info');
    $('#form_record').attr('class', 'form edit');
    $('#form_record').attr('data-id', id);
    $('#form_record .field_container label.error').hide();
    $('#form_record 
.field_container').removeClass('valid').removeClass('error');
    $('#form_record #c_customer_name').val(output.data[0].c_customer_name);
    $('#form_record 
#c_customer_number').val(output.data[0].c_customer_number);
    hide_loading_message();
    show_lightbox();
  } else {
    hide_loading_message();
    show_message('Information request failed', 'error');
  }
});
request.fail(function(jqXHR, textStatus){
  hide_loading_message();
  show_message('Information request failed: ' + textStatus, 'error');
});
});
// Edit submit form
function save(){ 
$(document).on('ready', '#form_record', function(e){
e.preventDefault();
// Validate form
if (form_record.valid() == true){
  // Send information to database
  hide_ipad_keyboard();
  hide_lightbox();
  show_loading_message();
  var id        = $('#form_record').attr('data-id');
  var form_data = $('#form_record').serialize();
  var request   = $.ajax({
    url:          'data.php?job=edit_record&id=' + id,
    cache:        false,
    data:         form_data,
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'get'
  });
  request.done(function(output){

  if (output.result == 'success'){
    // Reload datatable
    table_records.api().ajax.reload(function(){
      hide_loading_message();
      var c_customer_name = $('#c_customer_number').val();
        show_message("Info '" + c_customer_name + " edited/added successfully.", 'success');
//          emailFunction();
      }, true);

    } else {
      hide_loading_message();
      show_message('Edit request failed', 'error');
    }

  });
  request.fail(function(jqXHR, textStatus){
    hide_loading_message();
    show_message('Edit request failed: ' + textStatus, 'error');
  });
}
});
}
// save/email submit form
function email(){ 
$(document).on('ready', '#form_record', function(e){
e.preventDefault();
// Validate form
if (form_record.valid() == true){
  // Send information to database
  hide_ipad_keyboard();
  hide_lightbox();
  show_loading_message();
  var id        = $('#form_record').attr('data-id');
  var form_data = $('#form_record').serialize();
  var request   = $.ajax({
    url:          'data.php?job=edit_record&id=' + id,
    cache:        false,
    data:         form_data,
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'get'
  });
  request.done(function(output){

    if (output.result == 'success'){
      // Reload datatable
      table_records.api().ajax.reload(function(){
        hide_loading_message();
        var c_customer_name = $('#c_customer_number').val();
        show_message("Info '" + c_customer_name + " edited/added successfully.", 'success');
        emailFunction();
      }, true);

    } else {
      hide_loading_message();
      show_message('Edit request failed', 'error');
    }
});
  request.fail(function(jqXHR, textStatus){
    hide_loading_message();
    show_message('Edit request failed: ' + textStatus, 'error');
  });
}
});
}
});

0 个答案:

没有答案