putting all checked items in checkboxes in an array - html

I need to take all the checked items in an html page and put them in an array to perform a certain action on it. I don't understand the fields of a checkbox(name, value,...). I am using html with ruby on rails!

If you need to do it on the client side using javascript you can use jQuery like this:
var arr = $('[type=checkbox]:checked');
If you need to do this on the server side in ruby on rails I cannot help you. However I do know that only checked checkboxes will get send back to the server on a GET or POST, so if you know what names these checkboxes you can get them from the request object and put them in a array. Non checked checkboxes will never get to the server, so you won't have to explicitly ignore them.

$_POST is already an array of everything within the form set. Use this to do your "certain action" on the checkboxes. If for whatever reason you need the information in a different variable, then just loop through the $_POST and put it into a different variable like so:
while ($row = $_POST) {
$new_var[] = $row[0];
}
That should work for you to put into a new/different array but frankly, I don't see the point of that since it's already an array.
As far as understanding the way the array of $_POST looks like, then var_dump() it and see. The name of the checkbox will be that in [] and the value will display after the =>. So if your input is named name=box1 and the value is value=1 then the array, from the $_POST standpoint, would look like [box1] => 1 so you can then use this section by calling it specifically like $foo = $_POST['box1'] and the value of $foo would be the value of box1 or you can do whatever else you want with that (rather than put into a new variable) by calling it.

If you do something like this:
<input type='checkbox' name='User[]' VALUE='someVal'>Visible Text</option>
Whatever checkbox is checked by the user then submitted will be in a POST array called User.
Adding the [ ] to the name creates an array when submitted.
Not sure hot to do this in ruby on rails but in php. When the form is submitted but the user an array is passed to the server. We names our checkboxes as User so our posted array is called User. In php i retrieve the posted array like this:
print_r($HTTP_POST_VARS["User"]);
then this outputs the array like so:
Array ( [0] => Me [1] => You [2] => SomeoneElse )
all of the data in the array were check checkboxes. the you can do a
foreach (​​​​​​​ $HTTP_POST_VARS["User"] as $key => $value) { $key is 0, $value is Me. and so on }
Hope this helps –

Related

Inserting and retrieving information from MySQL field in Laravel

I have a JSON field which I am experimenting with but I am having a bit of trouble with it.
I have added the following to my Customer model:
protected $casts = [
'billingConfig' => 'array'
];
And I updated a test field using the following in my controller:
$customer->billingConfig = ['attachableType' => $request->attachmentsConfig];
$customer->save();
After this, the following appears in my database:
{"attachableType": "combined"}
Now, when I go to grab this specific value through my blade:
{{$customer->billingConfig->attachableType}}
I get "Trying to get property 'attachableType' of non-object"
But when I use the below:
{{$customer->billingConfig['attachableType']}}
I get the "combined" value I was looking for.
I was using this guide: https://www.qcode.in/use-mysql-json-field-in-laravel/, and I guess I wanted to make sure I was doing everything right and their method was wrong or I had goofed up somewhere.
JSON data fields are being read as an array, which you can not call a property for, that's what I believe the reason for the error you got when called
{{$customer->billingConfig->attachableType}}
because it's an array, not a data object (like the case in javascript).
Thanks for sharing.

HTML url passing multiple parameters

I'm trying to pass multiple parameters with an url. I have checked out almost every topic about it, but can't seem to find an answer. I have tried different ways, but it still doesn't work.
It is only sending the first parameter.
If I start with post_id - I can't get comment_id
If I start with comment_id - I can' get post_id
My idea of url:
http://localhost/index.php?post_id=3&comment_id=6
OR
http://localhost/index.php?post_id=3&comment_id=6
I try to use it later like this:
else if(isset($_GET['post_id']) & isset($_GET['comment_id'])){
$post_id = $_GET['post_id'];
$comment_id = $_GET['comment_id'];
$user_id = $this->UserModel->getUser();
$BlogPost = $this->BlogModel->getBlogPost($post_id);
$BlogComments = $this->BlogModel->getBlogCommentList($post_id);
$BlogComments = $this->BlogModel->deleteBlogComment($comment_id,$user_id);
include 'views/ViewBlogPost.php';
}
Your below URL Structure is perfactly true
http://localhost/index.php?post_id=3&comment_id=6
Logic to Pass data Through URL in your script is called as QueryString Which you can build in below way
NAME=VALUE - this is called Name Value Pair , you can Pass Multiple Name Value pair by appending with '&' only
e.g.
http://localhost/index.php?name_1=value_1&name_2=value_2
on server side you will retrive them by using GLOBAL $_GET e.g print_r($_GET); to view all the passed data.
individually you can access them by using
echo $_GET['name_1']; echo $_GET['name_2'];
Also i suggest you to check your GPSC setting in your php.ini to define the priorities between your Super Globals.

