Reputation: 521
I'm unsure how to get my editfields.php file to load when an administrator selects the Edit button for whatever row in my SQL table (adminlogin.php).
I feel my editfields.php file has the right stuff, but I'm stuck when it comes to getting the data and having that data display in my view as opposed to what I have printing according to my edituser function.
** PLEASE ignore the approve function for now, as well as the if conditionak in adminlogin.php, both items for separate discussion **
Below are the following files:
Controller (Myform.php)
<?php
class Myform extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim');
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else{
$user = $this->input->post('user');
$company = $this->input->post('company');
echo '<pre>';
print_r($user);
print_r($company);
$this->db->insert("User_Inputs", $user);
$this->db->insert("companies", $company);
redirect("/myform/successmessage");
}
}
public function successmessage(){
$this->load->view('formsuccess');
}
public function getDatabase(){
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve(){
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>". $data['user']->email."</h1>";
die();
}
public function edituser(){
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h3>Showing results for ID #". $data['user']->id."</h3>";
die();
/* $data = array($id);
$this->db->where('id', $id);
$this->db->update('users', $data); */
}
}
Table View (adminlogin.php)
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach($users as $u){
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7){
$class = "btn btn-danger";
$button = "Deny";
} ?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="/ci/index.php/myform/edituser?user_id=<?php echo $u->id;?>">Edit</a></td>
<td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Edit View (editfields.php)
<html>
<head>
<title>Edit User Info</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong><?php echo $id; ?></p>
<strong>First Name</strong><input type="text" name='first_name' value="<?php echo $first_name; ?>"?>"/><br/>
<strong>Last Name</strong><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>
<strong>Email</strong><input type="hidden" name='email' value="<?php echo $email; ?>"/><br/>
<input type="submit" name="submit" value="Update">
</div>
</form>
</body>
</html>
The most basic, fundamental example I want to achieve at this juncture is at the following, though this example may not fit according to my code:
Upvotes: 0
Views: 799
Reputation: 142
OK, for ajax see the below modifications:
I've added jquery library and bootstrap library,
adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach ($users as $u): ?>
<?php
// added approve class
$class = "approve btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = " deny btn btn-danger";
$button = "Deny";
}
?>
<!-- id is added to each row to uniquely identify them so we can change it's color-->
<tr id="row_<?= $u->id ?>"style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<!-- data-id was added to store the user id -->
<td><a class="<?php echo $class; ?>" data-id="<?= $u->id ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script>
$('.approve').on("click", function (e) {
// prevent the browser to loading the page directly
e.preventDefault();
// get the user id from the button we clicked.
var userid = $(this).data('id');
//initiate ajax request
$.ajax({
url: $(this).attr('href'),
method: 'POST',
success: function (data) {
// on success add the bootstrap success class to the row
$('#row_' + userid).addClass('success')
},
error: function (data) {
// on error add boorstrap dange class to the row
$('#row_' + userid).addClass('danger')
}
});
});
</script>
</body>
</html>
for testing purposes i've changed the myform/approve function
public function approve()
{
//do something this is for testing
$result = true;
if ($result)
{
// success you have done was OK and we send back success code
$this->output->set_status_header(200);
} else
{
// failure we send back an 4* cient error code
$this->output->set_status_header(400);
}
}
Upvotes: 1
Reputation: 142
I've updated my answer with the full test code for controller and views:
Myform.php
<?php
class Myform extends CI_Controller {
public function __construct()
{
parent::__construct();
// Added form and url helper so all functions can use them.
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim|valid_email');
// The only reason i store the values in this variable is to
// allow the re-use of the same form for both index and edituser
// functions. This allows the re-population rule to work.
$form['dbValue'] = [
'first_name' => $this->input->post('user[first_name]'),
'last_name' => $this->input->post('user[last_name]'),
'email' => $this->input->post('user[email]'),
'id' => $this->input->post('id'),
];
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform', $form);
}
else
{
// I've disabled company here as i could not find any reference to it
// in the form you have given as an example
$user = $this->input->post('user');
// $company = $this->input->post('company');
echo '<pre>';
print_r($user);
// print_r($company);
if ($this->input->post('id') !== '' && $this->input->post('id') !==NULL)
{
// id is defined so we update the user
$this->db
->where('id', (int) $this->input->post('id'))
->update("User_Inputs", $user);
}
else
{
// id is not define so we create the user
$this->db->insert("User_Inputs", $user);
}
// $this->db->insert("companies", $company);
redirect("/myform/getDatabase");
}
}
public function successmessage()
{
$this->load->view('formsuccess');
}
public function getDatabase()
{
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve()
{
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>" . $data['user']->email . "</h1>";
die();
}
public function edituser($user_id = NULL)
{
// Just making sure we are capturing the id as an integer
$user_id = (int) $user_id;
// Get the user based on the $user_id
$data['user'] = $this->db
->where('id', $user_id)
->get("User_Inputs")
->row();
// We will use the below variable to set the form fields to the correct values.
$form['dbValue'] = [
'first_name' => $data['user']->first_name,
'last_name' => $data['user']->last_name,
'email' => $data['user']->email,
'id' => $user_id
];
$this->load->view('myform', $form);
}
}
View: adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php
foreach ($users as $u)
{
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = "btn btn-danger";
$button = "Deny";
}
?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<td><a class="<?php echo $class; ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
View: myform.php
<?php
// if the set_value default values are not set set them to NULL
echo form_open('myform');
echo form_label('First Name', 'first_name');
echo form_input('user[first_name]', set_value('user[first_name]', isset($dbValue['first_name'])) ? $dbValue['first_name'] : '' );
echo form_label('Last Name', 'last_name');
echo form_input('user[last_name]', set_value('user[last_name]', isset($dbValue['last_name'])) ? $dbValue['last_name'] : '' );
echo form_label('Email', 'email');
echo form_input('user[email]', set_value('user[email]', isset($dbValue['email'])) ? $dbValue['email'] : '' );
echo form_hidden('id', set_value('id', isset($dbValue['id'])) ? $dbValue['id'] : '' );
echo form_submit('Submit', 'submit');
echo form_close();
echo validation_errors();
Upvotes: 1