如何使用PHP将输入表单和激活函数记录到文本文件中


How to log to a Text file an input form and activate function using PHP

我想将用户输入表单记录到服务器上的文本文件中,onclick按钮触发一个函数,从另一个服务器得到状态结果,这很好地工作。我怎么能让一个按钮触发两个功能正确。如果我使用提交类型按钮返回函数不工作。我如何修复这些工作一起在一个点击?

html:

<form action="" method="POST"><input class="input" type="text" name="search" id="search" value="" size="13" height="20" width="150" maxlength="13" id="ItemCodeNoPopup" dir="ltr" autocomplete="on">
<input type="button" value="חפש" name="button" align="absmiddle" border="0" alt="submit" onclick="showDiv(); logger();"></form>   

post.js:

document.getElementById('quickContactForm')
$(document).ready(function(){
//$("#hiddenDiv").hide();
});
function showDiv(){
  var str=$("#search").val();
  var url="action.php?show="+str;
  console.log('url'+url);
  $.get(url,function(data,status){
  //   alert("Data: " + data + "'nStatus: " + status);
      $("#hiddenDiv").html(data);
      $("#hiddenDiv").show();
  });
    function logger(){
       var str=$("#search").val();
       currentTime = new Date().getTime();
       console.log(currentTime);
       data = [currentTime,str];
       $.post('logger.php',function(data){
       });
    }

action.php运行良好。

logger.php

<?php
 $Date = date('Y-m-d H:i:s');
 var str=$("#search").val();
 var ip=$_SERVER['REMOTE_ADDR'];
 $file = fopen("log.txt","a+");
  fwrite($file,$str,$ip);
  fwrite($file,$Date. "'n");
  fclose($file); 
  print_r(error_get_last());
}
?>

如果logger.php不依赖于action.php的结果,我建议在action.php中调用logger.php。因此,您不需要向服务器发出两个http请求。只有一个是必要的。

Action.php:

require_once('logger.php') 

如果第一个函数成功,可以对第二个函数执行回调。

document.getElementById('quickContactForm')
$(document).ready(function(){
 //$("#hiddenDiv").hide();
});
    function showDiv(){
     var str=$("#search").val();
  var url="action.php?show="+str;
  console.log('url'+url);
  $.get(url,function(data,status){
  //   alert("Data: " + data + "'nStatus: " + status);
      $("#hiddenDiv").html(data);
      $("#hiddenDiv").show();
       //callback to logger once the first function is completed
       logger();
  });
function logger(){
   var str=$("#search").val();
   currentTime = new Date().getTime();
   console.log(currentTime);
   data = [currentTime,str];
   $.post('logger.php',function(data){
   });
}

你可以从onclick中删除logger来检查