-6

Using this code I displayed the 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>";

instead of using this code I posted earlier

 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>";

Then this pops-out after I POSTED the values

Message: Undefined variable: id

Filename: views/Edit_view.php

Message: Undefined variable: name

Filename: views/Edit_view.php

This is the whole package

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();
  }
 } 
 ?> 
  • 2
    is there a question here? –  Apr 30 '16 at 03:51
  • 2
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Mike Apr 30 '16 at 03:52
  • I tried to use single quote in here but it is still not diplaying the form – Gian Paolo Dolotina Apr 30 '16 at 03:54

4 Answers4

2

First of all, it's a really poor practice to write your HTML code within PHP echo command. I'd suggest you to keep your HTML and PHP code separate.

Try this:

<?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>

You may want to go through this since these are fundamental coding patters.

PHP in HTML

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • I used this code earlier but it says that id and name are undefined... XD – Gian Paolo Dolotina Apr 30 '16 at 04:00
  • Then maybe you haven't declared those variables earlier. There's nothing wrong with the coding pattern I've mentioned. Try var_dump($id) and var_dump($name) in the previous line to see what you're gettting. :) – Indrasis Datta Apr 30 '16 at 04:01
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;
user2429266
  • 390
  • 1
  • 3
  • 19
  • PS: You should go with the solution from Object Manipulator if possible and strictly keep logic and templates seperated if possible. So first do all the logic and then use a template. – user2429266 Apr 30 '16 at 04:04
0

You can not expect that PHP can know, which double quote is to delimit PHP strings and which one is to mark HTML attributes.

bad:

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

You have several possibilities:

Enclose PHP strings in single quotes. You have to insert variables by closing the string and concatination:

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

You can use single quotes in HTML. Then double quotes are used in PHP, which allow variable interpolation within the string.

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

Interpolation is also possible in heredoc notation:

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

One of the best concepts is to use PHP as template language in HTML. That means: Don't output much HTML from PHP. Just write HTML and do short PHP outputs:

Name: <input type="text" name="newName" value="<?php echo $name?>"> &nbsp;
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
-1

Use like this :

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>";
Aju John
  • 2,214
  • 1
  • 10
  • 27