简体   繁体   中英

Why can't Code Igniter detect that my form is being submitted via POST?

I have a form in a Code Igniter view in my jQuery Mobile application.

<form action="<?= BASE_PAGE_URL ?>settings" method="post" id="settingsForm">
    <div data-role="fieldcontain">
        <label for="firstName">First Name</label>
        <input type="text" name="firstName" id="firstName" value="" placeholder="First Name" />
    </div>
    <div data-role="fieldcontain">
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName" id="lastName" value="" placeholder="Last Name" />
    </div>
    <input type="hidden" name="purpose" value="register" />
    <input type="submit" name="submit" value="Register" />
</form>

However, when I write this code into the controller method that the URL specified by action leads to:

echo ($_SERVER['REQUEST_METHOD'] == 'POST') ? "yay" : "nay";

"nay" is written to the page when I hit the submit button. How come Code Igniter cannot tell that I am submitting a post request?

If you want to determine if you have POST data or if request was a POST, use post() method from the input class

$this->input->post(index);

//returns FALSE if no POST data
//returns the POST array if there is data (hence, a POST)
//returns a specific data from the array if you provide "index"

It can be cause of register_globals . Use

if($_POST) 

OR CI input class

$this->input->post('var');

I wonder if your form is returning a post request as you expect, but that something is redirecting immediately afterwards, which you're seeing as a GET? Try putting a temporary exit() immediately into the form-handling action; I suspect that will come out as POST.

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