submitted forms with the same name to a php script? - php

Is it possible to submit forms with input checkboxes, each containing the same name, to a PHP script?
Is it possible to loop through the names to get all the values?
I am building a message system, and users can add/remove recipients dynamically. When they do, a hidden checkbox is generated in the form containing the value, yet I'm not sure what to do with the name. On the php end, on top of the recipients a subject and a message are submitted, and the script needs to loop through each name and perform various SQL tasks. I know there are much better ways of doing this, and feel free to suggest, but I'd really like to know if it can get done this way. Comment if you need to see code, but I warn you, it's really confusing.

<input type="checkbox" name="samename[]">
// on the post/get:
foreach( $_POST['samename'] as $eachId ){
// do whatever you want. build the where in a query, ' set = '.$eachId
}

Yes you can, use the same name with [] after it, it will cause all of the values to be stored in an array on PHP.
<input type=checkbox value=1 name=check[]>
<input type=checkbox value=2 name=check[]>
<input type=checkbox value=3 name=check[]>
<input type=checkbox value=4 name=check[]>
<input type=checkbox value=5 name=check[]>

Yes you can, array of post, look at this example:
<?php
print_r($_POST);
?>
<form action="form.php" method="POST">
<input type="checkbox" name="vehicle[]" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle[]" value="Car" /> I have a car
<input type="submit" value="Submit" />
</form>
Notice how vehicle has the square brackets?

Related

Appending checkbox selections to URL

I want to be able to search through my WordPress Woocommerce store, by selecting multiple product tags. By default, if you add tags to a product, they are visible on the product page, and if you click on a tag, it searches for other products which are also tagged the same. The results of this search look url like this:
http://localhost/ccloud/?product_tag=option1
If you manually add other tags to this URL, it searches for products which are tagged with both selections, like this:
http://localhost/ccloud/?product_tag=option1+option2
This works, but obviously I want users to be able to do this using checkboxes.
I’ve created this form (which doesn’t work)
<form name="search_by_tag" action=/product_tag.php method="get">
<input type=checkbox name="option1">Option 1<br>
<input type=checkbox name="option2">Option 2<br>
<input type=checkbox name="option3">Option 3<br>
<input type=checkbox name="option4">Option 4<p>
<input type=submit value="Search">
</form>
I think it doesn’t work because it’s not sending the action correctly. The result of selecting multiple checkboxes and searching looks like this:
http://localhost/product_tag.php?option1=on&option2=on
How can I correct the url (the first part is missing the directory) and remove the .php part etc? It doesn't work at all if I remove the .php extension
You’ll have to use a little JavaScript to make this work, but you could do it like this.
HTML:
<input type="checkbox" class="tags" value="red">Red<br>
<input type="checkbox" class="tags" value="blue">Blue<br>
<form name="search_by_tag" action="/ccloud/" method="get">
<input type="hidden" id="tags" name="product_tag" />
<input type="submit" value="Search"/>
</form>
jQuery:
$(function() {
$('form').on('submit', function() {
var tags = [];
$('.tags:checked').each(function() {
tags.push($(this).val());
});
tags = tags.join(' ');
if (tags) $('#tags').val(tags);
else $('#tags').remove();
});
});
So, what we are doing is using a hidden field to store the selected tags, and moving the checkboxes out of the form. Then, when they submit, we populate the hidden field so that it gets included in the query string. Spaces turn into +'s in the URL.
product_tag needs to be the name of the hidden field, and the action is /ccloud/, so that you end up with a URL like you want. Here is a jsFiddle of it in action.
A few issues I see in your code:
<form name="search_by_tag" action=/product_tag.php method="get">
<input type=checkbox name="option1">Option 1<br>
<input type=checkbox name="option2">Option 2<br>
<input type=checkbox name="option3">Option 3<br>
<input type=checkbox name="option4">Option 4<p>
<input type=submit value="Search">
</form>
The type property should have quotes around it. Also, you are not setting a value property. And to get the URL to be /product_tag you need to lop off that .php and mayeb set the form action to the full site URL So this should be closer to what you want:
<form name="search_by_tag" action="http://my.great.site/product_tag" method="get">
<input type="checkbox" name="option1" value="option1">Option 1<br>
<input type="checkbox" name="option2" value="option2">Option 2<br>
<input type="checkbox" name="option3" value="option3">Option 3<br>
<input type="checkbox" name="option4" value="option4">Option 4<p>
<input type=submit value="Search">
</form>
And that said, you need to add more logic—either via PHP or JavaScript like the user dave points out—to get this set. A simple get wouldn’t work.

proper structure for array of form field names for both jquery validation and php processing

