Why is JavaScript not setting the value of a hidden field? - javascript

I have a PHP form to submit and I'm trying to get (vanilla) JavaScript to set the value of a hidden field. For some reason, the hidden field (deleteRowId) is coming through as blank. Any help would be much appreciated.
Here is my code:
<?php
if (isset($transcribercertifications)) {
echo form_open('user/deletecertification/' . $tid, 'id="deleteCert"');
echo '<table>';
$count = 0;
if ($transcribercertifications) {
foreach ($transcribercertifications as $transcribercertification) {
echo '<tr><td>' . $transcribercertification . '</td>';
echo '<input type="hidden" name="certification[' . $count . ']" id="certification[' . $count . ']" value="' . $transcribercertification . '">';
echo '<input type="hidden" id="deleteRowId" name="deleteRowId" value="">';
echo '</td><td title="Delete certification"><button data-row-id="' . $count . '" id="deleteCert" onclick="deleteRow(' . $count . ');">X</button></td></tr>';
$count++;
}
}
echo '</table>';
echo form_close();
}
?>
<script type="text/javascript">
function deleteRow(rowCount) {
event.preventDefault()
alert('This will delete the certification. Are you sure ?');
alert(rowCount); // This value comes through correctly.
document.getElementById('deleteRowId').value = rowCount;
document.getElementById('deleteCert').submit();
}
</script>
When the function gets called, deleteRowId is always blank. If there's another way to set the value of deleteRowId so the function knows which certification to delete, I would be interested in seeing that too.

First of all id="deleteRowId" must be unique
and you can use setAttribute method
worked exampe
<input type="hidden" value="111" id="uniqID1">
<input type="hidden" value="111" id="uniqID2">
<script>
let aaa = document.getElementById("uniqID1");
aaa.setAttribute('value',222);
console.log(aaa.value)
</script>

Id cannot repeat.
in foreach loop use as id="deleteRowId"'.$count.'"" id="deleteCert"'.$count.'""
foreach ($transcribercertifications as $transcribercertification) {
echo '<tr><td>' . $transcribercertification . '</td>';
echo '<input type="hidden" name="certification[' . $count . ']" id="certification[' . $count . ']" value="' . $transcribercertification . '">';
echo '<input type="hidden" id="deleteRowId"'.$count.'"" name="deleteRowId"'.$count.'"" value="">';
echo '</td><td title="Delete certification"><button data-row-id="' . $count . '" id="deleteCert"'.$count.'"" onclick="deleteRow(' . $count . ');">X</button></td></tr>';
$count++;
}
and in JS function
getElementByID('deleteRowId'+rowCount).
getElementByID('deleteCert'+rowCount)

Related

Duplicate comment when trying to reply to comment

