如何使用 PHP 将输入表单记录到文本文件并激活功能

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

我想将用户输入表单记录到服务器上的文本文件中, onclick 按钮也会触发一个函数,该函数会提供来自另一台服务器的状态结果,效果很好。我怎样才能让一个按钮正确触发2个功能。如果我使用提交类型按钮,return 功能将不起作用。 我如何修复它们以便一键协同工作?

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());
}
?>

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

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.php 不依赖于 action.php 的结果,我建议在 action.php 中调用 logger.php。所以你不需要向服务器发出两个 http 请求。只有一个是必要的。

Action.php:

require_once('logger.php')