简体   繁体   中英

What's wrong with my form?

I had a form which was unstyled, so I decided to style it a bit.

Form that does nothing:

<form class="form-horizontal" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  <div class="form-group">
    <label for="username" class="col-sm-2 control-label">Username</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" placeholder="<?php echo $_POST['username']; ?>">
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label">API Key</label>
    <div class="col-sm-10">
      <p class="form-control-static"><?php if(strpos(file_get_contents("keys.php"),base64_encode($_POST['username'])) !== false) {echo "API Key already exists";} else { echo base64_encode($_POST['username']); }?></p>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Get API Key</button>
    </div>
  </div>
</form>

Working form:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  Username: <input type="text" name="username" value"<?php echo $_POST['username']; ?>" /><br /><br />
  Key: <input type="text" value="
  <?php if(strpos(file_get_contents("./keys.php"),base64_encode($_POST['username'])) !== false) {
    echo "API Key already exists";
  } else { echo base64_encode($_POST['username']); }?>" readonly /><br />
  <input type="submit" />
</form>

I'm not really sure what's wrong.

The broken form doesn't POST the data, thus it doesn't display it.

I think I finally understand your issue. Its not that the form doesn't POST. It goes to the next page. No, its that the values get lost.

So why do the values get lost? Because you have no name attribute on your inputs!

This:

<input type="text" class="form-control" placeholder="<?php echo $_POST['username']; ?>">

Should be:

<input type="text" name="username" class="form-control" placeholder="<?php echo $_POST['username']; ?>">

In fact, shouldn't placeholder be value ?

This line:

<input type="text" class="form-control" placeholder="<?php echo $_POST['username']; ?>">

is missing the /> at the end.

Also, in the working one, you have a

Key: <input type="text" value="...

The one that is not working:

<p class="form-control-static"><?php if(strpos(file_get_contents...

You are missing an input hidden or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM