display check boxes value from database in wordpress - php

I am storing service_name array for my 9 check boxes. This is my code for checkboxes.
<?php
$service_name=get_post_meta($edit_id, 'service_name',true);
?>
<input type="checkbox" class="service_name" size="80" name="service_name[]"
value="architecture"
<?php if(in_array('architecture',(array)$service_name) ){
echo "checked";
} ?>
onclick='chkcontrol(0)';/>Architecture
Other checkboxes are same as it is. Now I want to display only selected checkboxes from database on another page. So how do I do that? chkcontrol is for limiting checkbox selection limit upto 3. I am getting correct values while editing also.

Related

Pass an array of values as post data to document.forms[].method?

In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}

How to insert multiple textbox values in databse those are checked with checkbox with php?

Below is an explanation of exactly what I want
On the submission form I have multiple checkboxs. when users click on checkbox a textbox appears next to it, where user input text. I want to insert the value of texbox in array and insert it in database with PHP-MYSQL.
for example checkbox1 is index 0=>valueoftextbox, checkbox2 is index 1=>value of texbox2
How can I achieve this?
Serialize received $_POST.
<input type="checkbox" name="smth[]" value="One" />
<input type="checkbox" name="smth[]" value="Two" />
<input type="checkbox" name="smth[]" value="Three" />
<?php
if (isset($_POST)) {
$smth = serialize($_POST['smth']);
$query = mysql_query("INSERT INTO table VALUES ('$smth')") or die(mysql_error());
}
A few pointer to get you going:
1) From PHP use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
That way you can see WHAT is posted from your client-form.
2) Note that an unchecked checkbox ISN'T posted at all.
So if you have following checkbox in your form named "wantsnewsletter".
Then check that from PHP like this:
if (isset($_POST["wantsnewsletter"])){
// It was checked.
} else {
// It wasn't checked
}
Futhermore you just have to think up some sensible naming and do the inserts into you DB.

multi select option printing

I am just a beginner in php.
I am trying to print the values of the form when submitted. I used the following code for hobbies which is a field of my form.
<?php
$hobbies=array("Reading books","Listening to music","Swimming","Watching TV");
foreach($hobbies as $key=>$value) {
$hobbyvalues='<input name="hobbies" type="checkbox" value="'.$value.'" />'.$value."</br>";
echo $hobbyvalues;
}
?>
I store the selected hobbies like the following.
$hobbies = $_POST["hobbies"];
echo $hobbies;
I checked it by selecting more than one hobby and submit the form. But my result prints only the last selected hobby.
I need my code to display all the selected hobbies. How can i do that?
make it like this
<input name="hobbies[]"

Count number of Checked check boxes from a php gathered check list

I'm presented with a problem using PHP and MYSQL. I have a dynamic list of options which the user can select (maximum of 3) that are added from the administration panel as shown below:
<input type="checkbox" name="<?php echo "category".$i; ?>"
value="<?php echo $cat_id; ?>" />
<?php echo $cat_name; ?><br />
<?php
$i++;
}
echo "<input type='hidden' name='num_cat' value='$num_cat' />";
?>
I now want to count how many check boxes are 'checked' and if there are more than 0 and less than 4 checked it will update the mysql table with these stored. They are stored by means of 1's and 0's. So they tick 'yes' and a 1 is stored, they tick 'no' and a 0 is stored.
I've been trying to use jQuery and Javascript but they all seem to be for Check Box Forms which have the values pre-written within a form, mine are dynamic from a database.
Many thanks for your help.
Use [] in the end of your checkbox names and use the sizeof function on the corresponding $_POST[] array in your php script.
<input type="checkbox" name="values[]" value="1" />Value 1
<input type="checkbox" name="values[]" value="2" />Value 2
sizeof($_POST['values']);
Note that the checkboxes have the same name and end with brackets (values[]). This indicates that the checkboxes belong together and are bundled as an array in php. Only the selected values will be present in the array as well.

How do I call up values in PHP for user input in forms (radio buttons and selects)

Ok so my admin sets to edit a book which was created. I know how to bring in the values that were initially entered via a simple text field like 'bookname'. On the edit book page the book name field stores the currently assigned 'bookname' in the field (which is what I want! :) )
However I have other field types like selects and radio button entries...I'm having trouble calling in the already set value when the book was created.
For example, there is a 'booklevel' field, which I have set as radio button entries as; Hard, Normal, and Easy. When the user goes to edit the book, I'm not too sure on how to have the current value drawn up (its stored as text) and the radio button being checked. I.e. 'Normal' is checked if this is what was set when the book was created. So far I have this as the code for the adding book level:
<label>Book Level:</label> <label for="booklevel1" class="radio">Hard
<input type="radio" name="booklevel" id="booklevel1"
value="<?php echo 'Hard'; if (isset($_POST['booklevel'])); ?>"></label>
<label for="booklevel2" class="radio">Medium<input type="radio" name="booklevel"
id="booklevel2"
value="<?php echo 'Normal'; if (isset($_POST['booklevel'])); ?>"></label>
<label for="booklevel" class="radio">Low<input type="radio" name="booklevel"
id="booklevel3"
value="<?php echo 'Easy'; if (isset($_POST['booklevel'])); ?>"></label>
This all works fine by the way when the user adds the book... But does anyone know how in my update book form, I can draw the value of what level has been set, and have the box checked?? To draw up the values in the text fields, I'm simply using:
<?php echo $row['bookname']?>
I also noticed a small issue when I call up the values for my Select options. I have the drop down select field display the currently set user (to read the book!), however, the drop down menu again displays the user in the list available options to select - basically meaning 2 of the same names appear in the list! Is there a way to eliminate the value of the SELECTED option? So far my setup for this is like:
<select name="user_id" id="user_id">
<option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['fullname']?></option>
<?php
while($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['user_id']?>"><?php echo $row['name']?></option>
<?php } ?>
</select>
If anyone can help me I'll be very greatful. Sorry for the incredibly long question!! :)
For the radio buttons, simply add the attribute checked="true" if the current button's value matches the value you already have. For the dropdown, don't use the first option you have there, but instead use the same technique as the radio buttons, but with selected="true".
Check to see which string is stored in the DB (easy, normal, hard) and then set the checked field to 'checked'. For example:
<label>Book Level:</label> <label for="booklevel1" class="radio">Hard
<input type="radio" name="booklevel" id="booklevel1"
value="hard" <?php if($row['booklevel'] == 'hard') echo 'checked="checked"'; ?>></label>
As to the second part - you'll need to store the first value of $row['user_id'] in a temporary variable before entering the while loop. Then check each additional user_id you receive to determine whether it is equal. If it is, don't print its option. Like so:
<?php
$tmp = $row['user_id'];
while($row = mysql_fetch_array($result)) {
if($row['user_id'] != $tmp) {
echo '<option value="'.$row['user_id'].'">'.$row['name'].'</option>';
}
}
?>

Categories