Laravel 5 Form Submit to View - php

I'm recoding my website and moving over to Laravel but for some reason I can't figure out how to have a search form that redirects you to a profile page.
<form class="navbar-form navbar-right search" role="search" action="/profile/" method="GET">
<div class="input-group">
<input type="text" class="form-control navbar-player-search" placeholder="Enter IGN here" autocomplete="off" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-default">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
I have a route already set up which works if I went to example.com/profile/Callahan
Route::get('/profile/{Profile}', 'PlayerController#getStats');
but I need the form to do that same thing.
Is it possible to do this with default laravel 5.4.3 or do I need something like https://laravelcollective.com/docs/5.3/html?

I don't understand exactly the information which the input contains, but I'll consider this:
The user will write in the input and click on search.
Then, in your controller, you'll get the profile name after the search and redirect to the profile. Is that the case?
If yes, you should do something like that:
Routing
Route::get('/profile/{Profile}', 'PlayerController#getStats');
Route::get('/search', 'PlayerController#searchForm');
Route::post('/search', 'PlayerController#search');
Controller
public function searchForm() {
return view('search');
}
public function search(Request $request) {
// get the input
$inputText = ...;
// logic to get the profile name
$profileName = ...;
return redirect('/profile/'.$profileName);
}
View
<form class="navbar-form navbar-right search" role="search" action="/search" method="POST">
<div class="input-group">
<input type="text" class="form-control navbar-player-search" placeholder="Enter IGN here" autocomplete="off" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-default">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
Once again, I don't know if it's what you really want, but if it's not, let me know to try help you again.

Related

Laravel: getting another id from form after submitting// rating functionality for rentals

Route
Route::post('/review/{pId}','RentalController#review');
Controller:
public function review(Request $request,$propId){
$review = new Reviews();
$rpId = rand();
$review->rvid=$rpId;
$review->usid_fk = Auth::user()->uid;
$review->prId_fk = Property::find($propId);
$review->comment = $request->input('comment');
$review->rating = $request->input('rating');
$review->date = Carbon::now();
$review->save();
}
PHP
<form action="/review/{{ $prop->pid}}" method="POST">
{{csrf_field()}}
<div class="modal-body">
<input type="hidden" name="propid" value="{{ $prop->pid }}"/>
<input id="rating" name="rating" class="rating rating-loading" data-show-clear="false" data-min="0" data-max="5" data-step="1" value="0">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Comment</span>
</div>
<textarea name="comment" class="form-control" aria-label="Comment">
</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I am getting "page not found", so I don't know whether should I use "put" or "post" method in route as well as in form .
Your replies would be highly appreciated.
I think the problem is from the action in your form.
change your form to
<form action="{{route('review.post',['id'=>$prop->pid])}}" method="POST">
</form>
and change your route to
Route::post('review/{pId}','RentalController#review')->name('review.post');
Inspect the form from browser and make sure csrf token and action url are well formed.

Octobercms - CSRF Protection on a form to preventing multiple submission

I have a component with a form and then code to handle the submission. The problem is, if I spam the button it will submit the form multiple times. A solution to this would be to add a disabled state via Javascript to prevent the user from doing this.
From a security reason I do not want this to happen at all and prevent server side. I have a token and in the config I have CSRF enabled
'enableCsrfProtection' => true,
Here is my form
{{ form_open({ request: 'onSubmit' }) }}
<div class="form-group">
<label class="control-label" for="subject_name">Subject's name</label>
<input id="subject_name" name="subject_name" type="text" placeholder="" class="form-control input-md" required="">
</div>
<div class="form-group">
<label class="control-label">Postcode</label>
<input id="postcode" name="postcode" class="form-control input-md" type="text" required>
</div>
<div class="form-group">
<button id="submit" type="submit" value="submit" class="btn btn-lg btn-gold pull-right"><i class="fa fa-lock"></i> Request Job</button>
</div>
</form>
and in the generated html
Handler/ Session / Token
<input name="_handler" type="hidden" value="onSubmit">
<input name="_session_key" type="hidden" value="7Eg9bK4pcT2NOgWwUS0UFUckjkSMRC1UDBkBhPwO">
<input name="_token" type="hidden" value="00nbkK3EAo2I8WGWSh85qkMjHYig6aldrd3oe8HZ">
Then in my components code
public function onSubmit()
{
$name = post('subject_name');
$postcode = post('postcode');
if (Session::token() != Input::get('_token'))
{
/* Invalid token */
return print('Token invalid');
}
$job = new Job;
$job = $job->name = $name;
$job = $job->postcode = $postcode;
$job->save();
}
Yet I spam the submit button and it will execute multiple times. How can I add the ability to only execute once?
What you need is a loader
https://octobercms.com/docs/ui/loader
<div class="loading-indicator-container">
<button id="submit" type="submit" value="submit" data-load-indicator="Saving..." class="btn btn-lg btn-gold pull-right"><i class="fa fa-lock"></i> Request Job</button>
</div>
There are numerous solutions to what you want done.
My suggestion the most eloquent solution is a redirect or an ajax update. Communicate with your users by replacing the form with a message that says your "Request has been submitted. Thank you." This will stop them from being able to spam the submit button.
You could do something with the session like advised by Magnus. Make sure to include a timestamp so it can be removed after some time.

Form clears parameter from given action url - PHP Laravel

