【问题标题】:PHP, Text files and ArraysPHP、文本文件和数组
【发布时间】:2018-11-27 16:05:43
【问题描述】:

我想将以下 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
}
?>

【问题讨论】:

  • 为什么要问他们是否两次使用 MATLAB?
  • insset 应该是isset
  • @Barmar 也许他们第二次的回答不同。
  • @Andreas 但是代码实际上并没有检查他们给出的答案,它只是测试是否设置了参数。
  • 开个玩笑。我的意思是“你用过 Matlab 吗?”、“你确定你用过 Matlab 吗?”、“真的吗?”。

标签: php html arrays checkbox


【解决方案1】:

首先,你的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
}
?>

在您认为合适的情况下进行更改。

【讨论】:

  • 你的 HTML 也不好。 &lt;form&gt; 不能包含在另一个 &lt;form&gt; 中。
  • 我不不,不是我的HTML,我不知道他想做什么。
  • 你的 foreach 循环只是 $text = implode(',', $_POST['tool'])
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2019-08-03
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多