How to read multiple check boxes in PERL-CGI - forms

I have a form that have multiple checkboxes(All that apply) and I'm trying to read all the values selected....the user click submit it reloads the page and check for "post" and if is a new entry....but it only reads the first value selected and I don'w know what am I doing wrong ;/
<label class="checkbox-inline"><input type="checkbox" name="sections" value="Cars">Cars</label>
<label class="checkbox-inline"><input type="checkbox" name="sections" value="Trucks">Trucks</label>
<label class="checkbox-inline"><input type="checkbox" name="sections" value="Airplanes">Airplanes</label>
<label class="checkbox-inline"><input type="checkbox" name="sections" value="Cell Phones">Cell Phones</label>
sub post
{
if($id1 == 'active')
my #sections = $POST->{sections}->[0];
}

There are a few obvious problems in your code.
You're using == when you're trying to do a string comparison. Use eq instead.
Your if syntax is wrong. In Perl, you need to use braces - if (...) { ... }.
You are explicitly asking for the first element from the array of checkboxes. You need #{ $POST->{sections} } to get all of the values.
So, all in all, your subroutine should probably look like this:
sub post
{
if ($id1 eq 'active') {
my #sections = #{ $POST->{sections} };
# Do something else with #sections
}
}
Also, I'd ask you to seriously reconsider using CGI in 2019. Please read CGI::Alternatives and think about using more modern technologies.

Related

how to retrieve select options from my database

I'm trying to retrieve my select option from 3 databases located in a connection that's not my defaut connection.
but I'm getting an error : Undefined variable: marqs (View: C:\wamp64\www\projetSovac\resources\views\home.blade.php)
Here's my controller code
public function index()
{
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')
->with(compact($marqs))
->with(compact($models))
->with(compact($Finitions));
return View('home');
}
and my home.blade.php code
<tr class="filters">
<th><input type="text" class="form-control daterangepicker-field" placeholder="PĂ©riode d'analyse" disabled ></th>
<th><select class="form-control " disabled>
{!! Form::Label('marque', 'marque:') !!}
#foreach($marqs as $marque)
<option value="{{$marque->codeMarque}}">{{$marque->codeMarque}}</option>
#endforeach
</select>
</th>
Can you help identify the problem?
Thanks
compact($marqs) wants to have a string divining the variable you want to pass to the view. Use: compact('marqs') you can also combine your variables like compact('marqs', 'models', ....etc )
Also you are returning something 2 times now in the function this is not possible.
I would rewrite your function to be like this:
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')->with(compact('marqs', 'models', 'Finitions'));
Assuming the first 6 lines get you the actual data all i changed was the return.
You might want to read up on how to use laravel models
https://laravel.com/docs/5.7/eloquent
I am not sure if u have defined any but it could make your code allot simpler.

Customize error message from Abide in foundation 6

I have a simple sign up form with one input tag that is set up for an email. I'm using abide to validate the email. That works, but I would like to create my own error message and to style elements on the page of my choosing. Is this possible?
I found this which I know isn't validating an email input ( I can't seem to find the code that abide is using to validate emails)
abide: {
validators: {
myCustomValidator: function (el, required, parent) {
if (el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
}
}
I would assume if I am able to set up a custom validator that just mimics what abidie already does (email validation) then I could change all the elements on the page that I wanted to when the validation comes back false.
I'm not sure what exactly you're asking for but checkout the docs http://foundation.zurb.com/sites/docs/abide.html
So it is a little different now in Foundation 6. Once you initiate Foundation, you will have to overwrite certain properties in the Foundation.Abide object. Like this:
Foundation.Abide.defaults.patterns['dashes_only'] = /^[0-9-]*$/;
Foundation.Abide.defaults.validators['myCustomValidator'] = function($el,required,parent) {
if ($el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
};
Then in your markup in your form would something like this:
<input id="phone" type="text" pattern="dashes_only" required ><span class="form-error">Yo, you had better fill this out.</span>
<input id="max" type="number" data-validator="myCustomValidator" required><span class="form-error">Yo, you had better fill this out.</span>
You can customize the error messages in the span tags.

How to serialize html form in dart as a string for submission

In jQuery, there is a function to serialize a form element so for example I can submit it as an ajax request.
Let's say we have a form such as this:
<form id="form">
<select name="single">
<option>Single</option>
<option selected="selected">Single2</option>
</select>
<input type="checkbox" name="check" value="check1" id="ch1">
<input name="otherName" value="textValue" type="text">
</form>
If I do this with the help of jquery
var str = $( "form" ).serialize();
console.log(str);
the result would be
single=Single2&check=check1&otherName=textValue
Is there such functionality in dart's FormElement or I have to code it myself? Thanks.
I came up with my own simple solution that might not work in all cases (but for me it is workikng). The procedure is this:
First we need to extract all input or select element names and values from the form into Dart's Map, so the element name will be the key and value the value (e.g. {'single': 'Single2'}).
Then we will loop through this Map and manually create the resulting string.
The code might look something like this:
FormElement form = querySelector('#my-form'); // To select the form
Map data = {};
// Form elements to extract {name: value} from
final formElementSelectors = "select, input";
form.querySelectorAll(formElementSelectors).forEach((SelectElement el) {
data[el.name] = el.value;
});
var parameters = "";
for (var key in data.keys) {
if (parameters.isNotEmpty) {
parameters += "&";
}
parameters += '$key=${data[key]}';
}
Parameters should now contain all the {name: value} pairs from the specified form.
I haven't seen anything like that yet.
In this example Seth Ladd uses Polymers template to assign the form field values to a class which get's serialized.

How to delete list of files using checkbox in play framework?

I am new to play framework and i have a list of files and i wanna add check box so that i can delete the all the checked files on clicking the delete button, i am displaying the list of files using scala language, help is really needed
#form(action = routes.Application.delete, 'enctype -> "multipart/form-data") {
#for(order1 <- listfiles) {
#if(order1.isDirectory()) { }
else {
<input type="checkbox" name="#order1">#order1.getName()
}
}
<input type="submit" name="delete">
}
where #order1.getName() is the name of the file
You need to pass them as an array of params, take a look at the nico_ekito's answer to find how you can access them in the controller.
Optionally you can use ie. JavaScript to join all filenames/identifiers into one comma-separated String and then just split it in controller to List<String> which maybe will be even easier.
you have to use special syntax for name attributes [], to let to know play that you will send list of elements:
#for((value,index) <- listfiles.zipWithIndex ) {
<input type="checkbox" name="files[index]">#value.getName()</input>
}
and then you can bind them to list in you form mapping like this
files -> list(text)

How do i process multiple if statements in a sinatra post method

I am trying to do multiple things based on the values of my checkboxes. I have this on the htlm form :
<input id="one" checkbox" type="checkbox" value="1">
<input id="two" checkbox" type="checkbox" value="2">
And in Sinatra
post '/process-data' do
one = params[:one]
two = params[:two]
if one = "1"
"One"
end
if two = "2"
"Two"
end
end
I am unable to get the result from second if statement, my question is, how do i get Sinatra to be able to get the values from the second if.
try:
"One" if one == "1"
"Two" if two == "2"
You're using = rather than == or ===.