【问题标题】:bootsrap php contact form value shows error <br /><b>Notice</b>: Undefined variable: myThema in <b>C:\x\testForm.php</b> on line <b>105</b><br />bootsrap php 联系表单值显示错误 <br /><b>注意</b>:未定义变量:myThema in <b>C:\x\testForm.php</b> 行 <b>105</b> <br />
【发布时间】:2017-03-23 18:15:57
【问题描述】:

我正在使用引导程序,我想创建一个联系表单。我创建了一个名为 testForm.php 的文件,并在那里编写了我的 php 和我的 html 代码(我应该制作不同的文件吗?一个用于 php,一个用于 html?)。我的文件以 php 代码开始,并在 html 代码之后。只要我在 html 区域中放置一个&lt;?php echo....,无处不在,总是出现在站点未定义索引中:.例如

现在我只尝试使用一个参数 :thema 来查看它是否有效以及它是如何工作的,如果我将某些东西赋予价值,那么结果就像上图一样。 我的php代码:

<?php
if (isset($_POST["submit"])) {
    $thema = $_POST['thema'];



    // Check if thema has been entered
    if (!$_POST['thema']) {
        $errThema = 'Please enter your thema';
    }


}

?>

还有我的html代码:

<!-- MAIN SEITE -->
<h4><p class="text-primary"><u>Neuanforderungsformular</u></p></h4>

<!-- START FORM -->
<form method="post" role="form" action="testForm.php">
<!-- Thema Feld -->
<div class="form-group">
    <label for="usr">Thema / Title:</label>
    <input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($_POST['thema']); ?>">
</div>
<!-- Email Adresse Feld-->
<div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <div class="input-group col-sm-5">
    <input type="text" class="form-control input-lg" placeholder="Ihre Email Addresse" aria-describedby="basic-addon2">
    <span class="input-group-addon" id="basic-addon2">@teswt.de</span>
</div>

我怎样才能解决问题,以便我的联系表格可以正常工作?

【问题讨论】:

  • 你先声明$thema,然后再检查它是否已设置
  • 如何查看是否已设置?
  • if(isset($_POST['someName']))
  • 但我在乞讨时遇到了问题
  • 那是提交按钮,你还应该检查其他字段是否设置,特别是如果你接受空白值

标签: php html twitter-bootstrap contact-form


【解决方案1】:

这样使用

$thema = isset($_POST['thema']) ? $_POST['thema'] : '';

而不是

$thema = $_POST['thema'];

更新.1

试试看

<?php $thema = !empty($thema) ? $thema : ''; ?>
<input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($thema); ?>">

而不是

<input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($_POST['thema']); ?>">

【讨论】:

    【解决方案2】:

    当 $_POST 中不存在 thema 条目时,您也正在访问 $_POST['thema'])

    <div class="form-group">
        <label for="usr">Thema / Title:</label>
    <input type="text" id="thema" name="thema" class="form-control input-lg" 
            value="<?php echo htmlspecialchars($_POST['thema']); ?>">
     </div>
    

    那么你应该为 var 使用适当的设置,例如 $myThema

    if (isset($_POST["submit"])) {
          if (isset($_POST['thema'])){
              $myThema = $_POST['thema'];
          } else {
              $errThema = 'Please enter your thema';
              $myThema = '';
          }
      }
    

    .

    <div class="form-group">
        <label for="usr">Thema / Title:</label>
    <input type="text" id="thema" name="thema" class="form-control input-lg" 
            value="<?php echo htmlspecialchars($myThema); ?>">
     </div>
    

    【讨论】:

    • 我也尝试过还是一样的......来价值而不是thema mythema
    • 请更新您的问题并添加确切的错误消息(带有行号和显示此代码行的注释)
    【解决方案3】:

    试试这个:

    <?php
    if (isset($_POST["submit"])) {
        if (isset($_POST['thema'])){
            $thema = $_POST['thema'];
        } else {
            $thema = 'Please enter your thema';
        }
    }
    ?>
    

    你应该首先检查$thema是否已经设置。你所做的是你使用$_POST['thema'] 没有检查,因此错误出现在文本字段中

    【讨论】:

    • 是的,我已经尝试过,但仍然没有做任何事情,看起来和上图一样,当我点击提交时没有任何改变
    • 我编辑了,如果我没记错的话,如果$thema由于某种原因没有设置,Please enter your item会出现在文本字段中
    【解决方案4】:

    我会使用类似的东西:

    function GetPostOrDefault($key, $default = '')
    {
        if (array_key_exists($key, $_POST))
        {
            return $_POST[$key];
        }
        return $default;
    }
    

    然后

    <input value="<?= GetPostOrDefault('thema') ?>">
    

    <input value="<?= GetPostOrDefault('thema', 'Neues Thema') ?>">
    

    如果您没有发布任何帖子,您会收到此错误,因为未定义索引。

    这意味着$_POST['thema'] 仅在您提交的表单包含名称为thema 的字段时可用。在初始页面加载时,您执行 GET 请求。表单未提交。

    【讨论】:

    • 索引未定义是什么意思?
    • 我应该把这段代码和函数放在html区域内还是php区域内?对不起,我是新人:(
    • 在您拥有所有 Helper 函数 (PHP) 的地方。如果没有创建一个,请确保在您的 HTML 之前include 他们。 @JavaMunich
    • 确保您没有其他人引用 $_POST['thema'] 或其他索引为“thema”的数组,因为在我的回答中,只有在设置时才使用此索引..
    【解决方案5】:

    只需将您的默认 $thema 值移到 $_POST['submit'] 的检查之外。您当前的代码仅在提交表单时设置它。

    $thema = '';
    $errThema = '';
    
    if (isset($_POST["submit"])) {
        // Check if thema has been entered
        if (!$_POST['thema']) {
            $errThema = 'Please enter your thema';
        } else {
            $thema = $_POST['thema'];
        }
    }
    

    当然,您应该在表单中显示 $thema 而不是 $_POST['thema']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多