Gian Paolo Dolotina Gian Paolo Dolotina - 7 months ago 20
PHP Question

Displaying a form in HTML using PHP

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();
}
}
?>

Answer

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