简体   繁体   English

表单未使用php echo提交

[英]Form does not submit with php echo

I have the following code: 我有以下代码:

index.php 的index.php

<?php
  if (isset($_POST['post'])) {
    echo "<script>alert(".$_POST['val1'].")</script>";
  }
?>

<html>
...
<body>
...
<?php
  echo "<form id='form_id' method='post' name='myform'>";
  echo "<input id='val1' type='text' value='7'>";
  echo "<input id='submit' type='submit' value='submit'>";
  echo "</form>";
?>
...
<body>
</html>

This will create a form, a text field and a submit button. 这将创建一个表单,一个文本字段和一个提交按钮。 All those shown perfectly fine except it would not submit the form. 所有这些都表现得很好,除非它不会提交表格。 So my question is why does my form not work here and what should I do to solve this problem without moving it out from php. 所以我的问题是,为什么我的表单在这里不起作用?我应该怎么做才能解决此问题而不将其从php中移出。

Problem is that you are missing name attributes for the elements: 问题是您缺少元素的name属性:

<?php
  if (isset($_POST['post'])) {
    echo "<script>alert(".$_POST['val1'].")</script>";
  }
?>

<html>
...
<body>
...
<?php
  echo "<form id='form_id' method='post' name='myform'>";
  echo "<input id='val1' name='val1' type='text' value='7'>";
  echo "<input id='submit' name='post' type='submit' value='submit'>";
  echo "</form>";
?>
...
<body>
</html>

You can notice the addition of name attribute for text field and submit button. 您会注意到为文本字段和提交按钮添加了name属性。 Only name attribute will be used to identify the fields when form gets posted. 发布表单时,仅name属性将用于标识字段。

In addition to Thamilan's answer about the name attribute you should use empty ($POST['val1']) instead of isset . 除了Thamilan关于name属性的答案之外,您还应该使用 ($ POST ['val1'])而不是isset Here is a complete working example: 这是一个完整的工作示例:

<!DOCTYPE html>
<head>
<?php
if (empty($_POST['val1'])) 
  print "<title>Not yet submitted</title>";
else 
{
  echo "<script>alert(".$_POST['val1'].")</script>";
  echo "<title>Submitted</title>";
}
?>
</head>
<body>
<?php
  echo "<form id='form_id' method='post' name='myform'>";
  echo "<input name='val1' id='val1' type='text' value='7'>";
  echo "<input id='submit' type='submit' value='submit'>";
  echo "</form>";
?>
</body>
</html>

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

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