I created three different forms they need to be separated because they are collapsed in different buttons. It happens that I would like my controller to receive data from the three forms when I click on a submit button on the third form. Is it possible to receive input data from all forms with a single submit in Laravel?
Each form is inside a button identified by a data-target. Example:
<div class="container" id="myGroup">
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#entidade" role="button" aria-expanded="false" aria-controls="entidade">
Entidades
</button>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#dirigente1" role="button" aria-expanded="false" aria-controls="dirigente1">
Dirigente 1
</button>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#dirigente2" role="button" aria-expanded="false" aria-controls="dirigente2">
Dirigente 2
</button>
<form class="collapse" id="entidade" data-parent="#myGroup" method="post" action="/entidades/store">
.
.
</form>
<form class="collapse" id='dirigente1' data-parent="#myGroup" method="post" action="/entidades/store">
.
.
</form>
<form class="collapse" id='dirigente2' data-parent="#myGroup" method="post" action="/entidades/store">
.
.
</form>
</div>
Controller:
public function store(Request $request){
}
You just have to put all your forms into one, and that means have only one form tag that will send a POST request to your controller.
I would suggest implementing the logic in your controller that would handle empty fields and save only those fields that are not empty (i.e. if you leave first two forms blank and submit only the third one)
You can still keep all 3 submit buttons, but make sure to merge all forms into one, so you have only one form tag in a page like this:
<form method='POST' action='...'>
...form 1 inputs...
...submit button 1
...form 2 inputs...
...submit button 2
...form 3 inputs...
...submit button 3
</form>
thanks guys, I solved the problem leaving everything in a single form.
Related
I am using Laravel 7 and am stuck on trying to initiate a Controller method via a button in my view. I am using a form with a simple a tag but am not sure if how I am going about it is the best way. Besides, I am getting an error withe the way I am attempting it. The error is:
Too few arguments to function App\Http\Controllers\TasksController::finished(), 1 passed in C:\laragon\www\taskapp\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
I am truly stumped on how to call a method from a button click. What I am trying to do is (when the button is clicked) change the status of an item in my db called $task->task_status to "Complete".
In my table in the home view, I have a form like this:
<td>
<form method="POST" action="/tasks/{{$task->id}}">
{{ csrf_field() }}
{{ method_field('POST') }}
<div class="ml-5">
<i class="fa fa-check"></i>
</div>
</td>
In my web.php, I have:
Route::get('/finished/{id}', 'TasksController#finished')->name('finished');
And in my TasksController, I have the following:
public function finished(Request $request, $id) {
$task = Task::find($id);
dd($task);
$task->task_status = $request['task_status'] = 'Completed';
$task->save();
return redirect('home')->with('success', 'Task Completed and now viewable in Completed Tasks!');
}
If anyone can show me a better way of accomplishing this (without using axaj) I would really appreciate it.
Thank you in advance.
I ended up fixing this in using the following code:
in my web.php, I used:
Route::get('/finished/{id}', 'TasksController#finished');
in my home.blade.php, I have:
<td>
<form action="finished/{{$task->id}}" method="GET" style="display: inline" class="">
#csrf
#method('POST')
<div class="ml-5">
<button type="submit" class="btn btn-sm btn-success">
<i class="fa fa-check"></i>
</button>
</div>
</form>
</td>
And in my TasksController, I pretty much left that the way it was and it now works.
here is my code. I have a twig form
I want to access it using symfony WebTestCase crawler like this, but it doesn't work.
I want to use the Download button for testing purposes.
Does someone have a solution ?
<div class="modal-body">
{{ form(trackImageForm) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="$(this).prop('disabled', true);$('form[name={{ trackImageForm.vars.id }}]').submit()">Download</button>
</div>
i tried this
$form = $crawler->selectButton('Download')->form(array(...));
EDIT :
OK i can access to my form
{{ form_start(trackImageForm, {'attr': {'id': 'add_image_form'}}) }}
{{ form(trackImageForm) }}
i gave an id to my form and can n
$form = $crawler->filter('form#add_image_form')->form();
but will I be able to submit the form ?
As the documentation that you've referred to says:
The selectButton() method can select button tags and submit input tags. It uses several parts of the buttons to find them:
The value attribute value;
The id or alt attribute value for images;
The id or name attribute value for button tags.
There's no button in your code matching these conditions.
You should give it a name or id attribute and use it in you test.
#About your edit:
If it's not a <form>, you cannot submit it. That's pretty obvious. Instead of making the submit with JavaScript, just put these buttons inside form as they should be. Using JS to submit the form in you case makes no sense.
If you don't want to put the submits in you form class (which is correct), you can do that with something like:
{{ form_start(trackImageForm) }}
<div class="modal-body">
{{ form_rest(trackImageForm) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="$(this).prop('disabled', true);$('form[name={{ trackImageForm.vars.id }}]').submit()">Download</button>
</div>
{{ form_end(trackImageForm) }}
form_start prints <form> opening tag
form_rest prints all fields that have not been printed yet (which is all in your case)
form_end prints closing </form> tag
I am trying to load a page on click,
I created an email verification link which open the page and updates my Database all it works fine, but the page contains a button which i want to be clicked by user and open first page which have login window.
My verify.html:
<div class="container container-table">
<div class="row vertical-center-row">
<div class="text-center col-md-4 col-md-offset-4" style="background:blue">
<h3> Thankyou For Veryfing</h3>
<p>You Are Verified</p>
<form action="login.php/verify_to_login" method="post">
<button type="submit" class="btn btn-success" >Sign Up>Go To Login</button>
</form>
</div>
</div>
</div>
My Controller function which loads the verify page like this : http://localhost/index.php/login/verify/1567af19e10ce4
public function verify($VerificationCode){
$this->load->model('My_model');
$this->load->helper('url');
$this->My_model->verifymail($VerificationCode);
/* Send to the verification page */
$this->load->view("verify.html");
}
This is my verify_to_login() function which i want when i click on button it should open header.html, This should work like http://localhost/index.php/login/verify_to_login
but instead this it is showing http://localhost/index.php/login/verify/login.php/verify_to_login when i click on the button
public function verify_to_login(){
$this->load->view("header.html");
}
I am unable to understand why is this happening.?? :(
On html
<form method="" action="<?php echo base_url(); ?>controller_name/function_name">
<button id="submit-buttons" type="submit" >Submit 1</button>
</form>
And on Controller
function functionname(){
$this->load->view('some_view')
}
So basically your controller is fine, so just fix the button by putting the controller name and then the function name and all should work hopefully.
try
this
<button type="submit" class="btn btn-success" onclick="window.location.replace('YOUR_LINK_HERE');">Sign Up>Go To Login</button>
OR
<button type="submit" class="btn btn-success" onclick="window.location.href('YOUR_LINK_HERE');">Sign Up>Go To Login</button>
I have a list of users on my web page echoed using a PHP loop. The names are links look like this.
{$user['username']}
So, when I click on each user, I am taken to user.php page.
user.php displays each user's information based on the query parameter $user['id'] in the URL. Using $_GET['id'] method.
So, that's the current situation I have.
What I now want to do is, display user's information in a Bootstrap Model
So, how could I get that query parameter into the Bootstrap model. And do the same PHP processes to get the same functionality ?
If you have google this you can get number of results related to it.
Here is code seems to be referred from one of this question passing-data-to-a-bootstrap-modal
HTML
<a data-toggle="modal" data-id="ISBN564541" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value=""/>
</div>
</div>
Javascript
$(document).ready(function () {
$(".open-AddBookDialog").click(function () {
$('#bookId').val($(this).data('id'));
$('#addBookDialog').modal('show');
});
});
When i pass the hidden value to the php file it gives (true) 1 as a answer.
I am passing the value from the modal to the php file.
The span value was retrieved using jquery.
PHP CODE:
<?php
include "dbcon.php";
if(isset($_POST['pd_del']))
{
echo mysql_error();
$delid=isset($_POST['delidd']);
echo $delid;
}else
{
echo mysql_error();
}
?>
HTML CODE:
Form thats send the product id to the php file
<form name="prd_del" action="del_prod.php" method="post">
<div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="delModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">DELETE PRODUCT</h4>
</div>
<div class="modal-body">
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="pd_del" >Delete It!</button>
</div>
</div>
</div>
</div>
</form>
Your HTML :
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid">
Your JS :
$(".modal-body #delid").text( pd_del_id);
First problem is that you have 2 elements with the same id value (the sppan and the input field). You shouldn't.
Change one of your ids, something like that :
<h5>Do you want to Delete this Product ??? <span id="delid" name="delid"></span></h5>
<input type="hidden" name="delidd" id="delid_value">
And in your JS, if I understood what you want to do :
$(".modal-body #delid").text(pd_del_id); // Here you put the product's ID in your span to show it to the user.
$(".modal-body #delid_value").val(pd_del_id); // Here you put the product's ID in your hidden field to send it with your form.
Now, your PHP :
$delid=isset($_POST['delidd']);
echo $delid;
isset() function returns either true or false if the variable is set or not.
The variable $_POST['delidd'] is set (the hidden field is always sent to your PHP).
If you want to get the value (your product's ID) :
if (!empty($_POST['delidd'])) {
// The value is not empty
$delid = $_POST['delidd'];
} else {
// The value is empty : there is a problem (For example echo an error message or do whatever you have to do in that case.)
}