Good day to all, I ran into such a problem that when I try to reply to a certain comment, all comments are duplicated and at the same time duplicate comments are not displayed in the database, but only on the page. It feels like when you send a comment, not only one comment is sent, but some else, at least, it seems, the request to the database is made correctly + information is displayed in the same way:
enter image description here enter image description here enter image description here enter image description here
Php code:
<?php foreach ($showcomn as $result) {?>
<?php foreach ($showreplycom as $resultreply) {?>
<?php
if (isset($_SESSION['all-good'])) {
echo '<p id="user-comment-info">'.$result['name'].' '.$result['date'].'<br></p>',
'<div id="comment-user">',
'<p id="stealth-'.$result['id'].'">'.$result['comment'].'</p>',
'</div>',
'<form method="post" action="./check%20info/editsavechanges.php">';
if ($findid['id'] === $result['userID']){
echo '<textarea id="id-edit-save-'.$result['id'] .'" name="id-each-user" hidden>'.$result['id'].'</textarea>',
'<textarea name="edit-comment" id="edit-user-comment-'.$result['id'].'" class="edit-style" hidden >'.$result['comment'].'</textarea>',
'<div id="buttons-edit">',
'<button type="submit" name="save-edit-comment" id="save-edit-user-comment-'.$result['id'].'" hidden>Save</button>',
'<button type="button" onclick="cancelEdit('.$result['id'].')" id="cancel-edit-user-comment-'.$result['id'] .'" hidden>Cancel</button>',
'</div>',
'<div id="buttons-forms">',
'<button type="button" onclick="showForm('.$result['id'].')" id="open-edit-'.$result['id'].'" >Edit</button>',
'<button type="submit" name="delete-edit-comment" id="delete-edit-'.$result['id'].'" >Delete</button>',
'</div>';
}
echo '<div id="buttons-forms-reply">',
'<button type="button" onclick="showformReply('.$result['id'].')" id="reply-edit-'.$result['id'].'" >Reply</button>',
'</div>',
'<textarea name="reply-userid" id="comment-userid-reply" class="edit-style-user-id" readonly hidden>'.$findid['id'].'</textarea>',
'<textarea name="reply-user-name" id="comment-name-reply" class="edit-style-user" readonly hidden>'. $_SESSION['all-good']['login'] .'</textarea>',
'<textarea id="reply-save-comment'.$result['id'] .'" name="reply-comment-id" hidden>'.$result['id'].'</textarea>',
'<input type="hidden" name="reply-user-date">',
'<textarea name="reply-comment" id="reply-text-'.$result['id'].'" class="edit-style-reply" hidden ></textarea>';
echo '<div id="reply-buttons">',
'<button type="submit" name="save-reply-comment" id="save-reply-edit-'.$result['id'] .'" hidden>Save</button>',
'<button type="button" onclick="cancelReply('.$result['id'].')" id="cancel-reply-edit-'.$result['id'] .'" hidden>Cancel</button>','<br>',
'</div>',
'<div id="show-comments-reply">',
'<button type="button" onclick="showallReply(' . $resultreply['id'] . ')" id="show-reply-all-' . $resultreply['id'] . '" class="all_reply_button">Show all comments</button>',
'<button type="button" onclick="closeallReply(' . $resultreply['id'] . ')" id="close-reply-all-' . $resultreply['id'] . '" class="close_reply_button" hidden>Close all comments</button>',
'</div>',
'<div id="new_box_reply">',
'<p id="all-reply-com-' . $resultreply['id'] . '" class="reply_box" hidden>' . $resultreply['name'] . ' ' . $resultreply['date'] . '<br>' . $resultreply['comment'] . '</p>',
'</div>',
'</form>';
Here realization reply form
<?php include_once "../connection/coments_connection.php";
date_default_timezone_set('Europe/Kiev');
if (isset($_POST['save-reply-comment'])) { $commentID = $_POST['reply-comment-id'];
$name = $_POST['reply-user-name'];
$userID = $_POST['reply-userid'];
$comment = $_POST['reply-comment'];
$data = date('Y-m-d H:i:s');
$replyBase = "INSERT INTO `replycom`(`commentID`, `name`, `comment`, `date`, `userID`) VALUES ('$commentID', '$name', '$comment', '$data', '$userID')";
$resultReply = $Gcom->prepare($replyBase);
$resultReply->execute([
'reply-comment-id' => $commentID,
'user-name' => $name,
'reply-comment' => $comment,
'reply-user-date' => $data,
'reply-userid' => $userID,
]);
header('Location: ../index.php#contacts'); }
Here's a Foreach loop for the main comments:
<?php
include_once "./connection/coments_connection.php";
$showcomn = $Gcom->query("SELECT * FROM `comments` ORDER BY date DESC ");
$showcomn = $showcomn->fetchAll();
Here's a Foreach loop for reply comments:
<?php
include_once "./connection/coments_connection.php";
$showreplycom = $Gcom->query("SELECT * FROM `replycom` ORDER BY date DESC ");
$showreplycom = $showreplycom->fetchAll();

how to disable checkbox after due date has passed

I've this code in php, and it retrieve all the value I've in my database, and I want the user not be able to choose or tamper with the one that has expired date,
please can you help me ?
my code below only show u the word 'Expired' and 'Not Expired' on the web, since I'm only Echoing it.
I want to just disable the check box, and not let the user be able to change anything like the statues or remove the job.
please can you help me ?
$result = mysqli_query($dbCIE,$sqlCommand) or die(mysql_error());
echo "<form action='JobsLists.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
$exp_date = $row['DueDate'];
$today_date = date('Y/m/d');
$exp=strtotime($exp_date);
$td=strtotime($today_date );
if($td>$exp){
echo"Expiered";
}else{
echo"NOT Expiered";
}
echo "<tr>";
echo "<td> <input type='checkbox' id='myCheck' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td>"."<select name='JobStatus[".$row['JobId']."]'>";
if($row['JobStatus'] == "InProgress"){
echo "<option value='InProgress' selected>In Progress</option>";
echo "<option value='Completed'>Completed</option>";
} else {
echo "<option value='InProgress'>In Progress</option>";
echo "<option value='Completed' selected> Completed </option>";
}
echo "</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";
?>
Why not just not print it out?
if (!$td->exp)
echo "<td> <input type='checkbox' id='myCheck' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
It's better to just not show a button or input than to disable it sometimes. Inputs can be disabled with the DISABLED keyword
<input type="checkbox" name="foo" DISABLED>

I had an issue with bootstrap's datetime picker on MozilaFirefox

Hello guys I had an issue with datetimepicker on FF. In other browsers its doing good with the code I wrote. The problem is I have three drop down options. In each one I had, time is inserted by the user using datetimepicker but unfortunately the picker works only on one of the three of them. Any idea why this is happening
PHP CODE
<?php
echo '<div class="table-rsponsive overlap">';
echo '<table class="table" >';
$workingDaysNum = array();
$counter = 0;
for ($i = 0; $i < 3; $i++) {
echo '<tr>';
for ($j = 0; $j < 7/* count($spec) */; $j++) {
if ($i == 0) {
echo '<td>' . $days[$j] . '</td>';
} else {
$counter++;
$workingDaysNum[] = 'datetimepicker' . $counter;
?>
<td>
<div class="input-group date datetimepicker" id="<?php echo $workingDaysNum[$counter - 1] ?>">
<input class="form-control " id="filter-date" size="16" type="text" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-time"></span>
</span>
</div>
</td>
<?php
}
}
echo '</tr>';
}
JQUERY CODE
$(function () {
<?php
foreach ($workingDaysNum as $workDay) {
echo '$(' . $workDay . ').datetimepicker({';
echo 'format: \'HH:mm\',';
echo 'stepping: 30';
echo '});';
}
?>
<?php
foreach ($workingDaysNum as $workDay) {
echo '$("' . $workDay . '").on("dp.change",function (e) {';
echo 'saveSpec1Modal("' . $workDay . '", $("' . $workDay . '").data(\'date\'))';
//echo 'console.log($("#'.$workDay.'").data(\'date\'))';
echo '});';
}
?>
});
Ditch the loops in the jQuery.
Instead of activating datetimepicker by ID, you have already given them all a class, so you can run it once instead of looping.
$(function () {
$('.datetimepicker').datetimepicker({
format: 'HH:mm',
stepping: 30
});
});
Same applies to the change function.

jquery $(this).data selector

// Rate videos start
$('[value="bad"]').click(function (e) {
console.log($(this));
alert($(this).data("primary"));
});
echo '<div class="carousel-inner">';
echo '<div class="item active">';
echo '<h4>' . $vid->VIDEO_NAME . '</h4>';
echo '<input type="text" class="" data-primary="' . $vid->ID . '" value="' . $vid->ID . '">';
echo '<video width="500" height="281" controls>';
echo '<source src="' . base_url() . $vid->VIDEO_URL . '" type="video/mp4">';
echo 'Your browser does not support the video tag.';
echo '</video>';
echo '</div>';
echo '</div>';
echo '</div>';
I'm trying to get the ID of each video element but it returns undefined.
Set an identifier like id or class on your text field, then add a listener to the click event on that object:
// If you set by Class
$('.textToClick').click(function (e)
{
alert($(this).data("primary"));
});
// If you set by ID
$('#textToClick').click(function (e)
{
alert($(this).data("primary"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Setting identifier by Class -->
<input type="text" class="textToClick" data-primary="dataPrimaryFromClass" value="Class identifier">
<!-- or setting identifier by id -->
<input type="text" id="textToClick" data-primary="dataPrimaryFromID" value="ID identifier">

Return a Javascript array to a hidden input value

I am having trouble that will change the value of a hidden input type. I have a search that has multiple checkboxes created with a PHP while loop. I want to pass the items that are checked into a variable in PHP once submitted. This is the form with checkboxes and hidden value.
$search_sql = "SELECT * FROM `company` WHERE `groups` = '$groups' AND (`companyname` LIKE '%$search%' OR `directurl` LIKE '%$search%' OR `email` LIKE '%$search%' OR `phone` LIKE '%$search%' OR `groups` LIKE '%$search%' OR `notes` LIKE '%$search%')";
$result = mysql_query($search_sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
//remove the http from the links
$website = $row['directurl'];
$website = str_replace("http//", "", "$website");
$website = str_replace("http://", "", "$website");
$website = str_replace("https://", "", "$website");
$website = str_replace("https//", "", "$website");
//button to change colour
//display contacted companies
if($row['contact'] == 0){
//not contacted
$changecolor = '1';
$contacted = '<tr class="nocontact border">';
$button = '<td>
<form name="change_colour" method="post">
<input type="hidden" name="id" value="' . $row['id'] . '">
<input type="hidden" name="change_colour" value="' . $changecolor . '">
<input type="hidden" name="search" value="' . $row['companyname'] . '">
<input type="hidden" name="groupselected" value="' . $groups . '">
<input type="submit" value="C"></form>';
}
//contacted
elseif($row['contact'] == 1){
$changecolor = '2';
$contacted = '<tr class="contact border">';
$button = '<td>
<form name="change_colour" method="post">
<input type="hidden" name="id" value="' . $row['id'] . '">
<input type="hidden" name="change_colour" value="' . $changecolor . '">
<input type="hidden" name="search" value="' . $row['companyname'] . '">
<input type="hidden" name="groupselected" value="' . $groups . '">
<input type="submit" value="C"></form>';
}
//positive feedback
elseif($row['contact'] == 2){
$changecolor = '3';
$contacted = '<tr class="positive border">';
$button = '<td>
<form name="change_colour" method="post">
<input type="hidden" name="id" value="' . $row['id'] . '">
<input type="hidden" name="change_colour" value="' . $changecolor . '">
<input type="hidden" name="search" value="' . $row['companyname'] . '">
<input type="hidden" name="groupselected" value="' . $groups . '">
<input type="submit" value="C"></form>';
}
//negative feedback
elseif($row['contact'] == 3){
$changecolor = '0';
$contacted = '<tr class="negative border">';
$button = '<td>
<form name="change_colour" method="post">
<input type="hidden" name="id" value="' . $row['id'] . '">
<input type="hidden" name="change_colour" value="' . $changecolor . '">
<input type="hidden" name="search" value="' . $row['companyname'] . '">
<input type="hidden" name="groupselected" value="' . $groups . '">
<input type="submit" value="C"></form>';
}
else {echo 'error with the display';}
//button to select companies
$select_comp = '<form name="select_comp" method="post">
<input type="checkbox" id="' . $row['id'] . '" name="id" onclick="compTrig(' . $row['id'] . ')">
<input type="hidden" id="return_comp" name="return_comp" value="me">
<input type="submit" name="select_comp" value="Selected"></form></td>';
//Display link to website if available
if ($website !== ''){
$webdisplay = $contacted . $button . $select_comp . '<td><a target="_blank" href="http://' . $website . '">' . $row['companyname'] . '</a></td>';
}
else{
$webdisplay = $contacted . $button . '<td class="red">' . $row['companyname'] . '</td>';
}
//check if email has been submitted
if ($row['email'] !== ''){
$email = '<td>Email</td>';
}
else{
$email = '<td class="red">None</td>';
}
//display company details
echo $webdisplay;
echo '<td>' . $row['contactname'] . '</td>';
echo $email;
echo '<td>' . $row['town'] . '</td>';
echo '<td>' . $row['phone'] . '</td>';
echo '<td>' . $row['notes'] . '</td></tr>';
$companyfind = $row['id'];
}
and the Javascript below:
var select_comp = new Array();
var element = document.getElementById('return_comp');
function compTrig(clickedid)
{
if (document.getElementById(clickedid).checked == false)
{
return false;
}
else
{
select_comp.push(clickedid);
var clicked = clickedid;
var box= confirm(select_comp);
if (box==true)
return true;
else
document.getElementById(clickedid).checked = false;
var index = select_comp.indexOf(clicked);
if (index > -1)
{
select_comp.splice(index, 1);
}
}
select_comp.toString();
element.value(select_comp);
}
I can get a confirmation box to display the correct values but i cannot change the value of the hidden value which would work. I was thinking that it may be because the form is in a while loop and the form should be outside of the loop. Any help of this matter will be greatly appreciated.
You're doing this the opposite way 'round how it should be done and exposing too much information to be tampered with in the front-end. It's also causing a lot of unnecessary redundancy (redundancy is the programmer's mortal enemy). No Javascript is needed for this task.
All you should be passing to the browser is the relevant company information to populate a single form; then building the appropriate output in PHP after submission.
search_results.php
echo "<form name=\"select_comp\" method=\"POST\" action=\"select_comp.php\">";
$search_sql = "
SELECT id FROM `company` WHERE `groups` = '$groups'
AND (`companyname` LIKE '%$search%'
OR `directurl` LIKE '%$search%'
OR `email` LIKE '%$search%'
OR `phone` LIKE '%$search%'
OR `groups` LIKE '%$search%'
OR `notes` LIKE '%$search%')";
$result = mysql_query($search_sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<input type=\"checkbox\" name=\"id[{$row['id']}]\" value=\"1\"> {$row['company_name']}<br>";
// echo other company details
}
echo '
<input type="submit" value="C">
</form>';
This will give you an array of selected checkboxes in the $_POST variable:
select_comp.php
// $_POST['id'] will contain an array of selected checkboxes
// implode array into a comma-separated list for use with MySQL IN operator
// escaping a string where the numbers have been tampered with will cause an error, but it will be safe from injection
$sql = "
SELECT * FROM `company` WHERE `id`
IN (" . mysql_real_escape_string(implode(',', $_POST['id'])) . ")";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
switch ($row['id']) {
// first company
case '0':
// build styling here
break;
// second company
case '1':
// build more styling here
break;
// and so on
}
// output HTML with styling information computed above
}
But, what you should really be doing is setting the styling dynamically with values entered into the database and/or determined by file naming convention (while involving absolute validation of dynamically-generated styling specifications, of course).

Categories

Resources