有沒有什麼方法可以寫入用戶通過php腳本傳遞給文件的值。我在網上搜索了很多,找到用php編寫字符串到文件的例子,但是他們正在做的事情在場景中不起作用。實際上他們正在將php變量的值寫入文件,但在我的情況下,我想將用戶傳遞給使用php的文件的值寫入文件。也出現問題是,我想從HTML INPUT標記中獲取值,也可能將其轉換爲php字符串變量。 終於,有沒有任何方式來從HTML INPUT標記中獲取值並將其寫入文件?使用PHP腳本將HTML的輸入標籤的值寫入文件
-2
A
回答
1
我不能相信你「在網上搜索了很多東西」。
首先,將輸入值存儲在變量中,然後對其進行驗證,之後可以將變量數據寫入文件中。
因此,存儲在一個變量的值:(請確保通過HTTP-POST發送用戶數據 - <form method="post">
)
$data = $_POST['myFormValue'];
驗證
// do validation logic here with $data
,然後保存在數據文件
file_put_contents('/path/to/file/location.txt', $data);
而那就是它! 在PHP中,有很多不同的方式來編寫和修改文件。有關此主題的更多信息,請拿上PHP Dokumentation 看看希望它可以幫助
+1
雖然我無條件同意你的第一句話,我仍然不同意使用'GET'。黃金法則:'POST'是**發送**數據,'GET'是...獲取數據。 –
+0
當然你是對的。我編輯了我的答案。謝謝你的提示:) –
1
這真的出現,你可能要上晚自習PHP實際上是如何通過閱讀documentation工作。
下面是一些基本的代碼反正只是踢
<?php
$message = '';
//php parses the user input for you and populates $_POST, $_GET, and other superglobals
//in this case, if the $_POST[ 'string' ] exists, then you submitted
//the form, so we have something to store,
if(isset($_POST[ 'string' ])){
$fh = file_put_contents('file.txt', $_POST[ 'string' ], FILE_APPEND);
$string = htmlspecialchars($_POST[ 'string' ]);
$message = "<p>The string '$string' was stored.</p>";
}
?>
<html>
<head><title>test</title></head>
<body>
<?php echo $message ?>
<form method="post">
<input type="text" name="string" />
<input type="submit" value="store" />
</form>
</body>
</html>
0
HTML文件
<html>
<head>
<style>
body{
background-color:red;
text-align:center
}
h1{
Text-align:center;
text-color:red;
background-color:lightblue;
}
form{
text-color:white;
position:fixed;
background-color:blue;
width:50%;
Left:25%;
}
table{
text-align:center;
background-color:White;
width:100%
}
</style>
</head>
<body>
<h1>Womacks Flatworks</h1>
<hr />
<h2>What type of help would you like me to give you?<h2>
<form action="addition.php" method="post">
<table>
<tr><Td>Full Name</td><td><Input type=text name=name autofocus/></td></tr>
<tr><Td>Email</td><td><Input type=Email name=email /></td></tr>
<tr><Td>phone</td><td><input type="tel" name="tel"></td></tr>
<tr><Td>address for Job</td><td><Input type=text name=Address /></td></tr>
<tr><Td>Date for the Job</td><td><Input type=date name=start /></td></tr>
<tr><Td>time</td><td><Input type=Time name=time /></td></tr>
<tr><Td>Message</td><td>
<textarea name="message" rows="10" cols="30">describe job</textarea>
</td></tr>
<tr><Td><input type="submit"></td><td><input type="reset"></td></tr>
</table>
</form>
</body>
</html>
addition.php
<?php
// this section calls up the xml file
$fname = 'flatworks.xml';
if (file_exists($fname)) {
$xml = simplexml_load_file($fname);
//this line skips your root for you
$root = $xml->addChild("cd");
//this builds your record
$root->addChild("name",$_POST['name']);
$root->addChild("email",$_POST['email']);
$root->addChild("tel",$_POST['tel']);
$root->addChild("Address",$_POST['Address']);
$root->addChild("Price",'');
$root->addChild("start",$_POST['start']);
$root->addChild("time",$_POST['time']);
$root->addChild("message",$_POST['message']);
// this saves your file in good xml format
$xml->asxml($fname);}
else {
echo $fname.' does not exist.';
}
?>
flatworks.xml
<?xml version="1.0"?>
<catalog>
<cd>
<name>Mickey Mouse</name>
<email>[email protected]</email>
<tel>123456789</tel>
<Address>1230 sesame st.</Address>
<Price/>
<start/>
<time/>
<message>describe job</message>
</cd>
<cd>
<name>minnie mouse</name><
<email>[email protected]</email>
<tel>987654321</tel>
<Address>1254 sesame st.</Address>
<Price/>
<start>2016-07-27</start>
<time>13:00</time>
<message>describe job</message>
</cd>
<cd>
<name>Pluto</name>
<email>[email protected]</email>
<tel>987654321</tel>
<Address>1234 sesame st.</Address>
<Price/>
<start>2016-07-29</start>
<time>13:00</time>
<message>describe job</message>
</cd>
</catalog>
相關問題
- 1. 使用PHP保存包含輸入標籤值的HTML文件
- 2. 將html標籤寫入python中的文本文件
- 3. 使用bash腳本將多個輸入文字寫入文本
- 4. 如何將python腳本的輸出寫入html文件
- 5. 使用PHP寫入HTML表單的文本文件使用PHP
- 6. 無法讓PHP腳本將用戶輸入寫入文本文件
- 7. 將php腳本寫入文件
- 8. PHP MySQL輸入HTML標籤
- 9. PHP寫入文本文件從輸入
- 10. 如何使用php將html標籤寫入另一個html文件?
- 11. 如何將html輸入標籤的值傳遞給php代碼?
- 12. 有人可以解釋如何重定向url後,將html輸入值寫入.txt文件的php腳本
- 13. 輸入標籤文件值的jQuery
- 14. 將標準輸入寫入文件
- 15. 使用shell腳本寫入程序的標準輸入
- 16. 使用php將id3v2標籤寫入mp3文件?
- 17. 標籤內的PHP html輸入
- 18. 我想寫入輸入的插入位置上的文本文件,使用PHP
- 19. 如何在html文本輸入標籤中使用上標?
- 20. 通過輸入標籤的值在HTML
- 21. Python使用標準輸出和文件輸入寫入文件
- 22. 如何使用python將多個輸入寫入文本文件?
- 23. 不同的標籤在PHP/HTML輸入文件類型
- 24. 將值插入到php文件中的輸入標記
- 25. 將Html文本讀入標籤
- 26. PHP變量和HTML文本輸入值
- 27. 將腳本的輸出寫入配置單元中的文件
- 28. 將文本用戶輸入寫入excel
- 29. 使用php將表單輸入寫入.txt文件?
- 30. ASP.NET C#將文本框中的值寫入標籤:beginner simple
請發佈您的代碼。 –
是的,這是一件非常容易的事情。但是可能有許多事情你做錯了,我們開始猜測是愚蠢的。請張貼你到目前爲止和我或其他人將糾正它 –