2012-10-16 39 views
0

這個問題會讓你想:D。我有一個HTML輸入表單,因爲它似乎有太多的功能。一個功能就是這樣,所以當輸入按下它提交數據。其他功能是檢查是否輸入了某個關鍵字,以便將其重定向到其他URL。它完全適用於堆棧溢出的人。這裏是我的代碼:HTML輸入表單 - 添加Javascript和PHP函數

<script type="text/javascript"> 
<!-- 
function enter(e){ 
    if(e.keyCode == 13) 
    { 
     Login(); 
     return false; 
    } 
} 
//--> 
</script> 


    <script type="text/JavaScript"> 
    <!-- 
    function Login(){ 
    var keyword=document.getElementById("address").value; 
    var done=0; 
    keyword=keyword.toLowerCase(); 
    keyword=keyword.split(' ').join(''); 
    if (keyword=="example,example") { window.location="http://www.example.com"; done=1;} 
    if (keyword=="example1") { window.location="http://www.example1.com"; done=1;} 
    if (keyword=="example2") { window.location="http://www.example2"; done=1;} 
    if (done==0) { doSomethingElse(); } 
    } 
    //--> 
    </script> 

    <form name="enterkeyword" action="none"> 
     <input name="keyword" id="address" type="text" onkeypress="return enter(event);"/> 
     <div class="buttons"> 
     <button type="button" onclick="Login()">Submit</button> 
     </div> 
     </form> 

這裏是問題所在。我需要找到一種方法將每個提交的查詢保存爲excel或.CSV。我發現了一種使用PHP將輸入數據保存到.CSV文件的方法。我測試了它,它工作。但是當我嘗試用我的輸入表單上的現有函數實現它時,它會弄亂一切。下面是從「查詢節電功能」的代碼,這是HTML部分:

<html> 
<head> 
    <title>My Form</title> 
</head> 

<body> 
    <form action="save.php" method="post"> 
     <p> 
      What is your favorite movie?<br> 
      <input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" /> 
     </p> 
     <p> 
      What is your name?<br> 
      <input type="text" name="formName" maxlength="50" value="<?=$varName;?>" /> 
     </p>     
     <input type="submit" name="formSubmit" value="Submit" /> 
    </form> 
</body> 
</html> 

這是PHP部分(save.php):

<?php 
if($_POST['formSubmit'] == "Submit") 
{ 
    $errorMessage = ""; 

    if(empty($_POST['formMovie'])) 
    { 
     $errorMessage .= "<li>You forgot to enter a movie!</li>"; 
    } 
    if(empty($_POST['formName'])) 
    { 
     $errorMessage .= "<li>You forgot to enter a name!</li>"; 
    } 

    $varMovie = $_POST['formMovie']; 
    $varName = $_POST['formName']; 

    if(empty($errorMessage)) 
    { 
     $fs = fopen("mydata.csv","a"); 
     fwrite($fs,$varName . ", " . $varMovie . "\n"); 
     fclose($fs); 

     exit; 
    } 
} 
?> 

    <?php 
     if(!empty($errorMessage)) 
     { 
      echo("<p>There was an error with your form:</p>\n"); 
      echo("<ul>" . $errorMessage . "</ul>\n"); 
     } 
    ?> 

兩個代碼工作的偉大,但我只是不知道如何實現它們,所以他們可以工作togeather相同的形式... BTW這個PHP函數有2個輸入字段,我只需要1,我真的不需要這個錯誤信息,如果字段留空...我知道它的複雜,但可以有人比我更瞭解這一點嗎?

+0

你需要將'var keyword'保存爲'* .csv',然後將用戶重定向到不同的網站/頁面? – sed

+0

我需要將每個輸入保存到.CSV,並且只有在輸入了某個關鍵字時才重定向,如果不是這個函數被調用:doSomethingElse(); – Dreadlord

回答

1

完成更新

嘿,我再次分隔的功能和使代碼可讀性和可維護性。

的Index.html

<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" href="styles/index.css"/> 
    </head> 
    <body> 
     <input type="text" id="txt" value="example1"/><br/> 
     <input type="button" id="btn" value="doQuery()"/> 

     <script src="scripts/index.js"></script> 
    </body> 
</html> 

Index.js

function processValue() { 

    var inputText = document.getElementById("txt").value.toLowerCase().split(' ').join(''); 

    // Export query 
    exportQuery(inputText); 
} 

function exportQuery(query) { 

    var script = document.createElement("SCRIPT"); 

    script.setAttribute('type', 'text/javascript'); 
    script.setAttribute('src', 'exportQuery.php?query=' + query); 

    document.head.appendChild(script); 
} 

var urlMapper = { 

    "example,example": "http://www.example.com", 
    "example1": "http://www.example1.com", 
    "example2": "http://www.example2.com" 
} 

/** 
* Should be called when the PHP has finished execution 
*/ 
function onPhpReady(query) { 

    var url = urlMapper[ query ]; 

    if(url !== undefined) { 

     goToUrl(url); 

    } else { 

     codeAddress(); 
    } 
} 

function goToUrl(url) { 

    window.location = url; 
} 

function codeAddress() { 

    alert("Access Denied"); 
} 

(function() { 

    // Selfexecuting code. 

    function addButtonClickListener() { 

     var btn = document.getElementById("btn"); 

     btn.addEventListener("click", processValue, false); 
    } 

    function processKeyPress(e) { 

     if(e.keyCode == 13) { 

      processValue(); 
     } 
    } 

    function addInputKeyPressListener() { 

     var input = document.getElementById("txt"); 

     input.addEventListener("keypress", processKeyPress, false); 
    } 

    addButtonClickListener(); 
    addInputKeyPressListener(); 

}()); 

exportQuery.php

<?php 
    // Get the request 
    $query = $_GET['query']; 

    if(!empty($query)) 
    { 
     writeToCSV($query); 
     notifyReady($query); 
    } 

    function writeToCSV($query) { 

     $fs = fopen("storedQueries.csv", "a"); 

     fwrite($fs, $query . "\n"); 

     fclose($fs); 
    } 

    function notifyReady($query) { 

     // print back as plain javascript 
     print <<<ENDLINE 
     onPhpReady('$query') 
ENDLINE; 
     // The ENDLINE^can't use spaces or it will give a parse error. 
    } 
?> 

來源:DaniWeb

請不就是我的目標Webkit,所以爲了兼容性,你必須改變事件的附加方式等。

+0

我更新了它。它應該適合您的需求。 – EricG

+0

我檢查了所有瀏覽器和fkors關鍵字重定向在除IE以外的其他所有平臺中都正常工作 – Dreadlord

+0

不可靠的流程可能會導致PHP + goToUrl在URL甚至在Login中設置之前執行。它現在應該工作。 – EricG