Laravel 5 Can't send data from url - php

Hi Im currently trying to make a basic crud for my questions resource on laravel 5
so far so good, but now Im having troubles displaying the edit view, because the url is not being created correctly when I try to send the resource id in the url
here's the anchor Im using
<button class="submit-form button button btn btn-primary" style="margin: 0 1em;" type="submit">Editar</button>
here's the route in my routes file
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin/questions/update');
the method in the controller works just fine, when I manually type this url
/admin/preguntas/editar/4
It shows the view without problems, but when I go from the anchor the url it goes is this one
/admin/preguntas/editar?4
of course the 4 is the id from my resource, but why is not typing the correct url?
thanks in advance

You can't wrap a hyperlink around a button so my assumption is your problem is related to the form action (since button type is submit), not the a href.
Since it appears you're using bootstrap, there is no need to use a button to get the styling of a button.
<a class="btn btn-primary"> will work just fine.

And your "QuestionsController#edit" accepts "id" argument?
like
function edit($id){}
And I think, your are using wrong route link helper
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);

First write proper link button as Devon said:
Editar
Second you must write proper route name format:
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin.questions.update');
Hope it will help you to solve the problem :)

Related

Best way to keep pagination after editing a record

I'm very new to Laravel and I'm trying to make a crud application with a view for each db table that has a list with edit buttons on each record.
So far I have the paginated list which is working good and an edit view that is working as well.
The problem I'm facing is that whatever page I'm in I can click the edit button and edit the record successfully, but when I save the record I always go back to page 1.
I don't know and I'd like to know which is the correct (best) way to achive this.
Should I use session? Or maybe append the page parameter to the edit form action? Or else?
EDIT:
this is what edit method returns:
return redirect('/lyrics')->with('success', 'Lyric updated!');
Thank you for any help that points me in the right direction.
You can pass the page parameter to the edit page and then after the user update the record, you can redirect to the page you were in.
Something like this:
In the link where you call the edit page:
Edit
In your edit form:
<form>
... fields
<input type="hidden" name='page' value={{ app('request')->input('page') }}
</form>
Then in your update method at the controller
public function update($request)
{
// update
return redirect('homepage', ['page' => $request->page]);
}

how to put two parameters by jquery in a link to use by codeigniter

i want to put two parameters by jquery in href of a link and then use by codeigniter, my code is below:
$("#user_comment_show").append("<li>"+...+'<a id="remove_link" href="<?php echo base_url().'my_site/remove_user_comment/';?>'+data.user_email_address+"/"+data.user_text+'">'+.....+"</li>"
i see data.user_email_address and data.user_text in source page but by select a link codeigniter shows me an error :
The URI you submitted has disallowed characters.
what's wrong with code and how to send these two parameters to my public function in codeigniter by jquery in a link. i appreciate to help me solve my problem
data.user_email_address && data.user_text find by ajax in database
my a link in source page
change into like this
$("#user_comment_show").append("<li><a id='remove_link' href='<?php echo base_url()?>/my_site/remove_user_comment/'+data.user_email_address+'/'+data.user_text+'>Your Link Name</a></li>")
hopefully this will work fine.

Pass a PHP variable in a link to a Modal box

Hi i have a html link to a modal box which works perfectly, now what I am trying to do is pass a variable to the form in the modal box, how ever the link shows but nothing happens once it is clicked.
Here is my normal code:
Add
Here is my PHP code
echo "<a href=\"#accSettings1?ip_address={$ip_address}\" class='btn btn-small btn-primary hidden-tablet hidden-phone' data-toggle='modal' data-original-title=''>Add</a>";
Use your normal code and add PHP only when you need a PHP variable:
Add
if it's your application that doesn't work - then debug it first.
Use handwritten example to exercise with. Make it work. And then add a dynamical part using PHP to fill the variable. After that you have to verify if dynamical code produced the same result as a static one. To do that instead of hover you have to inspect page source and compare it with original code. Find the differences and correct them.
To me, such an url like #accSettings1?ip_address=value looks quite unusual. ?ip_address=value#accSettings1 looks more familiar to me. Though I am not a JS pro, nor I know your app internals and URLs intention

Codeigniter url gets appended every click

I have a problem with CI whenever i click a button in a form which has an action of image/upload or a hyperlink with the same link it gets appended whenever i click it the second time. say for example my home is localhost/admin and i click a button or a link which has image/upload.. so the url will now beh localhost/admin/image/upload but when i click the same button the second time the url will now beh calhost/admin/image/image/upload wchich well then cause a 404 error which ofcourse is the error given that the page is not found by just seeing that url. it gets appended every time i click the button or the link.
Anyone of you knows this please do share!
UPDATES:
BTW just a headsup for all those people who didn't know or who encountered this problem.. USE anchor or any helper in CI becuase if you manually put links in href or actions on form tag without putting the base_url.. your URL will be messed up.. helpers do append base_url. :D
Check out the Codeigniter docs and the URL Helper. That should help out.
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
I know for anchors you would just do:
echo anchor('image/upload', 'Upload');
This will append the url to the base url and you don't have to worry about changing anything or any 404 errors.
just put http:// at infront of your link.
or otherwise
change your config file:
$config['base_url'] = 'http://www.yourhost.com/home';
try to link the url as '/path-to-url'. notice '/' before url.
Link
currently you might be doing like below
Link
Also try changing
$config['base_url'] = 'http://localhost/home/';

Calling a php function with the help of a button or a click

I have created a class named as "member" and inside the class I have a function named update(). Now I want to call this function when the user clicks 'UPDATE' button.
I know I can do this by simply creating an "UPDATE.php" file.
MY QUESTION IS :-
Can I call the "update() function" directly without creating an extra file? I have already created an object of the same class. I just want to call the update function when the user clicks on update button.
an action.php example:
<?
if (isset($_GET[update])){
$id=new myClass;
$id::update($params);
exit;}
//rest of your code here
?>
Your button is in your view. Your method is in your class . You need a controller sitting in the middle routing the requests. Whether you use 1 file or many files for your requests is up to you. But you'll need a PHP file sitting in the middle to capture the button click (a POST) and then call the method.
As far as I know you can't do this without reloading your page, checking if some set parameters exist which indicate the button is clicked and than execute the button. If this is what you are looking for... yes it is possible on page reload. No as far as I know it is not possible directly because your php-function has to parse the results again.
Remember that a Model-View-Controller way is better and that this will allow you to ajax (or regular) requests to the controller-class.
You do it on the same page and have an if statement which checks for the button submission (not completely event driven) like so
if (isset($_POST['btn_update']))
{
update();
}
<input type="submit" name="btn_update" value="Update" />
That will have to be wrapped in a form.
Or you could do it with AJAX so that a full page refresh isn't necessary. Check out the jQuery website for more details.

Categories