简体   繁体   中英

What to use instead of if(isset($_POST['submit'])) for this.form.submit()?

I want to run a particular block of PHP if the user submits a form. It works if I use a submit button with name="submit" and:

<?php
if(isset($_POST['submit'])) {
code to run
}
?>

I don't know anything about javascript, and I want the code to run if the user changes a dropdown menu. If I make the first line of the dropdown

<select name="dropdownname" onchange="this.form.submit()">

the form appears (I haven't tested it) to submit if the user changes the dropdown choice. However, if I do this, the if(isset($_POST['submit'])) PHP code doesn't run. Is there a PHP if statement I can write that will respond to the form being submitted even though it's being submitted by a change in the dropdown and not a submit button?

您可能需要直接检查:

if(isset($_POST['dropdownname']))

Even you can use something like:

 if(count($_POST)){
 // form validation
 } else {
 //...
 }

你应该总是检查$ _SERVER ['REQUEST_METHOD']而不是特定的字段名称

如果此下拉列表用于显示某些数据,而不是写入数据库,则应使用GET方法。

<?php

if(!empty($_POST))
{
    code to run
}

?>

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