如何制作html / php表格?

时间:2016-01-08 18:03:05

标签: php html forms

我有一个php文件(让我们先调用它),它将名字存储在数据库中。我现在有一个不同的PHP文件与html / php形式(让我们称之为第二)。如何使用我的初始php(第一个)文件将表单(第二个)输入名称放入我的数据库?

以下是我的php表单:

<!DOCTYPE HTML>
<HTML>
    <head>
       <title>PHP FORM</title>
    </head>
    <body>
<h2>PHP FORM for Process Form</h2>

<form method="post" action="processForm.php">

    Name: <input type="text" name="names" required = "required"><br>
    <input type="submit" value="Create Users" onclick="formInputNames"><br>

    <input type="checkbox" name="activate" value="Activate">Activate

</form>

   </body>
   </html>

首先是&#39; php首先&#39;:

$nameList = 'Obi One, Not Naw, Lent Over, Foo Bar';

$newerName = 'Green Sauce';
$nameList = newUse($newerName,$nameList);

$email = '@email.org';


$fullnames = explode(" ",$nameList);

 function newUse($nep, $nameList){
      if($nep == empty($nameSplit[0]) && empty($nameSplit[1]) || empty($newName)){
    return "$nameList, $nep";
   } 
  return $nameList;
 }

/*I open the database here*/

foreach ($fullnames as $fullname){
    $nameSplit = explode(" ", $fullname);

 if ($nameList == empty($nameSplit[0]) || empty($nameList)){
    echo 'No First Name Here Or No Name At All';
    echo '<br>';
    echo '<br>';
} elseif ($nameList == empty($nameSplit[1])){
    echo 'No Last Name Here';
    echo '<br>';
    echo '<br>';
} else{

    $firstName = $nameSplit[0];
    $lastName = $nameSplit[1];
    $emailUser = $nameSplit[0].$email;

echo 'First Name: ' . $firstName;
echo '<br>';
echo 'Last Name: ' . $lastName;
echo '<br>';
echo 'Email Address: ' . $firstName . $email;
echo '<br>';
echo '<br>';
}

 $queryString = "INSERT INTO `project`.`user` 
(`id`, `firstName`, `lastName`, `email`, `activated`, `date_created`) 
VALUES 
(NULL, '$firstName', '$lastName', '$emailUser', '0', NOW())";

$result = mysqli_query($conn, $queryString)
or die (mysqli_error($conn));
}

我是php的新手,我真的迷失在这里。我很确定我需要使用POST,但我真的不明白。请帮帮我。谢谢。 我用Google搜索的所有内容都没有帮助我,本网站上的一些类似问题也没有。我需要帮助。

2 个答案:

答案 0 :(得分:1)

如果您将自己的功能放在processForm.php上方的位置,则可能是

  // I am assuming this comes from your database
  $nameList = 'Obi One, Not Naw, Lent Over, Foo Bar';

  $newerName = $_POST['names'];
  $email = '@email.org';

  function newUse($nep, $nameList){
  $nameSplit = array();
  $nameSplit = explode(" ", $nep);
      if(!empty($nameSplit[0]) && !empty($nameSplit[1]){
      return "$nameList, $nep";
      } 
  return $nameList;
  }

  $nameList = newUse($newerName,$nameList);

  // need to explode on comma into name pairs with spaces or there will be nothing to explode into $nameSplit later.
  $fullnames = explode(',', $nameList);

可能值得制作两个文本框 - 一个用于名字name="firstname",另一个用于第二个名称name="secondname",然后将两者放在一起,如

  $newerName = $_POST['firstname'] . " " . $_POST['secondname']; 

这样可以确保您降低人们放置两个空格或其他不需要的名称的风险,这会使您的explode();失败。

  $newerName = mysqli_real_escape_string($newerName);

在你将它放入函数之前将有助于消除一些安全问题,但它并非绝对可靠。

HTML将是

  <form method="post" action="processForm.php">
      First Name: <input type="text" name="firstname" required /><br />
      Second Name: <input type="text" name="secondname" required /><br />
      <input type="submit" value="Create Users" /><br /> 
      <input type="checkbox" name="activate" value="Activate" />Activate
  </form>

您可以将复选框的值设为1,然后将其选为$activate = intval($_POST['activate']);,其中强制转换整数将具有清除它的效果。然后,您可以将其用作MySql中当前为“0”的变量。

答案 1 :(得分:1)

您似乎只对如何发布值有疑问,所以您的HTML文件:

<form method="post" action="processForm.php">
   First Name: <input type="text" name="firstname">
   Second Name: <input type="text" name="lastname">
   <input type="submit" value="Submit">
</form>

您的PHP文件:

$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$name = $firstName ." ". $lastName;

   //Code for adding the values to the database.