I have multiple checkboxes in my form and i want to use those to generate the url.
An example :
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox" value="a">
<input type="checkbox" name="checkbox" value="b">
<input type="checkbox" name="checkbox" value="c">
<input type="checkbox" name="checkbox" value="d">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
When the user presses the submit button he will have to be redirect to the generated url created as followed :
www.domain.com/checkbox/a+b+c+d
Depending on the choises of the user the url should be generated as above.
How can i pull this of?
In your checkbox.php, get all the checked checkboxes, create the url you want, and redirect the user to it with header("Location: $url").
HTML:
<input type="checkbox" name="checkbox[]" value="a" />
<input type="checkbox" name="checkbox[]" value="b" />
<input type="checkbox" name="checkbox[]" value="c" />
<input type="checkbox" name="checkbox[]" value="d" />
checkbox.php:
<?php
if ($_POST) {
$url = "http://www.domain.com/checkbox/";
$params = '';
$checkBoxes = $_POST['checkbox'];
foreach ($checkBoxes as $value) {
$params .= $value;
if ($value != $checkBoxes[count(checkBoxes) - 1])
$params .= '+';
}
$url .= $params;
}
header("Location: $url");
Here's a php example if you don't want to use AJAX
Top of your checkbox.php:
<?php
if (isset($_POST['checkbox'])){
$url = 'checkbox/' . implode('+',$_POST['checkbox']);
header('Location:'. $url);
die;
}
?>
Related
I have several checkboxes that contain names (ids as referrence to database) - see code below. How can I select all checked values and add them to database (via MySQL) each row for each checked?
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
After clicking "send" with requiered checked names, the result in database should look like this (I selected John and Mike):
Id
1
3
(only selected ones)
How can I achieve that?
You need to wrap your inputs in a <form> element and give this form a method of post:
<form method="post">
<input type="checkbox" value="1" name="names[]" />John
<input type="checkbox" value="2" name="names[]" />Peter
<input type="checkbox" value="3" name="names[]" />Mike
<input type="checkbox" value="4" name="names[]" />Kattie
<input type="submit" value="Send" name="send" />
</form>
This will allow you to post submitted data from your form inputs to your PHP.
Note: If your HTML is in a different file (ie not in the same file as your form) you can add the action attribute to your form (eg: action="fileWithPHP.php")
Now you can access all checked checkboxes in your PHP using $_POST['names']. This will allow you to get your array of checked values. You can then use a foreach loop to loop through every value in your array:
<?php
if(isset($_POST['names'])) {
$names = $_POST['names'];
foreach($names as $name) {
echo "Add " . $name . " to db here<br />"; // add $name to db
}
}
?>
You can wrap the inputs around a <form> and send it to php and retrieve using $_GET or $_POST and update the database.
I have used POST method here.
HTML:
<form action="test.php" method="post">
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
</form>
PHP:
if(!empty($_POST['names'])) {
foreach($_POST['names'] as $check) {
echo $check; //instead of echo you can insert it to the database
}
}
How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database
I have 3 checkboxes used for searching
<input type="checkbox" onChange="this.form.submit()" ',$var1 ? ' class="checkon" checked="checked"' : '','name="c1" value="c1">
<input type="checkbox" onChange="this.form.submit()" ',$var2 ? ' class="checkon" checked="checked"' : '',' name="c2" value="c2">';
<input type="checkbox" onChange="this.form.submit()" ',$var3 ? ' class="checkon" checked="checked"' : '',' name="c3" value="c3">';
I want them to all be on by default, but if I set them to on:
$_POST['c1'] = 'on';
Then when I uncheck the box it is still on? How do I get it on by default but off when I uncheck it?
I am not sure why you have an onChange event with these inputs. You should let the user check and uncheck whichever boxes they want and then submit all the data together. Then do if(isset($_POST['c1'])){ //Box was checked }. Also have checked="checked" just as a HTML attribute (so <input type="checkbox" name="c1" value="on" checked="checked" />) rather than doing a shorthand if statement in an onChange event.
Here would be the way I would do it:
<form action="" method="post">
<fieldset>
<input type="checkbox" name="c1" value="on" <?php if (isChecked('c1')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c2" value="on" <?php if (isChecked('c2')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c3" value="on" <?php if (isChecked('c3')) echo 'checked="checked"'; ?> />
<input type="submit" name="submit" value="Go" />
</fieldset>
</form>
<?php
function isChecked($name) { //Darkbee's edit
return empty($_POST) || isset($_POST[$name]);
}
if(isset($_POST['submit'])){
if(isset($_POST['c1'])){
//Checkbox1 was checked when the form was submitted, code goes here
}
//Do the same for c2 & c3 as necessary
}
?>
Hope this helps!!
I have a code here...the thing that i want is to calculate the total instantly and show in the textbox without clicking the action button.
code:
<?php
include('include/connect.php');
if(isset($_POST['enter']))
{
$name = $_POST['name'];
$score1 = $_POST['optA'];
$score2 = $_POST['optB'];
$score3 = $_POST['optC'];
$score4 = $_POST['optD'];
$total1 = $_POST['total'];
$total = ($score1 + $score2 + $score3 + $score4);
mysql_query("INSERT INTO score (name,score1,score2,score3,score4,total) VALUE ('$name','$score1','$score2','$score3','$score4','$total')");
echo "succesful";
}
else
{
echo "you fail to execute!";
}
?>
code:
<form method="post" action="index.php">
<input type="text" name="name" />
<input type="radio" name="optA" value="1" />1
<input type="radio" name="optA" value="2" />2
<input type="radio" name="optA" value="3" />3<br>
<input type="radio" name="optB" value="1" />1
<input type="radio" name="optB" value="2" />2
<input type="radio" name="optB" value="3" />3<br>
<input type="radio" name="optC" value="1" />1
<input type="radio" name="optC" value="2" />2
<input type="radio" name="optC" value="3" />3<br>
<input type="radio" name="optD" value="1" />1
<input type="radio" name="optD" value="2" />2
<input type="radio" name="optD" value="3" />3
<input type="text" name="total" value="<?php echo $total ?>" />
<input type="submit" value="enter" name="enter" />
</form>
PHP is executed before the content reaches the browser, so no elements will be loaded, so you cannot access them. It is a server-side language, not client-side; therefore you cannot do this. An example of something client-side is Javascript. However, you can create a HTML element and set its attributes, then echo it onto the page.
You'll have to use JavaScript in some form or another to not have to post the answer back to the server and wait for a response.
Here's how you can do it in JQuery.
function calculateTotal() {
var total = 0;
$('form input:radio:checked').each(function () {
total = total + parseInt($(this).val());
});
$('form [name=total]').val(total);
return false;
}
In your HTML you'll need to change your submit button to something else like this:
<span onclick="calculateTotal()">Enter</span>
How do I insert multiple checkbox values into database? Here is my code, it workes for the radio buttons where there is only one value, but how do I insert more if I have checkboxes?
PHP CODE:
if(isset($_POST['radio']) &&
isset($_POST['checkbox'])){
$radio = $_POST['radio'];
$checkbox= $_POST['checkbox'];
$sql = "INSERT INTO info(radio, checkbox)VALUES ('$radio', '$checkbox')";
$result = mysql_query($sql);
}
HTML CODE:
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox" value="1">1<br>
<input type="checkbox" name="checkbox" value="2">2<br>
<input type="checkbox" name="checkbox" value="3">3<br>
<input type="checkbox" name="checkbox" value="4">4<br>
<input type="checkbox" name="checkbox" value="5">5<br>
<input type="checkbox" name="checkbox" value="6">6<br>
Html
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
Php
use a foreach to get all the values of checkboxes
//Do the necessary coding and then
if(!empty($_POST['checkbox']))
foreach($_POST['checkbox'] as $key=>$value){
if(trim($value) != ''){
//Do insertion here
}
}
}
change the names of yours checkboxes to:
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
on php do it:
$radio = $_POST['radio'];
$sql = 'INSERT INTO info(radio, checkbox)';
foreach($_POST['checkbox'] as $item){
$sql .= "VALUES('$radio', '$item')," ;
}
$sql = trim($sql, ','); //remove the last ',';
//performe all insert at once.
avoid to use mysql_* functions, try PDO
use php core functions serialize() and unserialize()