2012-02-24 140 views
0

我試圖從HTML Web表單(通過輸入文本字段)獲取並傳輸信息,然後將該信息存儲到PHP變量中的另一個文件中。正如你在下面看到的,我創建了一個字符串來搜索($ str_to_find)並手動使用'腳本'作爲字符串,但我想要做的是用另一個文件中的Web表單動態地填充這個區域。在PHP中動態存儲字符串

我知道這可能是一個普遍的問題,但我不知道從哪裏開始。

function check_files($this_file) { 
$str_to_find='script'; // the string(code/text) to search for 
//I want to fill the 'string' above with info from another file's web form, if possible. 
+0

你可能會想使用類似的file_get_contents' ()'(這裏是該文檔的頁面:http://php.net/manual/en/function.file-get-contents.php)中間有一些例子頁面,應該可以幫助你開始......至少就文件部分而言! – summea 2012-02-24 05:43:41

+0

好的....這是有道理的。我想我的問題是,如果來自Web表單內的文本字段,我將如何使用這些代碼來完成該操作?我認爲我很困惑將它寫入程序語言 – 2012-02-24 05:46:51

+0

你的方法或使用數據庫是否更復雜,但是可以使用簡單的[SQLite數據庫](http:// www。 php.net/manual/en/intro.sqlite.php)?如果您的PHP 5或更高版本,默認情況下啓用SQLite,並且您可以讀取和寫入存儲在磁盤上的單個文件中的sqlite數據庫中的值。 *也許你可以澄清你的表單和數據存儲/檢索的最終目標嗎?* – drew010 2012-02-24 05:48:47

回答

1

這聽起來像你只是想使用GET或POST數據。這裏是基本的樣本。一個人將在form.html上填寫表格並點擊提交。然後您將通過屬性名稱從此表單收集POST數據。在這種情況下,process.php腳本僅打印出「Hello <firstname> <lastname>」,但也可以根據需要顯示它。

form.html

<form action="process.php" method="post"> 
    <input name="fname" type="text" /> 
    <input name="lname" type="text" /> 
    <input type="submit" /> 
</form> 

process.php

$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
echo "Hello $fname $lname" 
... 

如果你想這顯示在同一頁面上的信息,您可以使用AJAX。示例請參閱http://api.jquery.com/jQuery.ajax/。我已經包含了一個如下:

form.html

... 
<head> 
... 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 
    // when document is ready do this 
    $(document).ready(function() { 

     // listen to when users click on the send button 
     $('#send-ajax').click(function() { 

      // get input data 
      $fname = $('#fname').val(); 
      $lname = $('#lname').val(); 

      // result container 
      $result = $('#result-ajax'); 

      // create ajax request to process and store 
      // result in the div container above the form 
      $.ajax({ 
       url: 'process.php', 
       type: 'POST', 
       dataType: 'HTML', 
       data: { 
        fname: $fname, 
        lname: $lname 
       }, 
       success: function($html) { 
        $result.html($html); 
       }, 
       error: function() { 
        $result.html('<b>Request Failed</b>'); 
       } 
      }); 
     }); 
    }); 
</script> 
</head> 
<body> 
    <div id="result-ajax"></div> 
    <input id="fname"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ /> 
    <input id="lname" /> 
    <button id="send-ajax" value="send">Send</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
</body> 
... 

process.php

同上

+0

謝謝Prowla,你的答案做到了。非常實用的謝謝你。如果您仍在論壇中,您是否知道如何將數據發佈到iframe中的process.php中?我運行腳本,並在新的瀏覽器窗口中打開process.php。我只想將結果顯示在同一頁面上的iframe內部。那是我需要使用AJAX的地方嗎? – 2012-02-24 17:29:28

+0

我不會理解爲什麼但不使用iframe。我會更新我的答案以包含AJAX請求 – Kurt 2012-02-24 22:02:59