使用PHP在HTML中显示表单

时间:2016-04-30 03:50:09

标签: php html forms

使用此代码我显示了表单

 echo "<form action='Stud_controller/updateData' method='POST'>";
 echo '<input type="hidden" name="sameId" value="'.$id.'">';
 echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
 echo '<input type="submit" value="Save">';
 echo "</form>";

而不是使用我之前发布的代码

 echo "<form action="Stud_controller/updateData" method="POST">";
 echo "<input type="hidden" name="sameId" value=".'"'.$id.'">';
 echo "Name: <input type="text" name="newName" value=".'"'.{$name}.'"> &nbsp;';
 echo "<input type="submit" value="Save">";
 echo "</form>";

然后在我发布值

后弹出

消息:未定义的变量:id

Filename: views/Edit_view.php

Message: Undefined variable: name

Filename: views/Edit_view.php

这是整个包

Stud_controller.php

<?php 
   class Stud_controller extends CI_Controller {  

    public function __construct()
{
        parent::__construct();
    $this->load->helper('url');
    $this->load->model('Stud_model');

 }

  public function index() { 
    $this->load->helper('form');
    $data['data'] = $this->Stud_model->getData();

    $this->load->view('Stud_view', $data);
  } 

  public function deleteData($row)
  {

    $this->Stud_model->delete($row);
    $this->redirect();
  }

  public function editData($row)
  { 
    $data['singleData'] = $this->Stud_model->getSingleData($row);
    $this->load->view('Edit_view', $data);
  } 

  public function updateData()
  {
    $data = array('id' => $this->input->post('sameId'), 'fname' => $this->input->post('newName'));
    $this->Stud_model->update($data);
    $this->redirect();

  }

  public function addData()
  { 
    $id = NULL;
    $name = $this->input->post('name');

    $data = array(
        'stud_id' => $id,
        'name' => $name,
    );

    $this->Stud_model->add($data);
    $this->redirect();
  } 

  public function redirect()
  {
    $this->load->helper('form');

    $data['data'] = $this->Stud_model->getData();

    redirect('http://localhost/gpdolotina/index.php/Stud_controller');

    $this->load->view('Stud_view', $data);
  }
  } 
 ?>

Edit_view.php

<!DOCTYPE html> 
<html lang = "en"> 

<head> 
    <meta charset = "utf-8"> 
    <title>Edit</title> 
</head>

<body> 
  <?php
    echo "This is the edit_view.";
    echo "<br /><br />";

    foreach ($singleData as $edit)
    {
        $id = $edit->stud_id;
        $name = $edit->name;
        echo $id;
    }

    echo "<form action='Stud_controller/updateData' method='POST'>";
    echo '<input type="hidden" name="sameId" value="'.$id.'">';
    echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
    echo '<input type="submit" value="Save">';
    echo "</form>";

    ?>
  <a href="http://localhost/gpdolotina/index.php/Stud_controller">Home</a>


 </body>

 </html>

Stud_view.php

  <!DOCTYPE html> 
  <html lang = "en"> 
  <head> 
   <meta charset = "utf-8"> 
   <title>View Students</title> 
  </head>

  <body> 
  <?php
    echo "This is the view.";
    echo "<br /><br />";
  ?>

  <form method="post" accept-charset="utf-8" action="Stud_controller/addData">

     Name: <input type="text" name="name">&nbsp;
     <input type="submit" value="Add Name"><br><br>

  </form>


  <table border="1">
  <?php
    echo "<tr>";
    echo "<td>Student ID</td>"; 
    echo "<td>Name</td>"; 
    echo "<td>Edit</td>"; 
    echo "<td>Delete</td>"; 
    echo "<tr>"; 

     foreach ($data as $row)
    {
         echo "<tr>";
           echo "<td>".$row->stud_id."</td>"; 
           echo "<td>".$row->name."</td>"; 
           echo "<td><a href = '"."stud_controller/editData/"
              .$row->stud_id."'>Edit</a></td>"; 
           echo "<td><a href = '"."stud_controller/deleteData/"
              .$row->stud_id."'>Delete</a></td>"; 
           echo "<tr>"; 
           //
    }

  ?>
  </table>
 </body>

 </html>

