fetch data from database and check the checkbox - php

i have a problem here to check the checkbox from database.
because there is textbox inside my checkbox.
My checkbox looks like this : https://jsfiddle.net/qqeh07e5/
First of all. It is okay to check the checkbox if there no textbox inside checkbox, because i saved the value of checkbox. But if there is a textbox inside the checkbox, i don't save checkbox's value, but save textbox's value. So my problem how to make the value from database know that its belong textbox A, not textbox B.
Here is i've done so far.
<?php
$temp_detailEvent = $_SESSION['detail_event'];
$q = sqlsrv_query($conn,"SELECT * FROM EventDetail WHERE id_eventDetail='$temp_detailEvent'");
while($row = sqlsrv_fetch_array($q))
{
$tuj = explode(",",$row['tujuanTraining']);
<div class="col-lg-4">
<div class="form-group">
<label>Latar Belakang</label>
<div class="checkbox">
<label>';
?>
<input type="checkbox" name="tujuan[]" id="cekbox_tujuan1" <?php if(in_array("Pencapaian Kompetensi Karyawan",$tuj)) { ?> checked="checked" <?php } ?> value="Pencapaian Kompetensi Karyawan">Pencapaian Kompetensi Karyawan
<?php
echo '</label>
</div>
<div class="checkbox form-inline">
<label>
<input type="checkbox" id="cekbox_latarbelakang2" value="Mengacu pada Analisa GAP pada FPT Nomor">Mengacu Pada Analisa GAP pada FPT Nomor
</label>
<input type="text" id="textbox_latarbelakang1" name="latarbelakang[]" class="form-control input-sm" placeholder="">
</div>
<div class="checkbox form-inline">
<label>
<input type="checkbox" id="cekbox_latarbelakang3" value="">
Lain - lain
</label>
<input type="text" id="textbox_latarbelakang2" name="latarbelakang[]" class="form-control input-sm" placeholder="Lain - lain">
</div>
</div>
</div>';
}
i am just check if there is a value same in database with in_array
Hope someone can help me, thank you very much. Hv a nice day and CHEERSS!!

Related

Spit data to the select field from the database

enter image description hereI am new to coding. I just started building a website and I am currently working on the profile page. I wanted the input fields to be populated by the information the user has provided (see attached picture). This I have achieved.
My problem now is... I can't spit information from the database to the select tag.
For the other input fields, I just had to echo the data spooled from the database as seen in my HTML below:
<div class="col-sm-6 white-bg">
<div class="row">
<div class="col-sm-6">
<label>
<span class="text-left">FIRST NAME</span>
<input type="text" name="fname" value="<?php echo $first_name ?>" disabled>
</label>
</div>
<div class="col-sm-6">
<label>
<span class="text-left">LAST NAME</span>
<input type="text" name="lname" value="<?php echo $last_name ?>" disabled>
</label>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label>
<span class="text-left">GENDER</span>
<select name="gender">
<option value="" disabled hidden selected>--Select--</option>
<option value="Female">Female</option>
<option value="Male">Male</option>
<option value="Others">Others</option>
</select>
</label>
</div>
<div class="col-sm-6">
<label>
<span class="text-left">DATE OF BIRTH</span>
<input type="DATE" name="dob" value="<?php echo $dob ?>">
</label>
</div>
</div>
</div>
But I can't do the same for the select tag. How do I have an option selected based on the information the user has provided such that the Male option is selected if the user entered male or the the Female option is selected if the user entered Female.
You should add the keyword based on the value returned from your DB.
So in your code add something like
-- For your default select option do
<?php echo(empty($theGender)?" selected":""); ?>
-- For Male/Female do, checking for value you stored
<option value="Female"<?php echo(($theGender === "female")?" selected":""); ?> >Female</option>

hide/show php checkbox by id like www.anysite.php?id=10

