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[]"
Related
I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!
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.
A little about my code:
I have code that dynamically displays emails with a checkbox next to each email.
<?
foreach($er as $row){ ?>
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="check" checked="checked">
<?
echo $row[email]."<br><br>";
echo "<input name='emailID' id='emailID' type='hidden' value='".$row[emailID]."' />";
} $emailquery->execute(); ?>
I can't seem to come up with a way that deletes the emailID of each email from a specific database table when you uncheck the checkbox. When you re-check the checkbox, I want to insert it back into the database table.
The emails won't go away, because they are stored in a completely different table than the one I want to insert/remove it from.
I know this is kind of a full question, so I will answer any questions you may have. Thank you in advance for all your help!
first, change your input to
<input name="emailcheckbox[]" value="<?php echo htmlspecialchars($row[email]);?>" ...
so, after you post this form back to server you will have
$_POST['emailcheckbox'] == array('checkedemail1', 'checkedemail2'...)
so you will need to delete all e-mails from your table and insert emails from this array, with this you will delete unchecked ones and save only checked ones
You can do this way:
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="[tablename][email]" checked="checked">
Insert Page:
<?
foreach($_POST['emailcheckbox'] as $item)
{
$query = "INSERT INTO ".$item[0]." VALUES(".$item[1].")";
.....
}....
?>
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.
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>';
}
}
?>