Stud_model.php

 <?php 
  class Stud_Model extends CI_Model {

  function __construct() { 
     parent::__construct(); 
     $this->load->database();
  } 

  public function add($data) { 
     if ($this->db->insert("stud", $data)) { 
        return true; 
     } 
  } 

  public function delete($stud_id) { 
     if ($this->db->delete("stud", "stud_id = ".$stud_id)) { 
        return true; 
     } 
  } 

  public function update($data) { 
     $this->db->set("name", $data['name']); 
     $this->db->where("stud_id", $data['id']); 
     $this->db->update("stud", $data); 
  } 

  public function getSingleData($stud_id)
  {
     $getSingleData = $this->db->select("name");
     $getSingleData = $this->db->select("stud_id");
     $getSingleData = $this->db->from("stud");
     $getSingleData = $this->db->where("stud_id", $stud_id);
     $getSingleData = $this->db->get();

     return $getSingleData->result();
  }

  public function getData()
  {

     $getdata = $this->db->select("*");
     $getdata = $this->db->from("stud");
     $getdata = $this->db->get();

     return $getdata->result();
  }
 } 
 ?> 

4 个答案:

答案 0 :(得分:2)

首先,在PHP echo命令中编写HTML代码是一种非常糟糕的做法。我建议你将HTML和PHP代码分开。

试试这个:

<?php 

  var_dump($id);   // Debug the value of $id
  var_dumo($name); // Debug the value of $name
?>

<form action="Stud_controller/updateData" method="POST">
   <input type="hidden" name="sameId" value="<?php echo $id; ?>">
   <label>Name: <label>
   <input type="text" name="newName" value="<?php echo $name; ?>">
   &nbsp;
   <input type="submit" value="Save">
</form>

您可能想要了解这些,因为这些是基本的编码模式。

PHP in HTML

答案 1 :(得分:0)

$form = implode('',array(
  '<form action="Stud_controller/updateData" method="POST">',
  '<input type="hidden" name="sameId" value="'.$id.'" />',
  '<label class="label" for="newName">Name: </label>',
  '<input type="text" name="newName" value="'.$name.'" />',
  '<input type="submit" value="Save" class="submit-button">',
  '</form>'
));

echo $form;

答案 2 :(得分:0)

您不能指望PHP可以知道,哪个双引号用于分隔PHP字符串,哪个用于标记HTML属性。

坏:

echo "<form action="Stud_controller/updateData" method="POST">";

您有几种可能性:

将PHP字符串括在单引号中。您必须通过关闭字符串和连接来插入变量:

echo '<input type="hidden" name="sameId" value="' . $id . '">';

您可以在HTML中使用单引号。然后在PHP中使用双引号,它允许在字符串中进行变量插值。

echo "Name: <input type='text' name='newName' value='{$name}'> &nbsp;";

在heredoc表示法中也可以插值:

echo <<< END_OF_STRING

<form action="Stud_controller/updateData" method="POST">
  <input type="hidden" name="sameId" value="{$id}">
  Name: <input type="text" name="newName" value="{$name}"> &nbsp;
  <input type="submit" value="Save">
</form>

END_OF_STRING

最好的概念之一是在HTML中使用PHP作为模板语言。这意味着:不要从PHP输出很多HTML。只需编写HTML并执行简短的PHP输出:

Name: <input type="text" name="newName" value="<?php echo $name?>"> &nbsp;

答案 3 :(得分:-1)

像这样使用:

echo "<form action='Stud_controller/updateData' method='POST'>";
echo '<input type="hidden" name="sameId" value="'.$id.'">';
echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
echo '<input type="submit" value="Save">';
echo "</form>";