I have a list of cities in my database. There is an edit button that brings up a modal with a form where they can change the name of the city and submit. I'm having a hard time getting the city_id to pass from the edit button through the modal and to the controller. I get a "message undefined variable for the city_id on the line that tries to put the city_id in the form open code.
Adding a modal into the mix has made this confusing to me.
View Code
Edit
<!-- Modal for Edit -->
<div class="modal hide" id="editCityDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h3>Edit City</h3>
</div>
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/edit_city/'. $city_id); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<!-- jQuery for Modal -->
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
$('#editCityDialog').modal('show');
});
</script>
Controller Code
// Edit City
public function edit_city() {
$id = $this->uri->segment(3);
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}
Model Code
// Edit City
public function edit_city($city_id, $city_name) {
$data = array(
'city_name' => $city_name
);
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
You can use the city_id you already have and set the action with that:
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/edit_city/'+city_id);
$('#editCityDialog').modal('show');
});
if echoing <?php echo $c['city_id']; ?> works for the edit button, you should do that for the form_open as well like this
<?php echo form_open('/cities/edit_city/'. $c['city_id']); ?>
I would also recommend that if your edit_city() function is only ever called if there is a segment(3), then it is easier to do this:
// Edit City
public function edit_city($id) {
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}
Related
I am trying to a create a View which provides a summary table of various site members profiles - then each summary has a button, that once clicked, takes the user to that member's full profile. I can get the summary page to work properly, but I cant get the second part -where the button takes the user to the members full profile to work.
Here is my controller:
public function network_barre()
{
$this->load->model("Profiles_model");
$style ='barre';
$profilesubdata["fetch_data"] = $this->Profiles_model->fetch_data($style);
$this->load->view('templates/header');
$this->load->view('pages/page-network_barre', $profilesubdata);
$this->load->view('templates/footer');
Here is my Model "Profiles Model"
function fetch_data($style)
{
$this->db->select("username, email, style, about");
$this->db->from("profiles");
$this->db->where('style', $style);
$query = $this->db->get();
return $query;
}
Here is the view
<div class="container">
<div class="row d-flex justify-content-center card-top">
<?php
if($fetch_data->num_rows()>0)
{
foreach ($fetch_data->result() as $row)
{
?>
<div class="col-lg-4 col-md-6">
<div class="card card-warning wow zoomInUp mb-4 animation-delay-5">
<div class="withripple zoom-img">
<img src="<?=base_url();?>assets/img/demo/avatar4.jpg" class="img-fluid">
</div>
<div class="card-body">
<span class="badge badge-warning pull-right"><?php echo $row->style; ?></span>
<h3 class="color-warning"><?php echo $row->username; ?></h3>
<p><?php $string=character_limiter($row->about, 255, '…(More in Profile)') ; echo $string?></p>
<div class="row">
<div class="col text-center">
<!-- BUTTON -->
<form method="POST" action="<?php echo base_url(); ?>public_area/gotopublicprofile/<?php echo $row->email; ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<button type="submit" class="btn btn-raised btn-sm btn-warning" name="submit" value="submit"><i class="zmdi zmdi-account"></i>Profile</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php //this tag close out php tags above
}
}
?>
</div>
</div>
here is Controller gotopublicprofile
function gotopublicprofile()
{
$this->load->model("profiles_model");
$email = '$this->uri->segment(3)';
$profiledata["fetch_profiledata"] = $this->Profiles_model->fetch_profiledata($email);
$this->load->view('templates/header');
$this->load->view('pages/page-profilepublic', $profiledata);
$this->load->view('templates/footer');
}
And here is the fetch_profiledata function in the Model:
function fetch_profiledata($email)
{
$this->db->where('email', $email);
$query = $this->db->get("profiles");
return $query;
}
I always get these error messages:
Undefined property: Public_area::$Profiles_model
AND
Call to a member function fetch_profiledata() on null
I am sure I am just missing something simple, as lots of websites use this approach.
Thanks in advance!
So I figured it out..it was a combination of little things:
First
$email = '$this->uri->segment(3)';
became
$email = $this->uri->segment(3);
Thanks #kirb! and
function gotopublicprofile()
{
$this->load->model("profiles_model");
became
function gotopublicprofile()
{
$this->load->model("Profiles_model");
Thanks #Atal Prateek!
I'm trying to insert date from datepicker but the sql statement is not working imo.
here is the code:
if(isset($_POST['confirm']))
{
$order_finish_time = $_POST['order_finish_time'];
$note = $_POST['note'];
$finish_query = mysqli_query($link, "INSERT INTO orderdetails SET order_finish_time=$order_finish_time AND note=$note WHERE order_id=$order_id");
$order_r = mysqli_fetch_array($finish_query);
if(isset($order_r))
{
$result = mysqli_query($link, "UPDATE orderdetails SET order_status = 'Order_Finished' WHERE order_id=$order_id");
?>
<script>alert('Order confirmed!');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
else
{
?>
<script>alert('Something wrong');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
}
else part of the code is working and im getting something wrong message. Plese help me out if you can spot error in my code.
The form im using is:
<html>
<head>
</head>
<body>
<form class="form-horizontal" method="post">
<div class="row">
<div class="col-xs-12">
Order will be finished :
<input id="datepicker" class="form-control" name="order_finish_time" required/>
</div>
</div>
<button type="submit" name="confirm" id="confirm" class="btn btn-success btn-lg" style="width: 100%;">
<span class="glyphicon glyphicon-ok-sign"></span>
Confirm
</button>
</form>
<!-- datepicker -->
<script>
$( "#datepicker" ).datepicker({
dateFormat : 'yy-mm-dd'
});
</script>
</body>
</html>
here is the picture of my tables:
enter image description here
Your code seems to be incomplete in many ways. No jQuery and jQuery UI is defined in the head.
Your form is missing the field with the name text.
No database connection is made.
The variable $order_id is not defined in your code.
Use var_dump(mysqli_error_list($link)); after executing statements to see possible errors in your SQL. This is procedural style.
Your query is plain wrong. You cannot insert with a where clause, it is used for select, update and delete.
The correct insert query would be
$finish_query = mysqli_query($link, "INSERT INTO `orderdetails` (`order_finish_time`, `note`) VALUES ('$order_finish_time', '$note')");
Please explain what your program is supposed to do. It does not make sense the way it is written right now. This works, but since I do not know your intentions it may be -logically- wrong.
<?php
if (isset($_POST['confirm'])) {
$order_finish_time = $_POST['order_finish_time'];
$note = $_POST['note'];
$link = mysqli_connect('localhost', 'root', '', 'orderdetails');
$finish_query = mysqli_query($link, "INSERT INTO `orderdetails` (`order_finish_time`, `note`) VALUES ('$order_finish_time', '$note')");
$order_id = mysqli_insert_id($link);
if($finish_query === true)
{
$result = mysqli_query($link, "UPDATE `orderdetails` SET `order_status` = 'Order_Finished' WHERE `id`=$order_id");
?>
<script>alert('Order confirmed!');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
else
{
?>
<script>alert('Something wrong');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
}
?>
in my EditController I have indexAction :
public function indexAction()
{
$delete = new DeleteForm();
$view = new ViewModel(array('delete' => $delete ));
return $view;
}
and the view
<?php
$formDelete = $this->delete;
$formDelete->prepare();
$formDelete->setAttribute('action', $this->url(NULL, array('controller'=>'Register', 'action' => 'process')));
?>
<?php echo $this->form()->openTag($formDelete);?>
<div class="form-group">
<?=$this->formLabel($formDelete->get('reason')); ?>
<?=$this->formElement($formDelete->get('reason'))?>
<?=$this->formElementErrors($formDelete->get('reason'))?>
</div>
<div class="form-group">
<?=$this->formElement($formDelete->get('delete-button'))?>
</div>
<?php echo $this->form()->closeTag();?>
Despite I have action attribute set to RegisterController and processAction in page source I see always the same action. In setAttribute $this->url I can type whatever I want, in the code source always is
<form method="post" name="deleteForm" enctype="multipart/formdata" action="/App/public/element/edit" id="deleteForm">
The first parameter of the URL helper should be the name of the route you want to use, not NULL. By using NULL, ZF will fall back on using the current route, which I'd guess is not correct in your case.
I use Datatables to submit answers of questions from a survey. Each question is in new page, and the next is seen with the "Next" button. I want to have only one submit button and it has to appear in the end of all questions. In my code, it's under the table and is visible all the time. My biggest problem is that this submit button insert in db only the last question, but not all questions.
Please can you help me? :)
My view is:
<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#example').dataTable( {
"pagingType": "simple",
"lengthMenu": [[1], [1]],
"bSort": false,
"language": {
"paginate": {
"previous": true
}
}
} );
} );
</script>
</head>
<body>
<?php
echo form_open('index/survey_fill/' .$survey_id ); ?>
<table id="example" >
<thead>
<tr><th>Question</th></tr>
</thead>
<tbody>
<?php
echo validation_errors();
foreach ($survey as $row)
{
?>
<tr>
<td>
<?php echo "$row->question"; ?><br/>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '5'
);
echo form_radio($data);
echo " 5 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => ' 4'
);
echo form_radio($data);
echo " 4";
</td></tr>
<?php
}
?>
</tbody>
</table>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">
</form>
</div>
</body>
</html>
My model is:
public function survey_fill()
{
$answers=($this->input->post('answer'));
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('survey_answers', $data);
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
I'm thinking Datatables is quite a lot of overkill for what you're trying to do. Personally, I'd output this in a set of divs and do a simple forward-back type button with jQuery toggle, which is a LOT less overhead. There's also very low-overhead paging solutions out there that could suffice. Remember, datatables is not only reformatting the table, but it has an encyclopedia of API calls that are loaded, plus the data model it holds. That's a LOT for a show/hide type test.
Regardless, the solution is pretty straightforward. Wrap your container (whatever it is, datatable or divs) with a <form> tag. Put the submit button at the end, and show/hide or enable/disable it via whatever means you want. Datatables has an API call that can tell you the current page, and you could key off that to enable your button. Since the button will be before you close the <form> it will automatically submit.
Alternative, you could wrap your container in the form, build a jQuery .on('click') event that serializes the form and submits it via Ajax if that was desired. The key if you go this route is to preventDefault() so the form doesn't auto-submit as is standard for html forms.
1)how do i access the search $keyword inside the paginator partial to create search freindly urls ? clearly, passing the keyword as $this->view->paginator->keyword doesnt work.
2) currently, the search button's name is also send as param . for eg when searching for 'a' the url becomes http://localhost/search/index/search/a/submit//page/2. any way to stop this ?
Search Form:
<form id="search" method="get" action="/search">
<input id="searchfield" onClick="this.value=''" type="text" name="search" value="Search wallpapers"/>
<input id="searchbutton" type="submit" value="" name="submit"/>
</form>
Action inside searchController:
public function indexAction()
{
$keyword=$this->_request->getParam('search');
$alnumFilter=new Zend_Filter_Alnum();
$dataModel=new Model_data();
$adapter=$dataModel->fetchPaginatorAdapter("title LIKE '%".$keyword."%'", '');
$paginator=new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(18);
$page=$this->_request->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$this->view->keyword=$keyword;
}
index.phtml (view) file:
partialLoop('wallpaper/table-row.phtml',$this->paginator) ;?>
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml');?>
</div>
search-paginator-control.phtml file:
if ($this->pageCount){
$params=Zend_Controller_Front::getInstance()->getRequest()->getParams();
if(isset($this->previous)){
?>
Previous
<?php
}
else{
?> Previous <?php
}
foreach($this->pagesInRange as $page){
if($page !=$this->current){
?>
<?=$page;?>
<?
}
else{
echo $page;
}
}
if(isset($this->next)){
?>
Next
<?php
}
else{
?> Next <?php
}
}
The paginationControl view helper accepts a 4th parameter, that is a array of parameters, so in your case you could do something like this:
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml', array('keyword' => $this->keyword));?>
</div>
Then you access it inside your pagination using $this->keyword.