2016-07-28 13 views
-2

を使ってファイルに書き込むには、PHPスクリプトを使用してユーザーがファイルに渡す値を書く方法があります。私はPHPを使用してファイルに文字列を書くための例を見つけるためにウェブ上で多くを検索しましたが、彼らがやっていたものはシナリオでは動作していませんでした。実際には、PHP変数の値をファイルに書き込んでいましたが、私の場合、PHPが使用するファイルにユーザーが渡した値を書きたいと思っています。また問題は、HTML INPUTタグから値を取得したいと思っていて、おそらくCONVERTをPHPの文字列変数に変換することです。 最後に、 HTML INPUTタグから値を取得してファイルに書き込む方法はありますか?HTMLの入力タグの値をPHPスクリプト

+2

コードを記入してください。 –

+1

はい、これは非常に簡単なことです。しかし、あなたが間違っていることがいくつでもある可能性があります。推測を開始するのは私たちがばかげているでしょう。あなたがこれまでに持っていたものを投稿してください。誰かがそれを訂正します。 –

答えて

1

私はあなたが「ウェブの周りをたくさん検索した」と信じることはできません。

まず、入力値を変数に格納して検証し、その後に変数データをファイルに書き込むことができます。

ので、変数に値を格納します -

$data = $_POST['myFormValue']; 

検証

// do validation logic here with $data 

をし、その後にデータを保存する(<form method="post">をHTTP-POSTを介してユーザデータを送信することを確認してください)ファイル

file_put_contents('/path/to/file/location.txt', $data); 

それはそれです! PHPでは、ファイルの書き込みや変更にはさまざまな方法があります。このトピックの詳細については、PHP Dokumentationの をご覧ください。

+1

最初の文章に無条件に同意しますが、私はまだ 'GET'を使用することに同意しません。ゴールデンルール: 'POST'は**データを送信すること、' GET'はデータを取得することです。 –

+0

あなたはもちろんです。私は私の答えを編集しました。ヒントありがとうございました:) –

1

documentationを読むと、PHPの実際の動作を調べることができます。ここで

はちょうどキック

<?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> 
ために、とにかくいくつかの基本的なコードです
関連する問題