Reading form paramaters with PHP when PHP generated HTML form used

166 views Asked by At

I am looking for help about a basic problem. I would like to write a .php script, what generates a HTML form, and when the form is submited, then the same .php script gets the form paramter, do the operation.

This is code I have so far, but it is not working:

<?php 

echo "<html>";
echo "<head>";
echo "<title>This is a test</title>";
echo "</head>";
echo "<body>";
echo "<form name='input' action='phptest1.php' method='get'>";
echo "Type the folder name: <input type='text' name='foldername[]'>";
echo "<input type='submit' value='OK'>";
echo "</form>";
echo "</form>";
echo "</body>";

$folder_var = $_POST['foldername'];
  if(empty($folder_var)) 
  {
    echo("No folder name was specified.");
    exit();
  } 
      echo "Folder name is : " . $folder_var[0];

?>

My goal is put a separate html file and a php file together, where the php file is generating the html form, and the same php script interpreters it.

a1.html:

<html>
<head>
<title>This is a test</title>
</head>
<body>
<form name="input" action="a2.php" method="post">
Type the folder name: <input type="text" name="foldername">
<input type="submit" value="OK">
</form>

</body>

a2.php

<?php 


$folder_var = $_POST['foldername'];
  if(empty($folder_var)) 
  {
    echo("No folder name was specified.");
        exit();
  } 
      echo "Folder name is : " . $folder_var;

?>
1

There are 1 answers

1
allen213 On BEST ANSWER

For a start Change

echo "<form name='input' action='phptest1.php' method='get'>";

to

echo "<form name='input' action='phptest1.php' method='post'>";