简体   繁体   中英

How can I get style="display:none" value by using POST method in PHP

This is my php code.

if (isset($_POST['submit'])) {
$uname = $_POST['uname'];       
$pass= md5($_POST['pass']);
$level = $_POST['ulevel'];
$role = $_POST['urole'];}

This is HTML code

<div id="alevel" style="display: none">
    <select id="select" name="urole">
        <option value="">Select Role</option>
        <option value="admin">Admin</option>
        <option value="editr">Editor</option>
    </select>
</div>

I want after submit select role in urole value POST to $role

If you want to POST anything to a different page, you'll have to put it in a form tag. This is the only way I know of (though it wouldn't surprise me if there were others; somebody comment if I'm wrong about this).

Here's an example:

<form action="nextpage.php" method="POST">
  <select name="select">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
  </select>
  <input type="text" name="input1">
  <input type="text" name="input2">
  <input type="submit" name="submit">
</form>

When you click on the 'submit' input, it loads the 'nextpage.php' (this is the 'action' attribute of the form) and POSTs data to that page (this is why the 'method' attribute says 'POST'). Your $_POST variable will look like this:

$_POST == Array(
  "select" => "option1", //or option2 or option3, depending on value selected
  "input1" => "value1", //whatever they put in the input boxes
  "input2" => "value2"
)

So you can see that the keys are the 'name' attribute and the values are the 'value' attribute of the selects or inputs that are in the form. Only values inside the form will be submitted.

You should also note that surrounding the form with a div that has a style of "display: none;" means that it won't be visible on the page and the only way to interact with it is by running JS commands through the console; not very user-friendly.

EDIT : making it not visible doesn't prevent it from being used. It will still post the form as long as the 'submit' button can be clicked somehow.

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