2

I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:

<html>
  <head></head>
  <body>
    <form>
      <input type='text' name="name" value='myName'>
    </form>
    <p>
      <?php
        $name = $_POST['name'];
        echo $name
      ?>
    </p>
  </body>
</html>

Is there a reason I can't get the value of name? Sorry for asking such a simple question...

here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering

1
  • 1
    Add an <input type="submit" value="Go" /> and modify the for to POST method (`<form method="POST">=), submit the form, then it should be visible. Commented Jan 11, 2014 at 20:39

4 Answers 4

7

PHP is a server-side language, so you'll need to submit the form in order to access its variables.

Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:

echo $_GET['name'];

It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:

<form method="post">
    <input type='text' name="name" value="myName" />
    <input type="submit" name="go" value="Submit" />
</form>

Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried that too... you mean <form method='POST'>, right? didn't work
Well, it should. Are you sure you're actually submitting the form?
look at the fiddle and try it yourself: doesn't work! jsfiddle.net/DCmu5/1
Are you serious? Of course it doesn't work on jsFiddle... PHP code needs executing on a server, a proper server. Not jsFiddle.
ok, I'll try on my server and tell you how it goes, even tho I think I've already tried
|
3

Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>

<form action="yourpagename.php" method="post">
    <input type='text' name="name" value='myName'>
    <button type="submit">Submit</button>
</form>

Click button and whatever is typed in your field will show up.

1 Comment

wow... is it possible that such a simple question which should be at the basics of PHP isn't answerable?
1
<html>
<body>

<form method="post" action="1.php">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_POST['fname'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>

</body>
</html>

Comments

0

try this

<?php
    if(isset($_REQUEST['name'])){
                $name = $_REQUEST['name'];
                echo $name;
    }
     ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.