PHP、文本文件和数组

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (30)

我有下面的HTML代码,我想写到PHP中。(在这个例子的末尾,我有到目前为止所得到的。)

<form action="action.php" method="post" />
 Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>

If yes, please indicate which of these currently purchased toolboxes you use $
form action="action.php" method="post" />
    Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="rad" value="yes" checked> Yes<br>
<input type="radio" name="rad" value="no"> No<br>

If yes, please indicate which of these currently purchased toolboxes you use $

<input type="checkbox" name="tool" value="Control">Control Systems Toolbox<br>
<input type="checkbox" name="tool" value="Image">Image Processing Toolbox<br>
<input type="checkbox" name="tool" value="Optimiz">Optimization Toolbox<br>
<input type="checkbox" name="tool" value="Robust">Robust Control Toolbox<br>
<input type="checkbox" name="tool" value="Signal">Signal Processing   Toolbox<br>
 Please enter a comma separated list of toolboxes you would like to use for you$
<input type="text" name="textquestion" value=""><br>
<input type="submit" value="Submit">

我正在寻找一种将复选框放入数组中的方法,以便用户可以选中多个框,并且这个答案也与另一个答案一起存储在文本文件中。

<?php
$file = "data.txt";
if(insset($_POST['tool'])){
$tool= $_POST['tool'];
    foreach($tool as $tol=>$value){
    }
} 
 if (isset($_POST['rad']) && value  && ($_POST['question'])) { 
   $fh = fopen($file, 'w+'); 
 $text = $_POST['rad']. ' ' .$value. ' ' .$_POST['question']; 
fwrite($fh,$text); // Write form data to the file
fclose($fh); // Close the file
}
?>
提问于
用户回答回答于

首先,你的HTML代码不好,应该是:

<form action="action.php" method="post" />
 Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>

If yes, please indicate which of these currently purchased toolboxes you use $
    Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="rad" value="yes" checked> Yes<br>
<input type="radio" name="rad" value="no"> No<br>

If yes, please indicate which of these currently purchased toolboxes you use $

<input type="checkbox" name="tool[]" value="Control">Control Systems Toolbox<br>
<input type="checkbox" name="tool[]" value="Image">Image Processing Toolbox<br>
<input type="checkbox" name="tool[]" value="Optimiz">Optimization Toolbox<br>
<input type="checkbox" name="tool[]" value="Robust">Robust Control Toolbox<br>
<input type="checkbox" name="tool[]" value="Signal">Signal Processing   Toolbox<br>
 Please enter a comma separated list of toolboxes you would like to use for you$
<input type="text" name="textquestion" value=""><br>
<input type="submit" value="Submit">

因此,你的复选框都将被考虑在内。

然后,要创建一个新数组,可以使用:$xxx = array();。因此,在您的php文件中,编写:

<?php
$file = "data.txt";
if (!empty($_POST['tool'])) {
    $text = implode(',', $_POST['tool']);
}
if (condition) { 
    $fh = fopen($file, 'w+');  
    fwrite($fh,$text); // Write form data to the file
    fclose($fh); // Close the file
}
?>

扫码关注云+社区

领取腾讯云代金券