2013-08-22 20 views
1

我有一个HTML表单,并使用get方法如何变量匹配我的HTML表格到PHP

我想输入的数据,如果用户选择鞋期权价值shoes_sales.txt,并输入所有休息到clothes_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; 
?> 
+2

把'header'重定向末;它可能会在用户开始写文本文件之前重定向用户。 – andrewsi

+2

我认为这个'header(“Location:thankforsumbitting.html”);'即使在脚本尝试写入文件之前也会立即重定向。尝试拿出或使用'回声“谢谢你”;'。虽然这没有经过测试,我现在没有时间进行测试。尝试把它放在'fclose($ handle);'而不是,但是这可能会导致'headers already sent'错误消息。 –

+0

@andrewsi你在11秒内击败了我! –

回答

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> 
2

$_GET变量的值不符合目前name属性:

$_GET['variable1'] 

应该是

$_GET['q16_selectFrom16'] 

你也检查== "shoes"== "clothes",而你在你的option价值观使用大写字母。

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> 
区分大小写的

。这可能不是问题,但它是更好的方法。