开发者

obtaining the selected option value in PHP [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have the following piece of code which resembles a form with a drop-down list. 开发者_开发技巧The selected item from the list should be sent to process.php page which will do some work.

<form name="form" method="GET" action="process.php">
  <div id="extra-components">
    <select name="boolean">
      <option value="and" selected="yes">AND</option>
      <option value="or">OR</option>
      <option value="not">NOT</option>
    </select>
  </div>
</form>

My question is: in the process.php file how do I obtain the selected value from the form? via the $_GET method? Thanks in advance


If this is all the code of the form, it probably will not work. What you have forgotten is some submit button or at least some JS code to submit form without the submit button.

The proper code would be something similar to this (notice the additional 'submit' input):

<form name="form" method="GET" action="process.php">
  <div id="extra-components">
    <select name="boolean">
      <option value="and" selected="yes">AND</option>
      <option value="or">OR</option>
      <option value="not">NOT</option>
    </select>
    <input type="submit" name="submit" value="Submit" />
  </div>
</form>

And then, after clicking "Submit", the data will be sent to the process.php script and should be accessible by using:

$_GET['boolean']

Plus, it should be some value from the following strings:

  • 'and'
  • 'or'
  • 'not'

Try it - it should not hurt ;) The example solution:

if (isset($_GET['boolean']) && in_array($_GET['boolean'], array('and', 'or', 'not'))) {
    $chosen_boolean = $_GET['boolean'];
} else {
    $chosen_boolean = null;
}

Then if the

$chosen_boolean === null

that means that the value given was inproper or not supported (not from the available options). That should help you avoid any errors regarding the inproper value for this.


Depending on your HTML code you can access boolean value by $_GET['boolean']. $_GET['boolean'] will contain one of <option>'s value element until user will change it in his URL.

Esit:

Tip: Don't forget to filter user-submitted data, always. HTML, JS restrictions are nothing. 'Always assume that user is malicious' (@MarkB)


Yes. The value of the selected option will be passed as the value of the select parameter. So with this code, you'd get

$_GET['boolean']

set to 'and'


Yes, you find the content in $_GET['boolean'].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