尝试使用按钮不起作用发送值

Basically I have a page of buttons, which each hold dates (retrieved from MySQL DB)

So it looks something like:

23 March 2014
22 March 2014

And so on.

I want to pass a value with that button, so when the user clicks on the button (using onclick (this project is just for my own use)) it takes them to a page which displays relevant data about that work day.

I've tried to use a hidden field, but that does not seem to be working. I've tried using values on the button elements themselves, to no avail either. Nothing I try seems to be working. The code I have is the following:

        for($i = 0; $i < $res; $i ++)
        {
            echo "<form method=\"get\" action=\"view.php\">";
            echo "<div class=\"button date\" value=\"$myArray[$i]\" onclick=\"window.location='view.php'\" name=\"button\">";
            echo "<p> $myArray[$i] </p>";
            echo "<input type=\"hidden\" value=\"$myArray[$i]\" name=\"WantedDate\">";
            echo "</form>";
            echo "</div>";

        }    

($myArray[$i] holds the date)

Then on the page "view.php" (which they are sent too after they click on the button) I have:

    $olddate = $_GET['WantedDate'];

    echo $olddate;

This produces no results.

Basically I need a solution whereby each button can contain a unique value, so the next page can use that to read it from the database.

Thanks

There are a couple of ways to do this. You can submit the form, as others have said. To do that, you'd need to change your code a bit and use <input type="submit">.

<form method="get" action="view.php">
  <?php for($i = 0; $i < $res; $i ++){ ?>

    <p><input type="submit" name="WantedDate" value="<?php print $myArray[$i]; ?>"></p>

  <?php } ?>
</form>

Note here that:

  1. You only need to use one form element. The code you posted, would actually print an entire form for each button. That isn't necessary.

  2. PHP is also a template language. You can move in and out of it, and don't need to use echo to print all of your HTML.

The other way to do this would be to append the date to the URL, and use a link instead.

  <?php for($i = 0; $i < $res; $i ++){ ?>

    <p>
        <a href="view.php?WantedDate=<?php print $myArray[$i]; ?>">
           <?php print $myArray[$i]; ?>
        </a>
    </p>

  <?php } ?>

Get rid of the onclick=\"window.location='view.php'\". You are telling the browser that at the moment someone clicks the button it should redirect to view.php, it won't submit the form.

Change that into type=\"submit\", telling the browser to submit the form.

Update:

Sorry, you said 'I basically have a page of buttons' so I figured it would be a button. I see you are trying to create a button using a div. Which won't work, as the div was never designed to work as a form element.

Change the onclick=\"window.location='view.php'\" into onclick=\"window.location='view.php?WantedDate=$myArray[$i]'\", you can get rid of the other things (value and name) as they won't do anything with the div. Furthermore you can lose the hidden input element.

There are other things wrong with your form, you close the form before you close the div, which may seems like a logical thing to do (that's how you started, right?) but HTML does not work that way; It should be

  echo "<form [...]>";
  echo "<div [..]>";
  [...]
  echo "</div>";
  echo "</form>";

You're using div as a button! That'll not cause the form to sumbit. Change it:

echo "<form method=\"get\" action=\"view.php\">";
echo "<input type=\"hidden\" value=\"$myArray[$i]\" name=\"WantedDate\">";
echo "<input type=\"submit\" value=\"$myArray[$i]\" />";
echo "</form>";

you need to do like this

echo "<div class='button date' value='".$myArray[$i]."' onclick=window.location='view.php?date='".$myArray[$i]."' name='button'>";

or you can use location.href='view.php?date='".$myArray[$i]."'

then in

view.php

$date = $_GET['date'];

echo $date;