www.webdeveloper.com
Results 1 to 6 of 6

Thread: server request method if statement not working

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    84

    server request method if statement not working

    this totally doesn't make sense but the following php...

    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){
    //code
    }else{
    echo 
    $_POST['username'];

    and the following html when submitted with the username 'thepulsiphyer'

    HTML Code:
    <form action='login.php' method='post' enctype="text/plain" class='login'>
    				<table>
    					<tr>
    						<td>
    							<label>USERNAME</label>
    						</td>
    						<td>
    							<input type='text' name='username'>
    						</td>
    						<td>
    							<label>PASSWORD</label>
    						</td>
    						<td>
    							<input type='password' name='pass'>
    						</td>
    						<td>
    							<input type='submit' value='LOGIN'>
    						</td>
    					</tr>
    				</table>
    			</form>
    spits out this:

    thepulsiphyer

    could this possible make any less sense than it does? any ideas for how to make the condition pass?

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    849
    Any reason you are using REQUEST_METHOD instead of just :
    PHP Code:
    if (isset($_POST['username'])) 

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,518
    Yeah, unless you're implementing some sort of REST application where you need to know what the HTTP request type was, I'd just look for the $_POST (or whatever) elements of interest. If you are trying to be "RESTful", then it would make more sense to have something like:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        if(isset(
    $_POST['username'])) {
            echo 
    $_POST['username'];
        }
    }else{
        echo 
    "NOT A POST REQUEST";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Hmm... seems like folks aren't noticing what you're actually asking -- it appears $_SERVER['REQUEST_METHOD'] does not equal the method your form is submitting by.


    Do you have any scripting that's trapping the onsubmit and trying to AJAX it? Have you tried echo'ing out the value in $_SERVER['REQUEST_METHOD'] to see what's in it when the condition fails?

    Unrelated to your problem, but you might want to consider using semantic markup instead of tables for layout while at it... lose the table, get a fieldset in there, and get FOR attributes on your labels pointing at ID's on the INPUT. You may also find benefits to flipping your single and double quote usage.

    Really though, if a variable does not contain a value you expect or a simple comparison is failing, echo out the failing var to see what it contains.

  5. #5
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    84
    Quote Originally Posted by NogDog View Post
    Yeah, unless you're implementing some sort of REST application where you need to know what the HTTP request type was, I'd just look for the $_POST (or whatever) elements of interest. If you are trying to be "RESTful", then it would make more sense to have something like:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        if(isset(
    $_POST['username'])) {
            echo 
    $_POST['username'];
        }
    }else{
        echo 
    "NOT A POST REQUEST";

    I found out that there was a previous block testing this - if(isset($_GET['logout'])) - that wasn't closed and, therefore, wasn't allowing a post method to even be tested for. sorry for the waste of a post

  6. #6
    It happens, but that's why "snippets" are often useless in diagnosing a problem. If we'd seen the entire code, we probably would have noticed it.

    Sometimes you just need more eyes on the code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles