Unexpected URI segment in codeigniter - codeigniter-2

I am accessing user profile page with this type of url
foo.com/index.php/controller/view_profile/user_id
its working fine. In the profile page I have different segement of the profile and using buttons for them.
if I click on a button which should take me to user financial data, its redirects to
foo.com/index.php/controller/view_profile/user_finance/user_id
instead of
foo.com/index.php/controller/user_finance/user_id
in the button link i just used following code
echo '<button class="btn btn-info btn-large">Financial Information</button>';
What am I doing wrong? Thanks in advance.

Please use the following to generate the code for the link.
$array = array(
'class' => '<button class="btn btn-info btn-large">'
);
echo anchor('controller/user_finance/' . $user_id,"Financial Information",$array);
EDIT:
The above link you are using wont work as it needs full url which anchor helper will generate.

Related

Delete function using href="action.php" method

im using codeigniter 3 to delete a user, the view file its like this
<a href="action.php?id=<?=$row[TB_PREFIX.'id']?>" class="btn btn-sm btn-danger popovers deletar" type="button" data-content="Excluir" data-trigger="hover" data-placement="right" data-toggle="tooltip" >
<i class="fa fa-trash-o"></i>
</a>
it calls a file using href attribute, and the action.php file is a switch case logic, which calls ''deletar'' case to delete the user , the code that does it's
case 'deletar':
if(!is_null($id)){
$id = str_replace("#", "", $id );
$obj_sql->Query("DELETE FROM ".TB_ATUAL." WHERE ".TB_PREFIX."id = '".$id."'");
$return["status"] = 'success'; // ['ok', 'info', 'erro']
$return["message"] = '<b>Registro deletado com sucesso!</b>';
}
break;
and its returning to a full blank page with null. i dont know how to make it work.
i expect to delete the user and back to the current page with the user deleted from database and the page. thanks to everyone who try to help me with this.
i know its not mvc pattern and im freelancer to this project.

Showing notification with AJAX

I have a navbar on my users' panel. A part of the navbar indicates if the user has a new unread message. In this case a badge will appear next to an icon. I've simplified the codes here to make them easier to understand and read.
So this is the simplified HTML code:
<div class="btn-group msg-box">
<i class="fa fa-envelope"></i>
// this is the default state, no badge is shown
</div>
Here is the AJAX request which calls a custom function every 10 seconds:
<script type='text/javascript'>
$(document).ready(function(){
setInterval(checkMsg,10000);
});
function checkMsg(){
$.get('ajax.php',{user_id : <?php echo $user_id; ?>},function(data){
$('.msg-box').html(data);
});
}
</script>
And this is the ajax.php file content:
if(isset($_GET['user_id']){
// a few lines of code here to check if that particular user has any unread message.
// In such case a variable name $newMessage is set to 1. Now ... :
if($newMessage>0){
$data='
<i class="fa fa-envelope"></i>
<span class="badge"><i class="fa fa-info"></i></span>
';
}else{
$data='
<i class="fa fa-envelope"></i>
';
}
echo $data;
}
First of all, I know the way I've written this AJAX request is very rookie, but it works fine anyway, up to one point!
In case the user has a new message, and if they stay on a page, the code runs perfectly and shows the badge. But when the user refreshes the page or goes to another page, even-though they have a new message, that default state is shown again where there's no badge. And I know it's of course because I have specified a default state via HTML codes.
I need to know how I can keep the result of the AJAX request regardless of how many times the user refreshes the page or goes to another page.
UPDATE
I tried storing the query result in a SESSION in my ajax.php file. So instead of $data I wrote $_SESSION['data'].
Back on my HTML I made the following change:
<div class="btn-group msg-box">
<?php
if(!isset($_SESSION['data'])){
?>
<i class="fa fa-envelope"></i>
<?php
}else{
echo $_SESSION['data'];
}
?>
</div>
I made this change because I considered the fact that SESSIONS, by definition, are created and accessed globally within the domain. So once it's set, it can be checked and used on all other pages.
So that only if that SESSION isn't set, the default state should be displayed. But that as well doesn't seem to have my desired result. Still the same thing happens.
Ok, answering my own question now. :)
My UPDATE seemed to be a good idea which I tried.
The problem there was that I had written session_start(); on my main PHP file which was included in all other PHP files of the project.
So I basically thought that when the ajax.php file is called, there's no need to write session_start(); again. Because ajax.php was called inside a PHP file that had session_start(); in it already. So, I was wrong!
Adding session_start(); to the beginning of my code in ajax.php simply fixed the issue.

How To Hidden Password in URL

i dont know how hidden password on url
i got problem like this http://127.0.0.1:8000/bulletin/%201/edit?passwordC=11111&page=1
My View
<form>
<div class="form-row" style="specified-width:200; position: absolute; bottom:0; margin-bottom:10">
<input style="width:150px" type="password" placeholder="Password" name="passwordC">
<input type="hidden" value="{{$buletin->currentPage()}}" name="page">
<button style="margin:0 5px" formAction="/bulletin/ {{ $tampil_B->id }}/deleteOper" type="submit" class="btn btn-danger">Delete</button>
<button formAction='(url(edit))' type="submit" class="btn btn-primary">Edit</button>
</div>
</form>
My Router
route::get('/bulletin/{id}/edit','BulletinController#edit');
my controller
public function edit (Request $request, $id)
{
$buletin = \App\Dashboard::find($id);
$url = "/?page={$request->page}";
if(is_null($buletin->password)){
$request->session()->flash('failed', 'Cant Edit Because this post not had been set password ');
return view('bulletin.edit_nopass', ['buletin' => $buletin,'url'=> $url]);
}
if (hash::check($request->passwordC,$buletin->password)){
return view ('bulletin.edit', ['buletin' => $buletin, 'url'=> $url]);//save and go back to card
} else {
$request->validate([
'passwordC' => 'required|required_with:password|same:password'
],[
'passwordC.required_with' => "Password not match",
'passwordC.required' => "Password Required",
'passwordC.same' => "The Password You Entered Do Not Match.Please Try Again"
]);
}
The issue is a byproduct of how you have written this solution. To remove the password from the URL, you will have to find a different mechanism to get to the edit page.
As it currently stands, you are doing a GET request to the edit page from the form, and because it is a GET request, the form parameters are sent in the URL.
From the edit controller method you are then returning a view, so the URL is never re-written.
That is why you have this problem, as to how you could solve this, there are many options; you could post to an endpoint that stores the approval in a session that you then check in middleware, or in the controller, and then return the view. You could use the reconfirm password middleware from Laravel. Or even a POST-REDIRECT-GET pattern, where you post the form and then redirect to the edit page from there with whatever you need to do to protect the edit endpoint.
There are many options, but its impossible to tell you how to solve this problem given that you need to rethink how you will solve it.
First of all it is not correct to send with GET .But if it is very vital you have two way:
1.use encrypt .but it is not safe too.because there is even online sites that can decrypte .
2.use Hash:make . Hashing is an unilateral.It means that you can not dehash it

How do I pass a variable to a controller function from the view in CodeIgniter?

I have a user controller with an agentexport function, which is supposed to download an excell spreadsheet. Below is the function:
function agentexport($agentName) {
if($this->isAdmin() == TRUE) {
$this->loadThis();
}
else {
$this->excel->setActiveSheetIndex(0);
// Gets all the data using agent name
$data = $this->excel_model->getdatabyname($agentname);
//print_r($data);
//die;
$this->excel->stream('crosstown.xls', $data);
}
}
In my views I am trying to execute the above function with the following button:
<a class="btn btn-sm btn-info" href="<?php echo base_url().'agentexport/'.$record->agentName; ?>" title="Download Sheet><i class="fa fa-pencil"></i>Download Sheet</a>
The above button is meant to download the spreadsheet right away.
The url is defined in my routes as :
$route['agentexport'] = "user/agentexport";
Did I define my route the right way ? When I click on the route I get the following url
http://www.XXXXX.com/John%20Grisham.
As you can see, the name is appended at the end of the url but the page shows a 404. What am I doing wrong ?
Personal opinion here, but I don't think there's any strong reason to use a route. If nothing else, the following will be a good experiment to see if the $route definition is the problem.
Delete the $route you have been using for 'agentexport'.
Change the link to
<a class="btn btn-sm btn-info" href="<?= base_url('user/agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
To test that the link works and is passing the value, use the following version of agentexport
public function agentexport($agentName)
{
echo $agentName;
//or alternately
//var_dump($agentName);
}
It is assumed that you verified $agentName is a usable value before you used it in the link. If the above shows you a value, then you know the $route was the problem.
You can experiment to find a $route, but $route['agentexport/(:any)'] = 'user/agentexport/$1'; should work. If you're going to switch back to using a route don't forget to revert the link code. I'd write it like this, where the URI is passed as an argument to base_url.
<a class="btn btn-sm btn-info" href="<?= base_url('agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
If you find a route that works - and using a route is what you really, really want - then restore the code in agentexport to what you actually need. But again, I don't see any strong reason to obfuscate the link's URL.
If, from the view, you're pointing to /controller_name/method/agent_name (which is basically what you're doing after the route "translation", all you need to do is pick the agent name from the URI using the URL helper (remember to load it beforehand)
$user_id = $this->uri->segment(3);
The above will take whatever is in the third segment of the URI (agent_name in my example) and assign it to the $user_id variable.
Remember that whatever is possible to be manipulated by the user cannot be trusted, so you need to sanitize $user_id and make sure that the user requesting the file is allowed to access it

laravel backup raising 404 to return a view

In short what is happening is, when I try to access a route, Laravel shows a 404 error, with a short description "Page not Found, No query results for model [App\Models\Product] teste ".
Inside of my model I have the method "teste" which invokes a route.
public function teste($crud = false)
{
return '<a class="btn btn-xs btn-default" href="product/teste" data-toggle="tooltip" title="Just a demo custom button."><i class="fa fa-search"></i> Add Item</a>';
}
button image in the datatable line
Adding a button inside of ProductCrudController
$this->crud->addButton('top', 'bteste', 'model_function', 'teste', 'beginning');
Inside of custom.php I have tried already:
CRUD::resource('product/teste', 'ProductCrudController#teste');
Route::get('product/teste', 'ProductCrudController#teste');
Inside of my Controller I have my method named teste
public function teste(){
return view('vendor/backpack/crud/teste');
}
And finally inside of this path I have my view "which is pretty simple" and only retues a simple hello.
What is the purpose
I need to build a form which allows the user to add "a component" for the product once that the product is always customized. This means, a combination of components makes up a new product.
What I need is: Add a new button in the product line which when clicked gonna redirect the user to add all components which makes up this product. Same idea of an order which contain items.
I could not find by myself the possible options to fit my need.
Is that possible to be done by backpack? If so is there any example to be followed?
Thanks

Resources