The simplified form in the html code below contains a number of repetitive form fields which must be both validated by means of some jquery validate code and processed by means of some php code...I have set up the form field below to facilitate the php processing, but then again get stuck with writing the proper jquery validate code as the 'name' tags are supposed to be unique. If I'd make the name tags unique I think the php coding becomes more complex.
Just wondering what a proper structure of repetitive form fields would be which facilitates both the jquery validation coding and php coding?
Any suggestions?
Thank you in advance.
html code:
<input type="checkbox" id="check0" name="productselection[]" value="productselected0">
<input id="nrofparts0" type="text" name="nrofparts[]">
<input type="checkbox" id="check1" name="productselection[]" value="productselected1">
<input id="nrofparts1" type="text" name="nrofparts[]">
<input type="checkbox" id="check2" name="productselection[]" value="productselected2">
<input id="nrofparts2" type="text" name="nrofparts[]">
heres one way you can do it:
HTML
<form id="theForm" method="post">
<input type="checkbox" name="checky[0]" />
<input type="checkbox" name="checky[1]" />
<input type="checkbox" name="checky[2]" />
<input type="submit" name="submit" value="submit"/>
</form>
I add a key to the array because it makes it easier to determine which checkboxes were clicked. you wont get anything for an unclicked one. that can make it confusing
jquery:
$('#theForm input[type="checkbox"]').each(function(){
// if($(this))... some validation
});
php:
if(isset($_POST['checky'])){
foreach($_POST['checky'] as $key => $val){
echo $key.' '.$val; // this will give you the key of the checkbox, and the value
}
}
you could also have a hidden input that counts the checkboxes. That way when you loop, you can use it to set the empty checkboxes in your php code.
hope this helps!

How to read the form's inputs by type?

I have a form:
<form name="form1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="a1" value="someValue1" />someValue1<br />
<input type="radio" name="a1" value="someValue2" />someValue2<br />
<input type="radio" name="a1" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="a2" value="someValue4" />someValue4<br />
<input type="radio" name="a2" value="someValue5" />someValue5<br />
<input type="radio" name="a2" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="a3" value="someValue9" />someValue9<br />
<input type="radio" name="a3" value="someValue7" />someValue7<br />
<input type="radio" name="a3" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
And want to read all inputs to array by type (radio). I know, how to read it by name, but how by type?
The input type attribute is not sent to the server when the form is submitted. Only the name and the value are sent. You will need to keep track of what's what yourself on the server using useful names.
make your form_to_write.php like this:
<?php
print_r($_POST);
and study it's output.
It contains everything you can get from the form. You are free to choose what to use. Enjoy.
As your question being a perfect example of a badly asked question, I can only guess your real needs.
It seems you want to get an array contains all radio buttons. You still can do it by using names.
make your radio buttons names like this
<input type="radio" name="radios[a1]" value="someValue1" />someValue1<br />
<input type="radio" name="radios[a2]" value="someValue4" />someValue4<br />
<input type="radio" name="radios[a3]" value="someValue9" />someValue9<br />
and you'll be able to access $_POST['radios'] array which contains all your radio fields
If you are looking for a PHP function like GetAllInputsOfType("radio") then you won't find it (unless you can do somethign fancy with the DOM, like JS does; maybe this will help?).
What I have done in similar circumstances is to rename my input fields according to type, so instead of a1, a2, a3, you could have radio_a1, radio_a1, radio_a3 and text_a4, memo_a5, listbox_a6, etc (and, btw, use some meaningful names, not a1, a2, a3 ;-)
Then you can loop thorough the array $_GET or $_POST looking for elements beginning radio_ ...
You could use something like a Zend_Form which keeps track of it (and could even do validating jobs etc). But you can't get the type of a form field with just php - you'll need to do things in JS which is on the client side and may not be trusted.

combine checkbox values using php mysql

I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).

PHP Form Arrays Using Checkboxes

If I make a long story very short, I have a short form I've made (an input, a select, and three checkboxes). I've made a function on a button that can dynamically add multiple instances of this form on my page. It saves it as an array (i.e. the input name is name="checkbox[]") which will save fine in my database. The problem I run into is I may have 6 instances of this form, but only some of the boxes are checked. So, I may have 6 text inputs, 6 select inputs, but maybe only 3 checkbox inputs. Since it only has 3 inputs, the array is only 3 pieces of data and when I run a for() statement it doesn't accurately save this information and tie it to the correct record.
I thought that maybe I could have a hidden input that will get its value assigned through javascript, but I don't know how to reference the checkboxes appropriately (you can't do id="blahblah[]" right?)
Sad and Confused,
ImmortalFirefly
I am not sure I have caught your drift on this one, but consider this:
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
</form>
Then another form is added
<form name=form1 method= post action = "">
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
<input type = submit>
</form>
Mock that up in html and POST it back to a webpage and see how it works, you can iterate through th post value to see which form was sent and which box checked, or put it all in one single form.
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
Then another series of checkboxes is added :
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
close off the form
<input type = submit>
</form>

Categories