FORM什么都不提交

时间:2019-06-24 08:29:48

标签: php html

我有一些php和html代码,我想做的就是从表单中捕获数据并将其打印在一个文件中。但是,当我单击“提交”时,没有任何数据。

我试图将代码分为2部分,但是没有变化。

这是代码:

<section id="register" class="section-with-bg wow fadeInUp">
        <div class="container">
            <div class="section-header">
              <h2>Register</h2>
            </div>
            <div class="form">
                <div id="errormessage"></div>
                <form id="data" action="#" method="post" class="contactForm">
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <input type="text" name="name1" class="form-control" id="name1" placeholder="Nama Depan" />
                            <!--<div class="validation"></div>-->
                        </div>
                        <div class="form-group col-md-6">
                            <input type="text" name="email" class="form-control" id="email" placeholder="Email" />
                            <!--<div class="validation"></div>-->
                        </div>
                        <div class="form-group col-md-6">
                            <input type="text" class="form-control" name="pekerjaan" id="pekerjaan" placeholder="Pekerjaan" />
                            <!--<div class="validation"></div>-->
                        </div>
                        <div class="form-group col-md-6">
                            <input type="text" name="nip" class="form-control" id="nip" placeholder="NIP (Jika Anda Seorang ASN)"/>
                            <!--<div class="validation"></div>-->
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <input type="password" name="pwd" class="form-control" id="pwd" placeholder="Password" />
                            <!--<div class="validation"></div>-->
                        </div>
                        <div class="form-group col-md-6">
                            <input type="password" class="form-control" name="pwd2" id="pwd2" placeholder="Repeat Password" />
                            <!--<div class="validation"></div>-->
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <input type="text" name="tlp" class="form-control" id="tlp" placeholder="Telepon" />
                            <!--<div class="validation"></div>-->
                        </div>
                        <div class="form-group col-md-6">
                            <input type="text" name="hp" class="form-control" id="hp" placeholder="HP" />
                            <!--<div class="validation"></div>-->
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea class="form-control" name="message" rows="5" placeholder="Alamat"></textarea>
                            <!--<div class="validation"></div>-->
                    </div>
                    <div class="text-center"><input type="submit" value="Submit" name ="submit" class="btn btn-primary" data-toggle="modal" data-target="#myModal"></div>
                </form>
            </div>
        </div>
    </section>

    <?php
      $nama =isset($_POST['name1']);
      $pekerjaan =isset($_POST['pekerjaan']);
      $nip =isset($_POST['nip']);
      $email =isset($_POST['email']);
      $pwd =isset($_POST['pwd']);
      $pwd2 =isset($_POST['pwd2']);
      $tlp =isset($_POST['tlp']);
      $hp =isset($_POST['hp']);
      $message =isset($_POST['message']);

      echo $nama;
      echo $pekerjaan;
      echo $nip;
      echo $email;
      echo $pwd;
      echo $pwd2;
      echo $tlp;
      echo $message;
    ?>

我尝试仅用1个输入和1个提交按钮来编写简单的代码,并且有效

更新: 谢谢大家的帮助,我已经解决了这个问题,我的JS代码出现了一些错误,现在我的代码可以工作了:)

4 个答案:

答案 0 :(得分:1)

您的代码返回“布尔”值,因为您为变量分配了布尔值:

$message =isset($_POST['message']);,此行将为$message变量分配true或false。

如果要检查是否已设置该值以便可以打印它,则必须执行以下操作:

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

这只是您的1行中的示例。相同的逻辑适用于所有变量。

答案 1 :(得分:1)

    <?php
if (isset($_POST['submit'])) {
      $nama =isset($_POST['name1']);
      $pekerjaan =isset($_POST['pekerjaan']);
      $nip =isset($_POST['nip']);
      $email =isset($_POST['email']);
      $pwd =isset($_POST['pwd']);
      $pwd2 =isset($_POST['pwd2']);
      $tlp =isset($_POST['tlp']);
      $hp =isset($_POST['hp']);
      $message =isset($_POST['message']);

      echo $nama;
      echo $pekerjaan;
      echo $nip;
      echo $email;
      echo $pwd;
      echo $pwd2;
      echo $tlp;
      echo $message;
}
    ?>

用您当前的php代码替换它。您不检查天气是否提交表单。可以使用if语句来检查提交按钮是否被单击。

答案 2 :(得分:0)

尝试将其添加到代码顶部

<?php
  session_start();
  extract($_POST, EXTR_SKIP);
  extract($_GET, EXTR_SKIP);
  extract($_SESSION, EXTR_SKIP);
  extract($_FILES, EXTR_SKIP);
?>

答案 3 :(得分:0)

我怀疑问题出在您分配变量值的方式上:$nama =isset($_POST['name1']);。将其简化为$nama = $_POST['name1'];。请记住,如果您的网站访问者未更改(任何一个)输入字段,则这些输入可能会显示您的placeholder=值。

<?php
  $nama = $_POST['name1'];
  $pekerjaan = $_POST['pekerjaan'];
  $nip = $_POST['nip'];
  $email = $_POST['email'];
  $pwd = $_POST['pwd'];
  $pwd2 = $_POST['pwd2'];
  $tlp = $_POST['tlp'];
  $hp = $_POST['hp'];
  $message = $_POST['message'];

  echo $nama;
  echo $pekerjaan;
  echo $nip;
  echo $email;
  echo $pwd;
  echo $pwd2;
  echo $tlp;
  echo $message;
?>