2015-06-07 37 views
1

我想將用戶輸入窗體記錄到服務器上的文本文件中, 以及onclick按鈕會觸發一個函數給出來自另一個服務器的狀態結果,效果很好。我怎麼能讓一個按鈕觸發2個功能。如果我使用提交類型按鈕返回功能不起作用。 如何解決這些問題以單擊一起工作?如何登錄到文本文件的輸入窗體並使用PHP激活功能

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

你就不能調用從'showDiv()'函數JS的記錄功能? –

+0

我最終只使用showDiv()並從Action.php調用logger.php。成功了!謝謝 – eyalix

回答

0

如果logger.php不依賴於action.php的結果,我建議在action.php的調用logger.php。所以你不需要向服務器做兩個http reqeusts。只有一個是必要的。

action.php的:

require_once('logger.php') 
0

可以執行回調至第二個功能,如果第一個功能是成功的。

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記錄儀檢查