how to checked input checkbox datatable laravel? - javascript

i want to make a statement if is_participant is one then check the box, but i tried but it doesn't work
$user = User::with('regency.province');
return DataTables::of($user)
->editColumn('is_participant', function ($user) {
return '<input ' . $user->is_participant == 1 ? "checked" : "" . ' type="checkbox" id="' . $user->id . '">';
})
is there something missing in my code

Try to apply the below code. I have tried to change quote formatting just the way PHP accepts.
->editColumn("is_participant", function ($user) {
$checked = ($user-> is_participant == 1) ? 'checked' : '';
return ' <input type="checkbox" id="' . $user->id . '" ' . $checked . '> ';
});

Related

How do I fetch values from database and display them in select option in javascript

I have created below code to do calculation on the data fetched from database. Steps:
Fetch the value that will be used by javascript to do calculation from database and display using 'u" value="'
Do calculation using javascript using the values fetched. 'u'
All what i require is to fetch those values and use it to calculate in JavaScript but nothing happens.
Below code fetches the data and values
<select name="urgency" id="urgency" class="form-control" onchange="caltotal()">
<?php
$query = $conn->query("SELECT idurgency,amount_added,value,hours_day FROM cww_avid_urgency");
foreach ($query as $key => $row) {
if ($row['idurgency'] == $post['urgency']) {
echo '<option selected="selected" id="' . $row['idurgency'] . 'u" value="' . $row['idurgency'] . '" title="' . $row['amount_added'] . '">' . $row['value'] . $row['hours_day'] . '</option>';
}
else {
echo '<option selected="selected" id="' . $row['idurgency'] . 'u" value="' . $row['idurgency'] . '" title="' . $row['amount_added'] . '">' . $row['value'] . $row['hours_day'] . '</option>';
}
}
?>
</select>
Below is the input type to display the values after the calculation
<input type="text" name="cpp" id="cpp" value="10.95" class="form-control" maxlength="14" readonly>
<b>Currency Code</b>
</div>
<div class="col-sm-7">
<input name="tamount" type="text" id="tamount" value="10.95" class="form-control" readonly>
<b>Currency Code</b>
<span class="tamount"></span>
</div>
Below is the script to do calculations and to make changes onselect
<script type="text/javascript">
function caltotal() {
var x = document.getElementById("urgency").value;
if (document.getElementById("urgency").value == 'u') {
var u = 10.95;
document.getElementById("cpg").value = +u;
document.getElementById("ta").value = +u;
document.getElementById("np").value = +u;
} else if (document.getElementById("urgency").value == 'u') {
var u = 11.95;
document.getElementById("cpg").value = +u;
document.getElementById("ta").value = +u;
document.getElementById("np").value = +u;
} else if (document.getElementById("urgency").value == 'u') {
var u = 12.95;
document.getElementById("cpg").value = +u;
document.getElementById("ta").value = +u;
document.getElementById("np").value = +u;
}
</script>
The results that I expecting that when select urgency is selected the affects the display likewise the rest of selects.
There are a few things here that are not right with your code.
The if in your foreach loop prints the same result regardless of the
$row['idurgency'] making it redundant.
Option tags do not need an id attribute, and may even be causing the
problem. The id attribute should be unique and only assigned once per page.
Then some of your javascript is redundant as well.
function caltotal() {
var urgency = document.getElementById("urgency").value;
var amount;
if (urgency == 'u') {
amount = 10.95;
} else if (urgency == '?') {
amount = 11.95;
} else if (urgency == '??') {
amount = 12.95;
}
document.getElementById("cpg").value += amount;
document.getElementById("ta").value += amount;
document.getElementById("np").value += amount;
}
Just make sure you have only one element on the page that has one of the three names, cpg, ta, np.
I would advise you use more descriptive names for your variables it will help you in the long run.

Submission of radio button variable in php

I am writing a program which will display a list students of a class and in front of each student name there are three radio button for Absent, present and on leave.
The list of student is generated with while loop. Now I want to pass the value of radio button through a variable like this.
echo "<tr>"
. "<td>$id</td>"
. "<td>$name</td>"
. "<td><input type=radio name=$name value=P></td>"
. "<td><input type=radio value=L name=$name></td>"
. "<td><input type=radio name=$name value=A></td>"
. "</tr>";
Is this possible to send value of radio button like this?
Yes, but please do note that your input attributes should have single/double quotes.
echo "<input type=radio name='$name' value='p' />";
or you can escape double quotations like this
echo "<input type=radio name=\"$name\" value=\"P\">";
or concatenate php variables
echo '<input type=radio name="'.$name.'" value="P">";
UPDATE:
Checking each student's profile
Inside your while loop create an array name. An array name is like this, it has square brackets after the string 'attendance'.
<input name="attendance[]" />
Now, for each student's row. Assign the student ID inside the array name.
echo "<tr>"
. "<td>".$id."</td>"
. "<td>".$name."</td>"
. "<td><input type='radio' name='attendance[".$id."]' value='P'></td>"
. "<td><input type='radio' name='attendance[".$id."]' value='L'></td>"
. "<td><input type='radio' name='attendance[".$id."]' value='A'></td>"
. "</tr>";
Putting the $id inside your array name will serve as your pointer, use it in your backend to update each student's attendance status.
foreach ($_POST['attendance'] as $key => $value) {
//$_POST['attendance'] variable is an array.
//Where $key variable is your Student ID, use this to update their status
//Where $value is a student's selected attendance status.
echo 'Student ID:'. $key . ' is '. $value . '<br>';
//Update a student's attendance status using $key as their id.
}
You can also simulate this code I made: http://viper-7.com/Tb1lbo
Yes, this is possible. Code:
<?php
$students = [
[
'id' => '1',
'name' => 'Amy'
],
[
'id' => '2',
'name' => 'Bob'
],
[
'id' => '3',
'name' => 'Charlie'
]
];
echo
'<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' .
'<table>' .
' <thead>' .
' <tr>' .
' <th>Student Id</th>' .
' <th>Student Name</th>' .
' <th>Attendance (P / L / A)</th>' .
' </tr>' .
' </thead>' .
' <tbody>';
foreach ($students as $student) {
echo
' <tr>' .
' <td>' . $student['id'] . '</td>' .
' <td>' . $student['name'] . '</td>' .
' <td>' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="P" />' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="L" />' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="A" />' .
' </td>' .
' </tr>';
}
echo
' <tr>' .
' <td colspan="3">' .
' <input type="submit" name="submit" value="Attendance" />' .
' </td>' .
' </tr>' .
' </tbody>' .
'</table>' .
'</form>';
if (isset($_POST['submit'])) {
echo '<pre>' . print_r($_POST, true) . '</pre>';
}
And on form submit:
Array
(
[attendance_1] => P
[attendance_2] => L
[attendance_3] => L
[submit] => Attendance
)
First you need to include single or double quotes around the name like this:
<tr><td>$id</td><td>$name</td><td><input type='radio' name='$name' value='P'></td><td><input type='radio' value='L' name='$name'></td><td><input type='radio' name='$name' value='A'></td></tr>";
You can then use the name of the radio button group to get the value from the $_POST or $_GET superglobal depending on your request type.
$name = 'name_of_the_radios'
$studentPresence = $_POST[$name] //For get requests use $_GET
if($studentPresence == 'P')
{
//Student is present
}
else if($studentPresence == 'A')
{
//Student is absent
}
else if($studentPresence == 'L')
{
//Student is on leave
}
else
{
throw new Exception("Invalid student presence argument');
}

How can i pass String to onclick message?

public function confirmTransactionButton($confirmationFlag, $buttonName) {
$disabled = '';
$warningMessages = 'Watch Out';
$functionName = "return confirm(" . '\'' . "$warningMessages" . '\'' . ")";
if ($confirmationFlag == constant("Y.ENUM")) {
$disabled = 'disabled';
}
$button = "<input name='$buttonName' type='submit' class='BUTTON' id='$buttonName' onclick='$functionName' value='" . constant('KONFIRMASI.CON') . "' " . $disabled . " />";
return $button;
}
can anyone help me this issue, alert dialog won't appear.
i case i change string to number, its work
$warningMessages = 'Watch Out';
$functionName = "return confirm(" . '\'' . "$warningMessages" . '\'' . ")";
To
$warningMessages = '123';
$functionName = "return confirm(".$warningMessages.")";
The problem is hat you are using the same delimiter for the JS string and for the HTML attribute value, this terminating the attribute value prematurely. Look at the generated source and you will see:
It would look something like
<input ... onclick='confirm('foo')' />
Can you see the problem (the syntax highlighter helps)?
You can fix this by using different quotation marks:
$functionName = "return confirm(" . '"' . "$warningMessages" . '"' . ")";
// ^ ^
So your HTML will become
<input ... onclick='confirm("foo")' />
And of course the best solution would be to not use inline event handlers at all. Have a look at these articles to learn more about event handling.

How can I write PHP code into JavaScript/Ajax?

I want to know how to write a pice of PHP code into JavaScript/Ajax.
This is my PHP code:
if ($folder = opendir('data/Tasklist/')) {
while (false !== ($file = readdir($folder))) {
if ($file != '.' && $file != ".."){
$data=file_get_contents("data/Tasklist/".$file);
$poc=explode(";",$data);
echo '<li class="taskli">
<button id="'. $file . '" class="Del"> Delete </button>
'. $poc[0] . " " . $poc[1] . '<div class="hidinfo">' . $poc[2] . '</div></li>';
}
}
closedir($handle);
}
And i want to write : id="'. $file . '" inside this code:
$.post( "data/remove.php",{HERE})
Since you're storing the $file variable in the <button> id, you can grab it from there:
$('.Del').click(function(){
var file = $(this).attr('id');
$.post( "data/remove.php",{id:file});
return false;
});

Rewrite code from javascript to PHP

I have code in javascript:
if (imageData[imageCount].comments.data != null)
{
text = 'Comments Data:<br />';
imageData[imageCount].comments.data.forEach(function(comment){
text += comment.from.username + ': ' + comment.text + '<br />';
});
}
Which imageData[imageCount] refers to $data in PHP.
i've trying to rewrite self in PHP but it doesn't worked.
foreach ($contents->data as $data) {
if ($data->comments->data != null)
{
foreach($data->comments->data as $comment)
{
$text = comment->from->username + ': ' + comment->text + '<br />';
});
}
I'm sure to have problem with the code structure. It returns Invalid argument supplied for foreach()
-- Edit --
I've successed to convert the snippet to PHP. But i faced a new problem since i'm trying to create XML File from API (in this case Instagram API).
Look at productName and productPrice attribute from product section:
http://pastebin.com/ubGGyp9b
Value productName="Sushi Homemade " and productPrice="50.000 " is just for productID="002". But why the value also filled to next productID >= 002.
Is there mistake from this code:
<products>
<category categoryName="Instagram">
<?php
foreach ($contents->data as $data) {
foreach($data->comments->data as $comment){
if(preg_match('/#title/', $comment->text)){
$komen = preg_replace('/#title/', '', $comment->text);
break;
} else { $komen = 'No Title'; }
}
foreach($data->comments->data as $comment){
if(preg_match('/#price/', $comment->text)){
$harga = preg_replace('/#price/', '', $comment->text);
break;
} else { $harga = '0'; }
}
echo '<product productName="'. $komen .'" productID="' . $data->id . '" thumbPath="' . $data->images->thumbnail->url . '" productPrice="'. $harga .'">
<details>
<![CDATA[
<img src="' . $data->images->low_resolution->url . '" width="100%"/>
' . $data->caption->text . '
]]>
</details>
</product>
You forgot an $ in the last line and some brackets were not correctly set. To concat strings in PHP use "." instead of "+"
foreach ($contents->data as $data) {
if ($data->comments->data != null) {
foreach ($data->comments->data as $comment) {
$text = $comment->from->username . ': ' . $comment->text . '<br />';
}
}
}

Categories