javascript 为什么我得到“未定义的索引”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4625466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:20:29  来源:igfitidea点击:

Why do I get "undefined index"?

phpjavascripthtmlcssforms

提问by magna

Currently I'm developing contact us form what is my question when i run that script on localhost, the name field and email field inside error has appeared like:

目前我正在开发联系我们表格,当我在本地主机上运行该脚本时,我的问题是什么,错误中的名称字段和电子邮件字段出现如下:

(<br /><b>Notice</b>:  Undefined index: name in <b>E:\wamp\www\rrr\btech\index.php</b> on line <b>49</b><br />)
(<br /><b>Notice</b>:  Undefined index: email in <b>E:\wamp\www\rrr\btech\index.php</b> on line <b>49</b><br />)

after click the submit the response will be undefined syntax error.

单击提交后,响应将是未定义的语法错误。

<tr>
<td valign="bottom"><span class="contactus-txt">
  <input name="textfield222" type="text" class="contact-field" style="width:125px;"   value="<?php echo $_GET['name'];?>" />
</span></td>
<td valign="bottom"><span class="contactus-txt">
  <input name="textfield2222" type="text" class="contact-field" style="width:125px;"  value="<?php echo $_GET['Email-Id'];?>"/>
</span></td>
</tr>

I used that html code.

我使用了那个 html 代码。

Can any one tell me what mistake I made?

谁能告诉我我犯了什么错误?

回答by Your Common Sense

Here goes the rightway:

这是正确的方法:

<?php
$FORM['name'] = "";
$FORM['Email-Id'] = "";
if (isset($_GET['name'])) $FORM['name'] = htmlspecialchars($_GET['name']);
if (isset($_GET['Email-Id'])) $FORM['Email-Id'] = htmlspecialchars($_GET['Email-Id']);
?>
<tr>
<td valign="bottom"><span class="contactus-txt">
  <input name="textfield222" type="text" class="contact-field" style="width:125px;"   value="<?php echo $FORM['name'];?>" />
</span></td>
<td valign="bottom"><span class="contactus-txt">
  <input name="textfield2222" type="text" class="contact-field" style="width:125px;"  value="<?php echo $FORM['Email-Id'];?>"/>
</span></td>

All variables should be initialized before use.

所有变量在使用前都应该初始化。

回答by user466764

Your question is not all that clear. I assume you have an action for this form and either a post or get method?

你的问题不是很清楚。我假设您对此表单有一个操作以及 post 或 get 方法?

From the two inputs I can see, your values should appear in the $_POST variable as $_POST['textfield222'] and $_POST['textfield2222'] for the post method and $_GET['textfield222'] and $_GET['textfield2222'] if the form is using the get method.

从我可以看到的两个输入中,您的值应该出现在 $_POST 变量中,作为 post 方法的 $_POST['textfield222'] 和 $_POST['textfield2222'] 以及 $_GET['textfield222'] 和 $_GET[' textfield2222'] 如果表单使用 get 方法。

As a general rule, never trust user input - validate the forms data before using it.

作为一般规则,永远不要相信用户输入 - 在使用之前验证表单数据。

I hope this is of use.

我希望这是有用的。