Appending checkbox selections to URL - php

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.

Related

Check all checkbox and store all selected items in a global variable

I have a list of emails with a checkbox next to it, the user will be able to select which address he/she wants to email. Now i've added another checkbox that when checked will check all the other checkbox. Below is the code i wrote (with help from stackoverflow of course) :
<SCRIPT LANGUAGE="JavaScript">
function selectFunction (checkall,field)
{
if(checkall.checked==true){
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}else{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
}
</script>
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
The "select all" function works fine, but using print_r i saw that when the submit button is clicked, the submited value is the last checkbox that i selected. For example if i click 5-3-1-2, the value in $_POST is "2" and not the rest.
i realized that my code can only register ONE selected checkbox, therefore only the last one is taken into account. So I rewrote the code by adding [] behind the name of the checkbox :
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list[]" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list[]" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list[]" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list[]" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list[]" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
Now, it registers the multiple selections when i checked with print_r.(if i click 5-3-1-2, the value in $_POST is now [0]=>5,[1]=>3,[2]=>1,[3]=>2.)
But the "select all" checkbox doesn't work anymore. I assume is due to the [] which transformed the field into an array. I tried various methods (by replacing "document.myform.list" with "document.myform.list[]" etc. ) None is working so far, i'll continue experimenting but if anybody have a clear idea on how to merge the 2 codes above please help.
Thank You
Change this:
onClick="selectFunction(document.myform.selectallcb,document.myform.list)"
to this:
onClick="selectFunction(document.myform.selectallcb,document.myform['list[]'])"
Here's the fiddle: http://jsfiddle.net/NxfH6/

distributing form elements among different webpages,looks simple but not able to fix

This question is similar to my previous question but not the same ... please check out....I am using totaly 3 webpages; form elements are distributed among two pages, "eg1.html" and "eg2.html", but all the form elements should be submitted to "eg.php".
Here is the code for eg.php which accepts the form elements from both eg1.html and eg2.html:
$size=$_POST["fontsize"];
$label=$_POST["label"];
$age=$_POST["age"];
$sex =$_POST["sex"];
code for eg1.html
<html>
<body>
<form action="eg.php" method="post">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</form>
</body>
Now What would be the code for eg2.html? just check out sample partial html code :but needs to be compleated....
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
The code should work exactly like this:
First user will open eg1.php he selects only one option that is "fontsize" .. next he clicks on the "link to eg2.html" to select two more options "age" and "sex" after selecting... he will be redirected back to eg1.php where he has to select one more option that is "label" ... then he will submit the form to eg.php. Which will hold all form elements those are 'fontsize' 'age' 'sex' and 'label' .....
I have seen many website using this technique please check out cooltext.com where user will get an option to click on the font image which will redirect him to fonts page after selecting one of the fonts images he will be redirected back to homepage,where he can select some other form elements or form elements and finally submits the form .... i have also seen many websites using this technique , i think this can be done using JQUERY/JavaScript but not sure ...please help me to fix this problem guyz,.,,,
Using js you can have the entire form on one page and divide it in steps like this
<form action="eg.php" method="post">
<div class="step1">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</div>
<div class="step2">
click here to go back to step1
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
<input type="submit" value="Submit"/>
</div>
</form>
js:
$('#step1_submit').click(function(){
$('#step1').hide();
$('#step2').show();
});
$('#step2_back').click(function(){
$('#step1').show();
$('#step2').hide();
});

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.

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