Passing select multiple on a form - php

I'm using codeiniter ,with Twig as template engine,and I have this select multiple on the template
<form action="{{ base_url }}mydirection/encuestas/perfil_interna" method="post" >
<select multiple="multiple" name="profile_questions">
{% for question in questions %}
<option id="qid_{{question.id}}">{{ question.text}}</option>
{% endfor %}
</select>
<input type="submit" value="Enviar">
</form>
this is the function on the controller
public function set_profile_internal_question(){
var_dump($_REQUEST);
}
But I get and empty array
and the route only work without the method
$route['mydirection/encuestas/perfil_interna']= 'Backend/Questions/set_profile_internal_question';
this way doesn't work
$route['mydirection/encuestas/perfil_interna']['POST']= 'Backend/Questions/set_profile_internal_question';
But its a form,post should work.

Related

Symfony 4.4 functional test delete form not working

I have an multi-delete functionality (kind of like PHPMyAdmin, checkboxes you can check to delete multiple items). The functionality itself works, but I'm implementing it in my functional test. However, somehow it doesn't work.
This is the code of my service (where multi-delete is handled, this is in a function that's called within controllers)
$form = $this->formFactory->createBuilder()->setMethod('DELETE')->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->deleteKeys($request);
return new RedirectResponse($this->session->get('referer'));
}
The function renders a template with the form, a button and a list of all items being deleted. This is the template:
{{ form_start(form) }}
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-body">
{{ form_widget(form) }}
<p>{{ 'standard.Are you sure you want to delete the following items'|trans }}?</p>
<p>
{% for item in items %}
<strong class="d-block">{{ item.displayProp }}</strong>
<input type="hidden" name="items[]" value="{{ item.id }}">
{% endfor %}
</p>
</div>
<div class="box-footer">
<input class="btn btn-danger" type="submit" name="delete_items" value="{{ 'standard.Delete'|trans }}">
<a class="btn btn-default" href="{{ app.session.get('referer') }}">{{ 'standard.Cancel'|trans }}</a>
</div>
</div>
</div>
</div>
{{ form_end(form) }}
So, testing it via my browser, it works completely fine. Then, lets look at the code in my test. The code to get to the confirm page works, it shows the right items selected to delete. But it contains a form:
$form = $crawler->selectButton('delete_items')->form(null, 'DELETE');
$crawler = $this->client->submit($form);
So, it selects the form based on the delete_items button name (which exists), it doesn't throw an error or something, it submits the form, but after it, getting the HTML of $crawler still displays the "deleted" items. Again, if I test the functionality in the browser in the module itself, it works, but in the functional test it somehow doesn't.
Also I tried using $this->client->followRedirect() which says the request isn't redirected. But it's just a normal delete form without validation so I don't see why it shouldn't work.
EDIT:
Apparently somehow the form isn't submitted, var_dump($form->isSubmitted()); says false. I'm not sure why it isn't submitted though since it does get submitted if I test it in the browser though.

Html form returns null?

I try to learn php/twig from a tutorial video and I tried to recreate the exercise but can't get back data from a simple form. The name variable in the action is always null. Everything else works. I looked through it multiple times but did everything like in the video. Some help would be really appreciated, thank you!
The action:
public function AddAction(Request $request) : Response{
$name = $request->request->get("name"); //always null
if ($name){
file_put_contents($this->fname, $name,FILE_APPEND);
$this->addFlash("notice","ITEM ADDED");
}
else{
$this->addFlash("notice","DATA ERROR");
}
return $this->redirectToRoute("list");
}
The form:
{% block body %}
<form action="{{ path('add') }}" mehod="POST">
<table>
<tr><td>Name of new item:</td>
<td><input type="text" name="name"/></td></tr>
<tr><td></td>
<td colspan="2"><input type="submit" value="SEND"/></td></tr>
</table>
</form>
{% endblock %}

Laravel - How to get the value of input in a view

I'm building a page with 2 forms like this:
index.blade.php:
<form action="/" method="post">
<input type="text" list="cats" name="catipt" id="catipt" />
<datalist id="cats">
#foreach($categories as $category)
<option>{{ $category->name }}</option>
#endforeach
</datalist>
<button type="submit">Add</button>
</form>
#include("catForms")
catForms.blade.php contains a form aside from the first one, with a send button to send the form input via email. The second form in which there are many div that each has input for the user to fill in. The first form allows user to add items to the dropdown list, so it's a post method. Based on the input in the first form, a corresponding div in second form will show, others will hide, using Jquery. Now, I want to send the data from the user via email, but how do I get the value of the input in the first form in the email view so that I can filter out the irrelevant div? Otherwise, I'll have to list out all div even they are empty.
The problem is that, there are two forms, so there are two Request $request, I can't get the input value from the first form in email view. I tried this in email view, but this doesn't work:
#php
use Symfony\Component\Console\Input\Input;
#endphp
Cat: {{ Input::get('catipt') }}
<form action="/" method="post">
<input type="text" list="cats" name="catipt" id="catipt" />
<datalist id="cats" name="cat-name">
#foreach($categories as $category)
<option value="{{ $category->name }}">{{ $category->name }}</option>
#endforeach
</datalist>
<button type="submit">Add</button>
</form>

How to write Controller in GRAV CMS

I created a form and want to save submitted data in back-end
//front-end
{% extends 'partials/base.html.twig' %}
{% block content %}
<form method="post" action="savedata">
<input type=text name="data">
<input type="submit">
</form>
{% endblock %}
//back-end
function savedata()
{
echo 'saved data:'.$_POST['data'];
exit;
}
I expect the code like this
$grav->post('/savedata', DataController->savedata);
But I cannot found any tutorial about custom Controller in documentation
How can I achieve this purpose?

Laravel form not sending request of dynamically generated content

I have a form like this:
<form class="form-horizontal" role="form" method="POST" name="myform" id="myform" action="{{ url('/mypage/') }}">
{{ csrf_field() }}
<div id="somecontent">
</div>
<div id="abutton">
</div>
</form>
Then some Jquery like this:
$('#somecontent').html('<select class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');
And then I add a button like this:
button = $('<button type="submit" form="myform" class="btn btn-theme">Send</button>');
$('#abutton').html(button);
And I also change the action dynamically:
$("#myform").attr("action","/mypage/" + item_id);
Then I got this in the web file:
Route::post('/mypage/{item_id}','mycontroller#do_something');
And then do this in the controller:
public function do_something($item_id,Request $request){
dd($request);
}
But the $request is empty, it does not contain the value selected in the dynamically generated select.
Any ideas why?
you haven't added name attribute of select, add name attribute and see it will populate into $request.
Try below code.
$('#somecontent').html('<select name="select_name" class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');

Categories