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.'"> ';
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}.'"> ';
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.'"> ';
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">
<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();
}
}
?>
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.'"> ';
echo '<input type="submit" value="Save">';
echo "</form>";
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; ?>">
<input type="submit" value="Save">
</form>
You may want to go through this since these are fundamental coding patters.
$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;
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}'> ";
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}">
<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?>">