Can't save the data using wbragancia widget yii2 for more than one dynamic form in single view

I am new in yii2 and i am using wbraganca/yii2-dynamicform widget in my view. My question is that I have to use more than two dynamic form in a single view. For single dynamic widget it works fine while saving from controller but others are saving just the last data of dynamic widget, while inspecting the post data in network array index for others except first dynamic form are not increasing (there index is only zero). Would you please suggest how should I have to do. How can I use multiple dynamic-form widget in single view. Thank you in advance.
I believe the problem must be in your view. You should ensure that each control has a unique name and then loop through the post variables in the controller.
<?= $form->field($modelx, "[{$i}]MyVariableName", ['labelOptions' => ['label' => false]])->textInput(['name' =>'myClassNameHere'."{$block}[$i][MyVariableName]"]) ?>
See the 'name' option in the code above. It gives a unique name to each field. Here the $block variable is a unique code I append to each widgetItem class in the DynamicFormWidget.
If I now look at my POST data, I see something like the following:
myClassNameHere0[0][MyVariableName]:1
myClassNameHere1[0][MyVariableName]:11
If this does not help - post a simply example that you cannot get working and I will have a look at it.

Yii2: How to send multiple parameters in redirect

I am redirecting from login page to dashboard page. I want to send teams variable that contain my existing data about the teams. I have tried following but that is not working...
return $this->redirect(array('site/dashboard', ['model' => $model1, 'teams' => $teams]));
model1 is getting sent but not teams...
As arogachev already pointed, the second param is the code, BUT the first param is an array. In order to put some parameters, you need to do something like this:
Yii::$app->response->redirect(['site/dashboard','id' => 1, 'var1' => 'test']);
So for every param you put extra item in the array where the key is the name and the value is the value of the get param.
Cheers!
Change your line to following. Since you have already used array no need for square brackets.
return $this->redirect(array('site/dashboard', 'model' => $model1, 'teams' => $teams));
See official documentation about this method.
The second parameter is status code, so what you are doing is completely wrong.
Passing the variables here does not make any sense because immediately another action starts to load.
You should pass variables to view in action to which you redirect and not where actual redirect happens.

Get both name and value from option using Perl CGI param

In my HTML code I have
<option name="PRODUCTS" value="3">Products</option>
I need get both the name and value values in my server side Perl function through my Perl CGI param.
I can only get the value, is there a way to get the name also?
<option name="PRODUCTS" value="3">Products</option>
An <option> element does not have a name attribute, so this is invalid HTML and the browser will ignore it (except that it might make it available to JavaScript).
When the form is submitted, the browser will send to the server the name of the select coupled with the value of the selected option. This is the only information that the server will receive.
If you want to get 'PRODUCTS' then you will need to either:
Include it in the value: value="3-PRODUCTS" and then my ($number, $word) = split '-', $value
Look up the word that 3 is related to on the server (e.g. in a hash embedded in the script, or with a database query).
CGI::param() with no parameters returns all the names. Then CGI::param($name) will return all the values for a given name.
for my $name (CGI::param()) {
for my $val (CGI::param($name)) {
print "name: $name, value: $val\n";
}
}
Update: sorry, misread "option" as "input". option's name attribute isn't sent to the server at all, so there's no way to tell what it was without also having the html itself. You could add javascript to set a hidden input field to the option's name when it's chosen, though. What does populateCatArray do?
I would use the Vars method if there is some doubts on what is getting passed, especially if you have thee same 'name' used multiple times or a js its producing elements that don't exist on the page normally.
use CGI;
use Data::Dumper
my $q = new CGI;
print
$q->header({ type=>'text/plain' }),
Dumper($q->Vars);