首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >文本文件和数组

文本文件和数组
EN

Stack Overflow用户
提问于 2018-06-19 02:49:31
回答 1查看 56关注 0票数 -1

我有以下HTML代码,我想写成PHP。(在此示例的末尾,我得到了到目前为止所拥有的内容。)

代码语言:javascript
复制
<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">

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

代码语言:javascript
复制
<?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
}
?>
EN

回答 1

Stack Overflow用户

发布于 2018-06-19 03:02:55

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

代码语言:javascript
复制
<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文件中,编写:

代码语言:javascript
复制
<?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
}
?>

在你改变你认为合适的方式之后。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50915752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档