Checkbox Insert / Delete From Database - php

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].")";
.....
}....
?>

Related

why if conditon is not behaving properly in comparing values

why if conditon is not behaving properly in comparing values one which is coming from database and other coming from form via post..inside while loop of mysql fetch array
<?php
$right=0;
$wrong=0;
$result = mysql_query("SELECT * FROM mcq") ;
$total =mysql_num_rows($result);
echo "total questions are :".$total;
echo "<br>";
while($row = mysql_fetch_array($result))
{
$b=$row['id'];
$a=$_POST['a_'.$b];
$cor=$row['correct'];
if($a==$cor)
$right++;
else
$wrong++;
}
$a is coming from radio buttons from previous page and $cor is coming from database..i m comparing selected value of radio button with cor(correct answer of that value) which is coming from data base.. but condition is not executing rightly so please help me!!!!
The radio button just send one value. Your way to retrieve it is wrong, because you are basing in the ID, but the id should be used in the value.
Usually in HTML you should use some like this:
<input type="radio" name="myradio" value="item1" />
<input type="radio" name="myradio" value="item2" />
<input type="radio" name="myradio" value="item3" checked />
So, you should try to get the value from post:
$myvar = $_POST["myradio"];
Can you paste the radio button code?

how to retrieve values of multiple ids stored in a single field in db in php

<form action="" method="post">
Name : <input type="text" name="fname"/><br/>
Activities :
<?php
$sql_activities = "select * from tb_activities";
$query_activities = mysql_query($sql_activities);
$active = 1;
while($row_activities = mysql_fetch_array($query_activities)) {
?>
<input type="checkbox" value="<?=$row_activities['activity_id'];?>" name="activities<?=$active;?>"/>
<?=$row_activities["activity_name"];?>
<?php
$active++;
}
?>
<input type="submit" value="Save" name="save" />
</form>
Here is my form. Now in this form I am saving two fields name and activities, suppose I entered a name and I choose five activies that are coming from a table, suppose I choose five activities, at the time of insertion I am sending IDs of the activities in the table but not the activity name. My problem is that at the time of editing I want to show the activity names that the user chooses. How to show the names of the activities that the user have chosen? Can anyone help me?
you are not clear.i think you are new to php and asking about sql join statement.http://www.w3schools.com/sql/sql_join.asp
If I'm understanding the question correctly (and I may not be) if you change your checkbox to:
<input type="checkbox" value="<?php echo $row_activities['activity_id']; ?>"
name="activitiesChosen[]" >
Then when you submit your form you will pass an array (activitiesChosen) that contains all the visitors choices (as activity IDs).
Then, when you want to display the choices you get the array from the database and just use the php in_array command to see if they have chosen that activity.

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[]"

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