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

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 ===.

Related

How to read multiple check boxes in PERL-CGI

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.

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.

Submit form method post and retrieve data

I am trying to search for the right method to submit HTML form with the data from excel cell and retrieve part of the result back.
HTML form URL is http://www2.stat.gov.lt:8777/imones/sektor.html:
<form action="sektor.chk_sekt" method="POST">
<br>
<b>Ūkio subjekto kodas: </b>
<input type="text" name="imone01" size="9" maxlength="9">
<br>
<br>
<input type="submit" value=" OK ">
</form>
Example data strings to submit are 303305024, 300983557, the value to be extracted from the response on the page http://www2.stat.gov.lt:8777/imones/sektor.chk_sekt is the line:
<BR>
<B>Veiklos rūšis pagal EVRK red. 2: </B>
479100 - Užsakomasis pardavimas paštu arba internetu
<BR>
The values from each cell in column A should be submitted within loop, and the retrieved results should be filled into corresponding cells in column B.
I have reviewed several similar questions but they seem to be using some different format of form and doesn't suit in this case.
Here is the example using XHR and RegEx to retrieve the data you need:
Option Explicit
Sub RetriveData()
Dim i As Long
For i = 1 To Cells.Rows.Count
If Cells(i, 1).Value = "" Then Exit For
Cells(i, 2).Value = GetData(Cells(i, 1).Value)
Next
End Sub
Function GetData(sCompany As String) As String
Dim sContent As String
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "http://www2.stat.gov.lt:8777/imones/sektor.chk_sekt", False
.Send "imone01=" & sCompany
sContent = .ResponseText
End With
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<head>[\s\S]*?</head>|(?!<br>)<[^>]*>|[\r\n\t]*"
sContent = .Replace(sContent, "")
.Pattern = "<BR>Veiklos r\u016B\u0161is pagal EVRK red. 2: (.*?)<BR>"
With .Execute(sContent)
If .Count = 1 Then GetData = .Item(0).SubMatches(0) Else GetData = "n/a"
End With
End With
End Function
The output is as follows for me:

Automatically update database with checkboxs that are unticked (false)

I currently update my database records with an input like this
$this->my_model->update($id, $this->input->post()));
(This is after I have performed a validation on all the input. The "model" also has a white_list of data to expect.)
Problem: if I am updating a checkbox, and it is "unticked" (i.e. 'false') - then that field is not 'posted' by the browser.
For example - if I tick checkbox_two - but leave checkbox_one unticked, $_POST shows:
[field_one] = "some value"
[field_two] = "some value"
[checkbox_two] = 1
Therefore my model will not update that field - since it is not part of the post.
The same does not happen in reverse - because a "ticked" checkbox is posted as "1" - and thus is correctly updated.
[field_one] = "some value"
[field_two] = "some value"
[checkbox_one] = 1
[checkbox_two] = 1
Question: Does anyone have an elegant solution to handle this, other than having to always specifically check for each checkbox?
<input type="hidden" name="example" value="FALSE" />
<input type="checkbox" name="example" value="TRUE" />
This practice is easy and logical. If checkbox is checked, $this->input->post('example') == 'TRUE' else, $this->input->post('example') == 'FALSE'.
The latest given value of a name="" overrides previous, thus givin prio checkbox > hidden.
EDIT: this solution is equal to given most rated answer in Rick Calder's comment. I've also come to this conclusion on my own, though.

(Lift) Ajax submit using SHtml.select onchange event

I'm tring to implement the classic functionality of this select:elements depends on this other select:selection using Lift, E.g. select the country and get the possible states for the selected country.
The problem that I'm facing is that the "this.myForm.submit()" that I have inside the onchange of the select element is not firing the ajax request. If I use an input type"submit" it works perfectly.
Is this behaivior related with Lift framework? Is there a better way of implementing this kind of functionality using Lift libraries?
The relevant snipped code:
"name=distributionPoints" #> SHtml.select(distributionPoints, Empty, selectedDistributionPoint = _, "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(devices, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
The view html:
<form class="lift:form.ajax">
<select name="distributionPoints" id="the_distributionPoints" onchange="submit()">
<option></option>
</select>
<br/>
Device:
<select name="devices" id="the_devices">
<option></option>
</select>
</form>
The rendered HTML:
<form id="F391649920812YBACEZ" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F391649920812YBACEZ").serialize(), null, null, "javascript");return false;">
<div>
Punto de distribución:
<select onchange="submit()" id="the_distributionPoints" name="F630704816482OLP514"></select>
<br />
Equipo:
<select name="F391649920817BLRJW5" id="the_devices"></select><input type="hidden" name="F391649920818HPS35E" value="true" />
<br />
</div>
</form>
[edit]
I finally got the solution. Like Chris mentioned I used ajaxSelect instead of just selects and instead of using setHtml there's a method called JsCmds.ReplaceOptions that does exactly what I was looking for.
You should understand that when using Ajax submit the page is not reloaded. So I would suggest you to use JsCmds.setHtml on server side to "reset" the second select element.
So, in fact the first select is an ajaxSelect which is meant to modify the second one (so it is not concerned by the hidden submit in my opinion). The second select is updated when the first one is changed, using 'selectPoint(s)'
Piece of Scala code
def selectPoint(s):JsCmd = {
selectedDistributionPoint = s;
newDevices:List[String] = findCorrespondingDevices(s);
JsCmds.setHtml("name=devices", SHtml.select(newDevices, Empty, selectedDevice = _, "id" -> "the_devices")) ++ SHtml.hidden(process))
}
"name=distributionPoints" #> SHtml.AjaxSelect(distributionPoints, Empty, s=> selectPoint(s), "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(Nil, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
Piece of template code
<input name="distributionPoints" id="the_distributionPoints" onchange="submit()"/>
<input name="devices" id="the_devices"/>