How can i Activate Multiple check boxes from mysql database using php - php

I have saved multiple courses id's in course field for student in Mysql database..
Ex : course : 1,5,7,9
if i edit the student how can i activate selected courses..?
enter image description here
<?php
$courses_query=mysql_query("select * from `course` where status='1'");
while($c_fetch=mysql_fetch_array($courses_query)){
?>
<input type="checkbox" name="course[]" value="<?php echo $c_fetch['id']; ?>" id="courseid" <?php if($re['course']==$c_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $c_fetch['course'];
?>
<?php
$cids=$c_fetch['id'];
$softw_qry=mysql_query("select * from `softwares` where course='$cids' and status='1'");
while($softw_fetch=mysql_fetch_array($softw_qry)){
?>
<input type="checkbox" name="software[]" value="<?php echo $softw_fetch['id']; ?>" <?php if($re['software']==$softw_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $softw_fetch['software']; ?>
<?php
$soft_ids=$softw_fetch['id'];
$topicsn_qry=mysql_query("select * from `topics` where course='$cids' and software='$soft_ids' and status='1'");
while($topicn_fetch=mysql_fetch_array($topicsn_qry)){
?>
<input type="checkbox" name="topics[]" value="<?php echo $topicn_fetch['id']; ?>" /> <?php echo $topicn_fetch['topic']; ?>
<?php } }} ?>

Related

Put the value as title in text field

i want to get the partcular text value as tittle in that field
How can i do..
<input type="text"
<?php if($name_one != '') { ?> value="<?php echo $name_one;?>" <?php echo set_value('name1'); ?>
<?php } else { ?> value="<?php echo set_value('name1'); ?>" <?php } ?>
name="act1" id="act1" title="<?php echo set_value('name1'); ?>">
You can try like this
<input type="text"
value="<?php echo (!empty($name_one)) ? $name_one : $name1 ; ?>"
Single line if condition

Send value from database to checkbox checked

I have a table with 3 fields looking_id,user_id,looking. looking_id is unique.
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=$dbg->query($sql);
<input type="checkbox" name="What_services[]" id="What_services" value="person_care" <?php if($stmt=='person_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="child_care" <?php if($stmt=='child_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="pet_care" <?php if($stmt=='pet_care')echo checked;?> >
but it's not working.
Try this
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=mysql_query($sql);
while($result = mysql_fetch_array($stmt))
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $result['looking'];?>" value="person_care" <?php if($result['looking']=='person_care')echo "checked";?> >
<?php
} ?>
For pdo try this
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
foreach ($stmt->query($sql) as $row)
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $row['looking'];?>" value="person_care" <?php if($row['looking']=='person_care')echo "checked";?> >
<?php
} ?>

Dynamic radio button with self-post using PHP & MySQL

I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />

Check checkbox depending on a value in database

I'm trying to check a checkbox based on its database value, i have tried the following but no joy, any suggestions?
I am using Kohana framework.
public static function defaultdistance(){
$result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
echo 'Value: '.$distancestore['value'];
}
}
<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label> </label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>
May be you need like this
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
?>
<label class="shortLabel">
<input type="radio" name="distance" value="10"
<?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> />
10 <?php echo $dict->transl('distance_km'); ?>
</label>
<?php
}
try this i think you have to use ternary operator to resolve this issue.
<input type="radio" name="distance" value="10"
<?php $select = $distancestore['value']=='10'?'checked':'';
echo $select; ?>
10 <?php echo $dict->transl('distance_km'); ?>

POST a hidden input + multiple options PHP

Hi there I'm quite new to PHP
I have this problem:
I would like to POST a multiple choice + a hidden field from a form:
<?php
if (isset($_SESSION['nickname']))
{
$result = mysql_query("SELECT * FROM users");
$teamsCount = ceil(mysql_num_rows($result)/2);
for ($i=1; $i<=$teamsCount; $i++)
{
// TEST: echo $i . " TeamsCount er: " . $teamsCount. "<br>";
?>
Team <? echo $i; ?>
<form name="addTeam" action="buildTeams.php" method="POST">
<input type="hidden" name="hiddenField" value="<?php $i; ?>" />
<select name="teams[]" multiple="multiple" size="<?php echo mysql_num_rows($result); ?>">
<?php
$query = mysql_query("SELECT * FROM users");
while ($row=mysql_fetch_array($query))
{
$id=$row["ID"];
$nick=$row["Nick"];
?>
<option value="<?php echo $id; ?>"><?php echo ucfirst($nick); ?></option>
<?php
}
?>
</select>
<input type="submit" value="Make them teams!!" />
</form>
<?php
}
}
?>
I think you have an error in this line:
<input type="hidden" name="hiddenField" value="<?php $i ?>" />
It should be
<input type="hidden" name="hiddenField" value="<?php echo $i ?>" />
Edit:
Put the team id in the select name. Example:
<select name="teams[<?=$i?>][]">
And in PHP do:
foreach ($_POST['teams'] as $team_id => $choices)
I think you should check $_POST['hiddenField'] to obtain hidden value

Categories