I have a form and a checkbox. By click on the checkbox, a div within some other divs will show up or hide. This works! But, if i visit my page with an id like xxxxx.php?id=10, the checkbox is checked but the divs are hide. if i click on the checkbox, the divs will show up, but the checkbox is unchecked (bad for e.g. mysql updates).. hope anyone can help me by this issue..
Here my code:
$(document).ready(function() {
$('#Show').hide();
$("input[name=mycheckbox]").click(function () {
$('#Show').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<div class="form-group">
<div class="form-group">
<div class="checkbox-inline">
<label>
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="yes"> my option
</label>
</div>
</div>
</div>
<div id="Show">
<div class="form-group">
<label for="textinput" class="control-label">Company </label>
<div class="">
<input type="text" class="form-control" name="name" id="name" placeholder="" value="">
</div>
</div>
</div>
</form>
My input field have this code (php doesn't work in the snippet):
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="yes" <?php if (isset($_GET['id'])) {echo $row->mycheckbox == "yes" ? 'checked="checked"' : "";}?>
thanks for your help!
I would recommend to use change event, trigger using .trigger('change') on page load so that the div in sync with the state of checkbox element.
Also you should .toggle(display)
$("input[name=mycheckbox]").change(function () {
$('#Show').toggle(this.checked);
}).trigger('change');

PHP button in a for each loop

I am displaying user details on a form using a for each loop. In the for each loop i am adding a button below the user details to delete the information for each user.
<?php
foreach( $Names as $Name )
{
$dbQuery = $db->prepare("select * from user WHERE Name = ?");
$dbQuery->execute(array($Name));
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC))
{
$ID = $dbRow['ID'];
$Email = $dbRow ['Email'];
}
?>
<div class="form-group">
<label for="hotel_id" class=" col-sm-4 control-label">ID:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="ID" name="ID[]" placeholder="Enter City Name" value="<?php echo $ID?>">
</div>
</div>
<div class="form-group">
<label for="Name" class="col-sm-4 control-label">Name:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Name" name="Name[]" value="<?php echo $Name?>">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-4 control-label">Email:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Email" name="Email[]" value="<?php echo $Email ?>">
</div>
</div>
<div class ="form-group">
<label for="removeUser" class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<input id="removeUser" name="removeUser" type="submit" value="Remove this user" class="btn btn-danger" onclick="return confirm('Are you sure you want to completely remove this user?');">
</div>
</div>
<?php
}
?>
I know how to delete the users but the problem i am having is that i cant seem to get the id for the specific user when the button is clicked. So far i have only been able to retrieve the last id displayed or all of the id's displayed.
Any help would be appreciated. Thanks.
First, you'll need to move the closing loop brace to the end of the html of each row. Then I recomend you to do a separated URL for deleting a record, and changing the submit button to a anchor tag (by CSS rules you can make it to look like a button), so the anchor will be:
Delete
So in the backend of delete.php you'll have something like:
if(isset($_GET['id'])){
$dbQuery = $db->prepare(sprintf("DELETE FROM user WHERE ID = %s", $_GET['id']));
$dbQuery->execute(array($Name));
}
You'll need to improve this delete query so it will be safe, because people might send you SQL code trough $_GET['id']
Your while loop is wrong, because last one is fetched and then html is rendered. Try add that html under while loop
Please add ID in hidden filed in a form. So you can get id each form submit like normal form field value. Add ID to hidden field value.

Check textarea is empty and checkbox was selected at least one when submit using jquery

I develop an app using codigniter. In view, I have a form like this :
<div class="box-content">
<?php
$properties = array('class' => 'form-horizontal', 'id' => 'myForm');
echo form_open("control_request/userRequest", $properties);
?>
<fieldset>
<div class="control-group">
<label class="control-label">Jenis Request :</label>
<div class="controls" id="chekboxes">
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Login" value="Login" > Login </label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Printer" value="Printer"> Printer </label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Monitor" value="Monitor"> Monitor</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Computer" value="Computer"> Computer</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Network" value="Network"> Network</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Lain-lain" value="Lain-lain" > Lain-lain</label>
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="Complaint" Complaint : </label>
<div class="controls">
<textarea class="cleditor" name="Complaint" id="Complaint" rows="3"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="submit" >Submit</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
<?php echo form_close(); ?>
Now, I create some javascript using jquery to validate, if the checkbox and textarea was qualify before submit. Here is the logic of jquery :
<script>
$(document).ready(function() {
$("#submit").click(function(e){
if (chekbox not selected at least one ) {
<p> Please choose at least one </p>
}
if textbox is empty {
<p> Pleaser fill the textbox </p>
}
if (all qualified)
submit
});
</script>
I just wanna check all of the element is qualified before submitting. But if not qualified, there will be a error message like this
()Checkbox1 ()Checkbox2 ()Checkbox3 ()Checkbox 4 <p>Please select one</p>
<textarea></textarea> <p>Please describe your complaint</p>
I am using jquery 1.9.1 , I will keep learn, and for the help thank you so much..
JSFIDDLE
Your question is not clear ..but it may help you what do you need
$("textarea:empty").length == 0 // text area empty
$(".checkbox [type=checkbox]:checked").length >= 1 // at least one is selected
If you need to make the textarea mandatory, use requierd
attribute. The jQuery validate plugin that you have used will do the
rest of the trick for validation.
For checking if at least one of the checkboxes is selected, you can
add one rule for checkboxes:
rules: {
'request[]': {
required: true,
minlength:2
}
},
OR
You can add a custom rule:
$.validator.addMethod("request[]", function(value, elem, param) {
if($(".request[]:checked").length > 0){
return true;
}else {
return false;
}
},"You must select at least one checkbox to continue");

Input value equal with previous enter value

let's say that I have a form
<form >
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="numeinculpat2">Nume inculpat2</label>
<div class="controls">
<input type="text" id="numeinculpat2" placeholder="Nume inculpat2" name="nume" />
</div>
</div>
</form>
And the user introduce some empty value .
How cand can I do if someone enter wrong data(or empty),when enter again, the input to have the value of data that he previous introduced ??
Add a value attribute and get last value from $_POST like below
<input
type="text"
id="numeinculpat2"
placeholder="Nume inculpat2"
value="<?php echo (isset($_POST['nume']) ? $_POST['nume'] : '' ); ?>"
name="nume"
/>
Input name must be unique, don't use same nume for other input, This might help
You are missing the action and the method attribute of form.
Follow the basic tutorial from here.
http://www.w3schools.com/php/php_form_complete.asp
If you are submitting it to the same page use " method="post">. Now when you submit and if the value is blank or whatever is the problem you will get the last entered value also use this to get the value.
if(isset($_POST["submit"])
{
$text1=$_POST["numeinculpat"];
}
over your html input
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" value="<?php echo $text1 ?>" />
</div>
</div>
Now when you submit the value you will get the last entered value.

Categories