ThreadPeak
ThreadPeak

Reputation: 149

html form button carrying array element as string

I want to be able to carry $_POST['v'] (string) to delete.php using an HTML form button.

$directory = "exports/";
$contents = scandir($directory,1);
foreach($contents as $k => $v) {
if ($v != '.' && $v != '..') { 
  echo "<a href=\"$directory" . $v . "\">".$v."</a>";
  echo "<form action=\"delete.php\" method=\"post\"><a class=\"buttonnohover\"><input type=\"submit\" class=\"button\" name=\"v\" value=\"Delete\" /></form>  <br>";
  }
}

At the moment the submit button simply brings back string(6) "Delete". I want $_POST['v'] to equal each individual $v (filename within the directory) carried on each button.

What am I not doing right?

Upvotes: 0

Views: 538

Answers (1)

kero
kero

Reputation: 10638

When you want to get the value of $v to delete.php using the form, you can use a hidden input:

echo '<a href="'.$directory.$v.'">'.$v.'</a>';
echo '<form action="delete.php method="post">
    <input type="hidden" name="v" value="'.$v.'" />
    <a class="buttonnohover"><input type="submit" class="button" name="submit" value="Delete" />
    </form><br>';

Currently you are getting "Delete" back, because it is the value of the button - just as you defined it in the HTML.

Additionally I would suggest renaming $v to $filename - currently it is only a cryptic variable, because the context is small of course you know what it holds, but only by looking at other lines of code. When you name it $filename, you'll directly know what the content of that variable will be.

Upvotes: 2

Related Questions