<form role="search" method="get" action="{{ route('people.search', array_replace(Request::all(), [" type" => "customers"])) }}">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search...">
<div class="input-group-btn">
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
The following form action link looks like this:
http://127.0.0.1:8080/people/search/customers?countries=Germany&statuses=3&page=1
But when I submit the search form, it redirects to:
http://127.0.0.1:8080/people/search/customers?search=test
Instead of including the parameters that were previously set.
Is there a way to prevent that?
What I did to include the previous parameter is just by adding them as hidden fields:
#foreach(request()->query() as $key => $value)
#if($key == "page") // Don't want the page parameter to be in there
#continue
#endif
<input type="hidden" name="{{$key}}" value="{{$value}}">
#endforeach

Submit Button Only Working on Index Page

Been having trouble with a form not submitting data to a page (Change Password) when on any page of the website apart from the index page (Which is working as it should). The website is built using CodeIgniter.
The code currently used on all pages for the form is the following:
<div class="modal-body">
<form role="form" id="change_password_form" action="<?php echo base_url('auth/change_password')?>" method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Old Password" name="old" type="password" autofocus>
</div>
<div class="alert alert-danger" id="old" role="alert" style="display:none"></div>
<div class="form-group">
<input class="form-control" placeholder="New Password - minimum 8 characters" name="new" type="password" value="">
</div>
<div class="alert alert-danger" id="new" role="alert" style="display:none"></div>
<div class="form-group">
<input class="form-control" placeholder="Confirm New Password" name="new_confirm" type="password" value="">
</div>
<div class="alert alert-danger" id="new_confirm" role="alert" style="display:none"></div>
<input type="hidden" name="user_id" value="<?php echo $user_id;?>" id="user_id" />
</fieldset>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="changepassword" value="Submit">Submit</button>
</form>
</div>
On the index page, this successfully goes to the change_password function and will check the details to see if they meet the rules. An error message is shown below the text box when submit is clicked. However on other pages using the same code, nothing happens when submit is clicked. when I go into Main.php and change the page of index.php in the index function to any other page (view_inventory.php for example), The change password works correctly on the new index page, but will not work on the old index page.
I can get the change password to work on other pages if I replace the submit button with the code below (Doesn't show the results under the text boxes, but on a new blank page), but I didn't really want to have to edit so many pages if it can be fixed in a smaller amount.
<input type="submit" value="Submit" class="btn btn-primary">
Any help would be gratefully appreciated.
I had the same error last week. you have to use a Submit inside form instead of button.
You can use form_submit for example:
echo form_submit('mysubmit', 'Submit');
// Would produce: <input type="submit" name="mysubmit" value="Submit" />
Here is the documentation:
http://www.codeigniter.com/user_guide/helpers/form_helper.html
Regards.

using a button to add data from a text input with angular

I'm using angular with a MySQL backend.
I think I'm missing something regarding ng-model.
When I click add new idea, nothing happens. Any ideas. I'd had it working, but then I changed some things, so I know it's registering to the database. Additionally, when it did work, it would only update without a refresh occasionally. My main goal is getting the adding working again, but if there's something else blatantly wrong, please feel free to give me a hard time.
thanks, all.
html
<form>
<div>
<input type="text" class="form-control" name="idea" ng-model="ideaInput">
<button class="btn btn" type="submit" ng-click="addIdea(ideaInput)">Add
New Idea</button>
</div>
</form>
php
<?php
require_once 'db.php'; // The mysql database connection script
if(isset($_GET['idea'])){
$task = $_GET['idea'];
$status = "0";
$created = time();
$query=mysql_query("INSERT INTO ideas(idea,status,created_at) VALUES ('$idea', '$status', '$created')") or die(mysql_error());
}
js controller
app.controller('ideasController', function($scope, $http) {
getIdea(); // Load all available ideas
function getIdea(){
$http.get("ajax/getIdea.php").success(function(data){
$scope.ideas = data;
});
};
$scope.addIdea = function (idea) {
$http.get("ajax/addIdea.php?idea="+idea).success(function(data){
getIdea();
$scope.ideaInput = "";
});
};
?>
This is what used to work.
<div class="col-sm-3">
<button ng-click="addNewClicked=!addNewClicked;" class="btn btn-sm btn-danger header-elements-margin"><i class="glyphicon glyphicon-plus"></i> Add New Idea</button>
</div>
<div class="col-sm-3">
<input type="text" ng-model="filterIdea" class="form-control search header-elements-margin" placeholder="Filter Ideas">
</div>
</div></div>
<div class="widget-body ">
<form ng-init="addNewClicked=false; " ng-if="addNewClicked" id="newIdeaForm" class="add-idea">
<div class="form-actions">
<div class="input-group">
<input type="text" class="form-control" name="comment" ng-model="ideaInput" placeholder="Add New Idea" ng-focus="addNewClicked">
<div class="input-group-btn">
<button class="btn btn-default" type="submit" ng-click="addIdea(ideaInput)"><i class="glyphicon glyphicon-plus"></i> Add New Idea</button>
</div>
</div>
</div>
</form>
try to add your controller reference in the div:
<form>
<div ng-controller="ideasController">
<input type="text" class="form-control" name="idea" ng-model="ideaInput">
<button class="btn btn" ng-click="addIdea(ideaInput)">Add
New Idea</button>
</div>
</form>

Categories