Viewed   88 times

I have searched the all questions with similar title, no solution for me yet.

I have a website running on apache2. I need to submit sensitive information through a form and I need to use POST method. Instead of POST, it sends GET request.

HTML:

<form action="/add_user.php" method='POST'>
   Enter Username: <input type="email" name="email" required="required" /> <br/>
   Enter password: <input type="password" name="password" required="required" /> <br/>
   <input type="submit" value="submit"/>
</form>

PHP:

<?php

$email=$_POST['email']; 
$password=$_POST['password'];
//do stuff
?>

I have opened Network monitor in Firefox, and the method is confirmed as GET. I have tried to even make it PUT instead of POST, still it sends GET. Also, $email and $password get the values if I change them to $_GET instead of $_POST.

Any help would be appreciated.

 Answers

3

One way to solve this is to reinforce your intentions by expliciting formmethod="post", like this:

<button type="submit" formmethod="post" formaction="add_user.php">Submit</button>
Monday, August 1, 2022
 
redbite
 
1

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022
1

This code is working. You need to add some condition, that checks, if $username is posted or not.

Something like that:

if(count($_POST)){
    $username ='';
    if(isset($_POST['user'])){
        $username = $_POST['user'];
    if ($username==null || !$username)
         echo 'username is null';
     echo strlen($username);
     echo $username;
   }

 }
Thursday, August 18, 2022
 
adnaan
 
2

This question was also answered by me in the Larachat slack forum, and for others sake here is the answer for the next one with such a problem.

Just a little back story. In the chat we found out that it was receiving a 301 error which is a redirect error. I had the same error recently when posting to a url on a staging server, it was working fine locally but not on the staging server.

The problem appeared to be a slash at the end of the post url.

So posting to https://example.com/post/to/ will not work.

Removing the / and posting to https://example.com/post/to will work.

Thursday, August 11, 2022
 
3

you want this:

<%= form_for @user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
  stuff goes in here as before
<% end %>
Friday, September 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :