2016-07-28 85 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>