简单的PHP:从表单输入获取变量 [英] Simple PHP: getting variable from a form input

查看:156
本文介绍了简单的PHP:从表单输入获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能真正使用PHP,从我在教程中看到的内容来看,它应该可以工作,但是不能:

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>

我是否有理由无法获得name的值? 很抱歉问这样一个简单的问题...

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

这是小提琴 http://jsfiddle.net/DCmu5/1/,所以,请尝试使用您所说的内容,然后在回答有效之前,先将其发送给我

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

推荐答案

PHP是一种服务器端语言,因此您需要提交表单才能访问其变量.

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

由于您未指定method,因此假设使用GET,因此您需要$_GET超全局变量:

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

echo $_GET['name'];

使用$_POST可能更好(因为这样可以避免直接在URL中传递值.因此,您可以将method属性添加到<form>元素中,如下所示:

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>

注意,我也添加了一个提交按钮,以达到良好的效果,但这不是必需的.只需在文本框内单击return即可提交表单.

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.

这篇关于简单的PHP:从表单输入获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