0

I am new to PHP. I have written a simple code to collect data from a form using PHP. I am using the POST Method to collect the form data but it goes to the else statement. Can someone please help me out. Thanks in advance!!

My Code-

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post">
        <input type="text" id="name" name="name">
        <input type="submit" name="submit" id="submit">
    </form>
<?php
if(isset($_POST)){
    $x = $_POST['name'];
    echo $x;
}else {
  echo "failed";
}
?>
</body>
</html>
Sabad Modi
  • 45
  • 1
  • 6
  • 4
    From the look of the script, I think you're trying to submit the form back to itself. If that's the case, you will always get the text "failed" before the form is submitted – Olawale Jan 08 '20 at 14:19
  • Looks you're missing to check `if($_POST['name'])`, you just checked for the whole array... Also you should define the "action" inside the form-Element – Petschko Jan 08 '20 at 14:24
  • Do `if(isset($_POST['name'])){` or better still `if($_SERVER['REQUEST_METHOD'] == 'POST' ){` then check `if(isset($_POST['name'])){` – RiggsFolly Jan 08 '20 at 14:24
  • If I want to post the data to another page, how should I do that – Sabad Modi Jan 08 '20 at 14:28
  • I tried to run this code and it seems fine. I used a Mac, but that shouldn't make a difference since PHP is server side. – Russ J Jan 08 '20 at 14:32
  • _If I want to post the data to another page, how should I do that_ Add an `action="some.php"` attribute to the `
    ` tag
    – RiggsFolly Jan 08 '20 at 14:34
  • Or check out the [Manual](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) – RiggsFolly Jan 08 '20 at 14:35
  • Also, turn on error reporting and see if anything comes out. – Russ J Jan 08 '20 at 14:36

3 Answers3

0

Don't you need an attribute called "action" to perform something like that? Or maybe it isn't needed if it is in the same file

h0merr
  • 182
  • 1
  • 9
  • No the default is to submit to the current form – RiggsFolly Jan 08 '20 at 14:22
  • If I want to post the data to another page, how should I do that – Sabad Modi Jan 08 '20 at 14:27
  • I dont remember PHP at all, but i can guide u, make a php file separated from the HTML one, and research the action attribute, then u bring the x var to that php file and from that php file you load in other html file or php file – h0merr Jan 08 '20 at 14:34
0

I think the culprit here is the isset function call. That function returns false even if a variable has been set but equals null, which I believe is what's happening here. Are you supplying any input before submitting?

I recommend using this to check if the form was submitted instead.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    ...
}

You can then process the inputs in that block. You might also try checking for !empty instead of isset if you want to keep exploring your script as it is now.

-1

Former answer was incorrect, time for an update.

Running your original code locally on OS X 10.14 using php 7.1 works fine.

Do you have a local server setup? How are you accessing the page that isn't behaving as expected? What happens when you place <?php phpinfo(); ?> in the same file?

user2355051
  • 605
  • 1
  • 8
  • 26
  • By default the form would submit to itself, if the action is missing or empty. – aynber Jan 08 '20 at 14:21
  • If I want to post the data to another page, how should I do that – Sabad Modi Jan 08 '20 at 14:25
  • Then what do you say about this @aynber https://stackoverflow.com/questions/9401521/is-action-really-required-on-forms – user2355051 Jan 08 '20 at 14:25
  • @LeoMessi10 just create a new php script and include the `action="differentphp.php"`. Then on the differentphp.php script you will be able to check the value of `$x` If this works for you please accept the answer. Thanks – user2355051 Jan 08 '20 at 14:29
  • I tested the original code on my own web server and it ran fine. – Russ J Jan 08 '20 at 14:33
  • @user2355051 Did you read the accepted answer? `If it's not set, the browser will likely use the same method as providing an empty string to it.` – aynber Jan 08 '20 at 14:40
  • @aynber you're correct. tested fine locally and I updated my answer. I'm thinking the issue is related to the server and how they're learning here. – user2355051 Jan 08 '20 at 17:31
  • @LeoMessi10 No worries, we've all been there. Make sure your development environment is up to date. I'm not 100% certain what was causing this for you, but I'm willing to bet something is either brand new or outdated that is causing this issue. Try different browsers and see if you can isolate the issue to how the browser implements the standards. – user2355051 Jan 08 '20 at 19:46