PHP Form Arrays Using Checkboxes - php

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>

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.

PHP: How to deal with a GROUP of HTML input text fields

I've got a bunch of <input type='text' name='input0' /> fields.
The user can add them dynamically so we'd get something like:
<input type='text' name='input0' />
<input type='text' name='input1' />
<input type='text' name='input2' />
...etc. We don't know how many there will be in total. When the form gets submitted, I want to loop through each of these input fields and assign them to a $_POST variable. These are NOT the only input fields in the form, there are other elements such as radio buttons, checkboxes and other text fields. My problem is I need to somehow identify these particular dynamically-generated text fields, and loop through them. How can I do this, when all I get on the server side are the names of the input fields?
Use:
<input type='text' name='input[]' />
then you can:
foreach($_POST['input'] as $input){
echo $input;
}

PHP Read and Write field values without a postback

I want to create a hidden field in my HTML page, all good, just a simple <input> field. Then, I'm trying to read the value of that input field as the page loads the first time, so the closest I've gotten to that is using a $_GET statement. I'm simply trying to echo out the value so that I can see that it's working like such:
<input type="hidden" name="selection" value="fixtures">
<?php
echo $_GET['selection']
and that just gives me nothing, if I try:
echo "<h1>$_GET['selection']</h1>
then I get 0.
Question is - what <form> parameters you have? Did you submit form? Simples example for it will be:
//form.php
<form method="GET" action="next.php">
<input type="hidden" name"selection" value="fixtures" />
<input type="submit" name="submit_btn" value="Send" />
</form>
//next.php
echo '<h1>'.$_GET['selection'].'</h1>';
You can achieve same thing with:
Link

submitted forms with the same name to a php script?

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?

PHP Search w/ categories

I wanted users to filter searches by categories
I have 3 PHP files. One named searchbycity.php, searchbystate.php, and the default search.php
My question is, how would I set it up so I could click on a radio button and the search bar
would know which php file to search for with that info?
Here's how I have the whole radio button thing laid out so far
<input type='text' size='70' name='search'>
<input type='image' value='search' src='images/tickmark.png'></a><br>
Search by
<input type="radio" onclick="eng = this.value;" checked name="sengines"
value="http://www.google.com/search?q=" />
City
<input type="radio" onclick="eng = this.value;" name="sengines"
value="http://www.altavista.com/web/results?q=" />State
(Ignore the Google search and the AltaVista search, I got it from a website:P)
You could write this html code (note the substitution of radio strings values with integers):
<form action="search.php" method="GET">
<input type='text' size='70' name='search'>
<input type='image' value='search' src='images/tickmark.png'></a><br>
Search by
<input type="radio" onclick="eng = this.value;" checked name="sengines"
value="1" />
City
<input type="radio" onclick="eng = this.value;" name="sengines"
value="2" />State
<input type="submit" />
</form>
When the user push the send button the search.php page will be execute (server side). This page may contains the following code:
<?php
if(is_integer($_GET['sengines']) && is_string($_GET['search'])){
switch($_GET['sengines']){
case 1: include_once "searchbycity.php";
searchByCity($_GET['search']);
break;
case 2: include_once "searchbystate.php";
searchByState($_GET['search']);
break;
}
}
?>
So, if the user selected the first radio button the searchbycity.php file and the hypothetical function called searchByCity present in the file will be called passing the $_GET['search'] value submitted by form. Else, will be included the searchbystate.php file and... the logic will be analogue.
Pay attention: the data sended by form must be sanitized by using the filter functions. In the code there is a first level of checking by using is_integer and is_string functions.

Categories