1

我有一个 html 表单并使用 get 方法

如果用户选择鞋子选项值,我想将数据输入到 shoes_sales.txt,并将其余所有输入到衣服_sales.txt。不幸的是,这个数据没有出现在我的.txt.

这是html表单:

 <li class="form-line" id="id_16">
   <label class="form-label-left" id="label_16" for="input_16"> Select choice </label>
   <div id="cid_16" class="form-input">
     <select class="form-dropdown" style="width:150px" id="input_16" name="q16_selectFrom16">
       <option value="Shoes"> Shoes </option>
       <option value="Clothes"> Clothes </option>

     </select>
   </div>
 </li>

这是试图检索表单值的 php:

<?php
  header("Location: thankforsumbitting.html");

  if ($_GET['variable1'] == "shoes") {
    $handle = fopen("shoes_sales.txt", "a");
  }
  else {
    $handle = fopen("clothes_sales.txt", "a");
  }
  foreach($_GET as $variable => $value) {
    fwrite($handle, $variable."=".$value."\r\n");
  }
  fclose($handle);
  exit;
?> 
4

3 回答 3

2

变量中的值与当前的属性$_GET不对应:name

$_GET['variable1']

应该

$_GET['q16_selectFrom16']

您还在检查== "shoes"and == "clothes",而您options 中的值使用大写字母。

于 2013-08-22T18:03:23.713 回答
1

在您使用的代码的开头

header("Location: thankforsumbitting.html");

将其放在文件末尾,以便在重定向之前执行每一行代码。

改变这个:

if ($_GET['variable1'] == "shoes")

if ($_GET['q16_selectFrom16'] == "shoes")

以及选项中值标签中的更改文本,来自

<option value="Shoes"> Shoes </option>
<option value="Clothes"> Clothes </option>

<option value="shoes"> Shoes </option>
<option value="clothes"> Clothes </option>

为了区分大小写。这可能不是问题,但这是更好的方法。

于 2013-08-22T18:11:50.903 回答
1

这个给你

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<?php

  if($_GET['q16_selectFrom16'] )
  {
  if ($_GET['q16_selectFrom16'] == "Shoes") {
    $handle = fopen("shoes_sales.txt", "a");
  }
  else {
    $handle = fopen("clothes_sales.txt", "a");
  }
  foreach($_GET as $variable => $value) {
    echo $variable;

    $fwrite = fwrite($handle, $variable."=".$value."\r\n");
    if ($fwrite === false) {
            header("Location: thankforsumbitting.html");
        }else
        {
          echo "Erorr";
        }
  }
  fclose($handle);
  exit;
  }
?>
<form action="" method="get">
<li class="form-line" id="id_16">
   <label class="form-label-left" id="label_16" for="input_16"> Select choice </label>
   <div id="cid_16" class="form-input">
     <select class="form-dropdown" style="width:150px" id="input_16" name="q16_selectFrom16">
       <option value="Shoes"> Shoes </option>
       <option value="Clothes"> Clothes </option>
     </select>
     <input type="submit" value="sss" />
   </div>
 </li>
 </form>
</body>

</html>
于 2013-08-22T18:26:43.760 回答