/    Sign up×
Community /Pin to ProfileBookmark

server request method if statement not working

this totally doesn’t make sense but the following php…

[code=php]
if($_SERVER[‘REQUEST_METHOD’] == “POST”){
//code
}else{
echo $_POST[‘username’];
}
[/code]

and the following html when submitted with the username ‘thepulsiphyer’

[code=html]
<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>
[/code]

spits out this:

thepulsiphyer

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

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 22.2014 — Any reason you are using REQUEST_METHOD instead of just :
[code=php]
if (isset($_POST['username']))
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 22.2014 — 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:

[code=php]
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(isset($_POST['username'])) {
echo $_POST['username'];
}
}else{
echo "NOT A POST REQUEST";
}
[/code]
Copy linkTweet thisAlerts:
@deathshadowJul 22.2014 — 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?

[i]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.[/i]

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.
Copy linkTweet thisAlerts:
@thewebiphyerauthorJul 22.2014 — 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:

[code=php]
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(isset($_POST['username'])) {
echo $_POST['username'];
}
}else{
echo "NOT A POST REQUEST";
}
[/code]
[/QUOTE]


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
Copy linkTweet thisAlerts:
@deathshadowJul 22.2014 — 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.
×

Success!

Help @thewebiphyer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...