Foreach 2 变量 $_POST

我有这个表格:


<form method='post'>

  <input name='i' type='hidden' value='' >

  <input name='email' type='text' >

  <input name='pass' type='password' >

  <input type="submit" >

</form>

以及在 .txt 文件中打印数据的 PHP 代码:


<?php

  $handle = fopen("data.txt", "a");

  foreach($_POST as $variable => $value) {

    fwrite($handle, $variable);

    fwrite($handle, "=");

    fwrite($handle, $value);

    fwrite($handle, "\r\n");

  }

  fwrite($handle, "\r\n");

  fclose($handle);

  exit;

?>

结果是:


i = 值

email = 值

pass = 值


我不需要打印$_POST['i']值。


任何的想法?


largeQ
浏览 61回答 1
1回答

qq_笑_17

添加一个 IF 测试然后忽略循环的迭代$variable使用icontinue;您还可以使用一个为每一行写入所有数据fwrite<?php$handle = fopen("data.txt", "a");foreach($_POST as $variable => $value) {&nbsp; &nbsp; if ( $variable == 'i' ){&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp;// will stop this itereation and continue with the next&nbsp; &nbsp; }&nbsp; &nbsp; // make one write for all 4 bits of data.&nbsp; &nbsp; fwrite($handle, "$variable = $value \r\n");}fwrite($handle, "\r\n");fclose($handle);
打开App,查看更多内容
随时随地看视频慕课网APP