开发者

Help related to reading values from a form and writing it to a destination file using PHP

Presently working on extracting values out of a form and writing it to a file.

My form has two text fields and a dropdown. The dropdown gets its el开发者_StackOverflow中文版ements from a text file.

A destination file needs to be created everytime a user edits the form.

The form looks like:

<form method="get" action="index.php" name="">
<input type="text" name="val1" size="" value=""> &nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="val2" size="" value=""> &nbsp;&nbsp;&nbsp;&nbsp;
<?php
  $spfile = file("path_of_the_text_file_elements_are_extracted_from");
  echo "<select name='sp' value=''>";
  foreach($spfile as $value) {
    echo "<option value='spval'>$value</option>";
  }
  echo "</select>"; 
?> &nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submit" value="Submit" />

Below is the code I was trying to use to create the file when the user edits the form:

<?php   

{
 $dest = "destination_file.txt";
 $file = fopen($dest,'w');

 fwrite($file,"Value1:");
 //$value1=$_GET['val1'];
 //fputs($file,$Value1);
 fwrite($file,"\n");
 fwrite($file,"Value2:");
 fwrite($file,"\n");
 fwrite($file,"Value3:");
 fclose($file);

}

Having problem at:

  1. Reading the values of the edited form and writing the values in the destination file.
  2. Not sure how to use $_GET for the dropdown to read the selected value off of the dropdown list, and to write it to the destination file.

The destination file should look like:

Value1:(value read from the form) Value2:(Value read from the form) Value3:(Value read from the form)


You need to first gain $_GET values to use them.

Like

$val1 = htmlspecialchars(trim($_GET['val1'])); // with trim we prevent line changes
$val2 = htmlspecialchars(trim($_GET['val2']));
$val3 = htmlspecialchars(trim($_GET['val3']));

And maybe you could all file writing in one line:

$content="Value1:".$val1." Value2:".$val2." Value3:".$val3."";

and then use following code

$dest = "destination_file.txt";
$fop  = fopen($dest,'w');
fwrite($fop, $content);
fclose($fop);

To combine all of those:

$val1    = htmlspecialchars(trim($_GET['val1']));
$val2    = htmlspecialchars(trim($_GET['val2']));
$val3    = htmlspecialchars(trim($_GET['val3']));
$content = "Value1:".$val1." Value2:".$val2." Value3:".$val3."";
$dest    = "destination_file.txt";
$fop     = fopen($dest,'w');
fwrite($fop, $content);
fclose($fop);

Please make sure that the directory where you are writing to is writable (Chmod 777).

I´d not recommend to use that code in real sites, since you should check GET security first (example htmlspecialchars())

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