Allen Aquino
Allen Aquino

Reputation: 45

How to match the variables to my html form to php

I have a html form and use the get method

I want to input the data to shoes_sales.txt if the user select shoes option value, and input all the rest to clothes_sales.txt. Unfortunately this data does not show up in my .txt.

This is the html form:

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

This is the php trying to retrieve the form values:

<?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;
?> 

Upvotes: 1

Views: 198

Answers (3)

Mohammed Dawod
Mohammed Dawod

Reputation: 18

Here you are

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

Upvotes: 1

M.Svrcek
M.Svrcek

Reputation: 5645

At the beginning of the code you are using

header("Location: thankforsumbitting.html");

Put it at the end of the file, so every lines of code are executed before redirection.

Change this:

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

to

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

and the change text in value tag in options, from

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

to

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

for case-sensitivity. It may not be problem, but it is better way.

Upvotes: 1

ಠ_ಠ
ಠ_ಠ

Reputation: 3078

The value in the $_GET variable does not correspond to the name property currently:

$_GET['variable1']

should be

$_GET['q16_selectFrom16']

You're also checking == "shoes" and == "clothes", while your values in your options use capital letters.

Upvotes: 2

Related Questions