简体   繁体   English

HTML PHP表单处理; 变量

[英]HTML PHP Form Processing; Variable

I'm a newbie learning and trying to understand how html forms and php processing works. 我是一个新手学习并试图理解html表单和php处理的工作原理。

Came across this pair of examples: 遇到这对例子:

HTML FORM: HTML表格:

<html>
<body>

  <form action="hello-web.php" method="GET">
  <label>Name:</label>
  <input type="text" name="yourName" size="24">
  <input type="submit">
  </form>

</body>
</html>

PHP PROCESSOR: PHP处理器:

<?php
$fname = $_GET["yourName"];

echo "Hello $fname!";
?>

OUTPUT SHOULD BE: Hello Entered/Example Name! 输出应该是:你好输入/示例名称!

QUESTION: When I try to change the variable "yourName" (on BOTH HTML and PHP files) to, for example "typeName" , the entered name on the form does not show up. 问题:当我尝试将变量“yourName” (在HTML和PHP文件上)更改为“typeName”时 ,表单上输入的名称不会显示。

In other words, the output becomes just: Hello ! 换句话说,输出变为:你好!

Is "yourName" a standard php or html variable? “yourName”是标准的php或html变量吗? Can it not be changed to what ever you want it to be? 它不能改变成你想要的东西吗?

Better yet, how exactly does the form process data? 更好的是,表单处理数据的具体方式是什么?


Here is my altered code that won't output the entered name (I posted here as an answer because all the codes shows up as a continuous line, like a paragraph, when I paste as a comment to your answer: 这是我改变的代码,不会输出输入的名称(我在这里作为答案发布,因为当我粘贴作为你的答案的注释时,所有代码都显示为一个连续的行,如段落:

HTML FORM(altered--typeName): HTML FORM(更改 - typeName):

<html>
<body>

  <form action="hello-web.php" method="GET">
  <label>Name:</label>
  <input type="text" name="typeName" size="24">
  <input type="submit">
  </form>

</body>
</html>

PHP PRCESSOR (altered--typeName): PHP PRCESSOR(更改 - typeName):

<html>
<body>
<?php
$fname = $_GET["typeName"];

echo "Hello $fname!";
?>

</body>
</html> 

You can see what data is available to you from the submitted form by outputting the entire array. 您可以通过输出整个阵列从提交的表单中查看可用的数据。 Since your form method is GET , the following will show you all that was submitted: 由于您的表单方法是GET ,以下内容将显示您提交的所有内容:

var_dump( $_GET );

From this, you can see what the variable names should be in your PHP script. 从这里,您可以看到PHP脚本中的变量名称应该是什么。

Array
(
  [YourName] => Jonathan
)

Anytime you come across a disconnect between what is being submitted, and what you're expecting, check $_GET (or if your method is POST , you would check $_POST ). 无论何时遇到提交内容与您期望的内容之间的断开,请检查$_GET (或者如果您的方法是POST ,则检查$_POST )。

For instance, if I were trying the following: 例如,如果我尝试以下内容:

echo $_GET["yourName"]; // nothing output to the screen

I could refer to the array contents printed above and see that the correct key is "YourName": 我可以参考上面打印的数组内容,看到正确的键是“YourName”:

echo $_GET["YourName"]; // Jonathan

$_GET["yourName"]; Contains a value based off of the input of the form field. 包含基于表单字段输入的值。 It's a php superglobal http://us3.php.net/manual/en/reserved.variables.get.php 它是一个php超全球http://us3.php.net/manual/en/reserved.variables.get.php

It sounds like you're changing the html form, but your not entering a value through the form. 听起来你正在改变html表单,但是你没有通过表单输入值。 Therefore, $_GET["yourName"]; 因此, $_GET["yourName"]; is empty 是空的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM