2016-07-28 73 views
-2

有沒有什麼方法可以寫入用戶通過php腳本傳遞給文件的值。我在網上搜索了很多,找到用php編寫字符串到文件的例子,但是他們正在做的事情在場景中不起作用。實際上他們正在將php變量的值寫入文件,但在我的情況下,我想將用戶傳遞給使用php的文件的值寫入文件。也出現問題是,我想從HTML INPUT標記中獲取值,也可能將其轉換爲php字符串變量。 終於,有沒有任何方式來從HTML INPUT標記中獲取值並將其寫入文件?使用PHP腳本將HTML的輸入標籤的值寫入文件

+2

請發佈您的代碼。 –

+1

是的,這是一件非常容易的事情。但是可能有許多事情你做錯了,我們開始猜測是愚蠢的。請張貼你到目前爲止和我或其他人將糾正它 –

回答

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> 
相關問題