Trying to call function Sel_item and pass it the fieldname1 variable as well as the id. The passing of the id works fine, but as soon as I try to pass the fieldname1, it dies. Basically trying to pass the id and the name of the person in mysql database to another function.
$id = $row["id"];
$fieldname1 = $row["person_name"];
$fieldname2 = $row["check_in_butt"];
$fieldname3 = $row["date_time"];
$str = "";
if($fieldname2 == true) $str = "checked";
echo '<tr>
<td>'.$fieldname1.'</td>
<td><input type="checkbox"'.$str. 'onclick="Sel_item('.$id.,.$fieldname1.')" </td>
I usually make it like this. Work for me
<?php
$id = $row["id"];
$fieldname1 = $row["person_name"];
$fieldname2 = $row["check_in_butt"];
$fieldname3 = $row["date_time"];
$str = "";
if($fieldname2 == true) $str = "checked";
?>
<tr>
<td><?= $fieldname1 ?></td>
<td><input type="checkbox" <?= $str ?> onclick="Sel_item('<?= $id ?>', '<?= $fieldname1 ?>')"></td>
</tr>
Related
I have a little trouble with my PHP, js code. When I submit form page is reloading but input checkbox isn't in checked mode, but I need is.
I think maybe I need to get parameter from URL and then compare it with the DOM element and after that set checked mode for input?
PHP code:
if(isset($_GET['user_id'])) {
try {
$PDO_connection = new PDO($dsn, $user, $password, $opt);
$queryForUserInfo = "SELECT position, dateOfBirth, rank, tellNumber, worker_id
FROM workers
WHERE user_id = :id";
$sth = $PDO_connection->prepare($queryForUserInfo);
$sth->bindParam(":id", $_GET['user_id'], PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$queryForComeAndReturnUserInfo = "SELECT date_come, date_return
FROM outside_records
WHERE worker_id = {$result[0]['worker_id']}
ORDER BY date_return DESC";
$sth1 = $PDO_connection->prepare($queryForComeAndReturnUserInfo);
$sth1->bindParam(":id", $_GET['user_id'], PDO::PARAM_STR);
$sth1->execute();
$outsideSchedule = $sth1->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e){
echo $e->getMessage();
};
}
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="get" id="listOfUsers_form">
<!--just for getting id to make db query-->
<?php foreach ($AllUsers as $user):?>
<tr class='usersList'>
<td><input type='checkbox' name='user_id' value='<?php if(isset($user)){echo $user['user_id'];}?>'></td>
<td><?php echo $user['name'];?></td>
<td><?php echo $user['surname'];?></td>
<td><?php echo $user['position'];?></td>
</tr>
<?php endforeach; ?>
</tbody>
</form>
js code:
<script>
$(document).ready(function(){
let row = $('#colorful_row').html();
if(row == "Відсутній на робочому місті") {
$("#status").css("background-color", "red");
} else if (row == "На робочому місці") {
$("#status").css("background-color", "lightgreen");
} else {
$("#status").css("background-color", "white");
}
$(".usersList td input:checkbox").on("change", function(){
$(this.attr("checked", "checked"));
$("#listOfUsers_form").submit();
});
});
</script>
Thanks, everybody
Use localStorage
Demo
For localstorage saved the boolean value as string .That why we compare with localStorage.getItem("box") == 'true'
$('input').prop('checked', localStorage.getItem("box") == 'true')
$('input').on('change', function() {
window.localStorage.setItem($(this).attr('name'), Boolean($(this).is(':checked')))
})
Here, If I write name of textbox is id from an array, then how it would be use in session.?
Updated:
My code is:
foreach($abc as $row)
{
<input type="text" name="<?php echo $row['id']; ?>" class="txt" id=txt_"<?php echo $row['id']; ?>" onblur="doAjax(this)"/>
}
Javascript code:
$(document).on('blur','.txt',function(){
$.ajax({
type: "GET",
url: "view_orders_checked_array.php",
data: {task: 'alltxt'},
async: false
});
});
In view_orders_checked_array.php:
if($task == "alltxt")
{
$_SESSION["textareaID"] = [];
foreach($rows as $row)
{
$al[] = array_push($_SESSION["textareaIDs"], $row["id"]);
}
print_r($al);
}
So, how it would possible to store textbox value in session array.?
Start the session at the top of every page that you want to access the session:
<?php session_start();
Then to add data to the session array:
<?php $_SESSION["foo"] = $bar; ?>
So if your textarea ID is $row["id"], you can do:
<?php $_SESSION["textareaID"] = $row["id"]; ?>
then call it whenever you want by doing:
<?php echo $_SESSION["textareaID"]; ?>
Of course, you can name the session array variable anything you want.
Or in a foreach loop:
<?php
$_SESSION["textareaIDs"] = [];
foreach($rows as $row){
array_push($_SESSION["textareaIDs"], $row["id"]);
} ?>
Script:
$('.removeVehicle').click(function() {
var $this = $(this),
$row = $(this).parent().parent();
alert($row.attr('data-vehicle-id'));
if (confirm("Delete vehicle? ") == true) {
$.post("removevehicle.php", {Id: $row.attr('data-vehicle-id')});
};
});
HTML/PHP:
<?php while ($row = $products->fetch_assoc()) { ?>
<tr data-vehicle-id="<?= $row['Vehicle_ID']?>">
<td class="VRM"><?= $row['VRM']; ?></td>
<td class="Make"><?= $row['Make']; ?></td>
<td class="Model"><?= $row['Model']; ?></td>
<td class="Colour"><?= $row['Colour']; ?></td>
<td class="Mileage"><?= $row['Mileage']; ?></td>
<td class="Advertised Price">£<?= $row['Advertised_Price']; ?></td>
<td class="Date of Registration"><?= $row['Date_of_Registration']; ?></td>
<td class="HPi Status"><?= $row['HPI_Status']; ?></td>
<td class="actions">
<button class="editLine">Edit line</button>
<button class="saveLine hide">Save line</button>
<button class="startSale" onclick="div_showSale()">Start Sale</button>
<button class="removeVehicle"><img id="trash" src="images/trash.png" alt="Delete Vehicle" height=20 width=20></button>
</td>
</tr>
<?php } ?>
removevehicle php:
<?php
require 'config/init.php';
if (isset($_SESSION['myusername'])) {
$mysqli = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$queryStr = "DELETE FROM VEHICLE WHERE Vehicle_ID = '" . $_POST['Id'] . "'";
$query = $mysqli->query($queryStr);
}
Works up to the point of the alert with the vehicle ID (correct vehicle ID is alerted). Essentially all I need to do is delete the vehicle/record from the database - any better suggestions or how to get the current method working?
Once I've got this working, I'll change the MySQLi query to counteract injection (it's not live yet).
obtain your data attribute information using .data() . Also return your PHP results and dump it to the console. Lastly, check your console for errors. Use this instead:
PHP:
$query = $mysqli->query($queryStr);
echo $query;
JS:
$('.removeVehicle').click(function() {
var $this = $(this),
$row = $(this).parent().parent();
var vehicle_id = $row.data("vehicle-id");
if (confirm("Delete vehicle? ") == true) {
$.post("removevehicle.php", {Id: vehicle_id}, function(result) {
console.log(result);
});
}
});
I think it the issue is with the sql query and wrapping the vehicle_id in quotes?
If you need to delete the row after you'll want to do it in a callback like this:
$.post("removevehicle.php", {Id: $row.attr('data-vehicle-id')}, function() {
// delete the row
});
I am attempting to create a variable from a database array when an HTML link is clicked. The goal is to redirect the user to a form populated using one piece of array data. In other words, the database will be queried and form populated according to which link is clicked (whatever the values of $row[1], $row[2], and $row[3] are).
<?php
ini_set('display_errors',1); error_reporting(E_ALL);
$DATE = date('Y-m-d');
require_once 'IRCconfig.php';
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($connection->connect_error) die($connection->connect_error);
$query = "SELECT * FROM CLIENT_CHECKIN1 WHERE DATE>='$DATE'";
$result = $connection->query($query);
if (!$result) die ("Database access failed: " . $connection->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo <<<_END
<pre>
$row[1] $row[2] $row[3]
</pre>
_END;
}
?>
If anyone can provide me with some incite as to how I could accomplish this I'd appreciate it greatly.
Please read more about sessions here
Then, to answer your question:
First you need to start the session, as simple as session_start(); on the top of your script.
Second you need to instantiate session variables with the DB values like this: $_SESSION['var'] = $value;.
Third, in the html file or whatever, where the form relies, just check for it:
if(isset($_SESSION['var'])) {
echo '<input type="text" value="'.$_SESSION['var'].'" />';
} else {
echo '<input type="text" value="" />';
}
and use the value if it is set.
L.E:
So... first thing's first... session_start(); without it, there is no point of having session.
Second, you create it like $_SESSION['some_name'] = $row[1] so that var will keep the value from $row[1]. I am presuming that it's the value you need. Do NOT do do it like $_SESSION['$row1'] because first of all this is incorrect, you will NOT have the value of row1 there. You need an unique name so that you can call it where you have the form.
The above code will become something like this:
<?php
session_start();
ini_set('display_errors',1); error_reporting(E_ALL);
$DATE = date('Y-m-d');
require_once 'IRCconfig.php';
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($connection->connect_error) die($connection->connect_error);
$query = "SELECT * FROM CLIENT_CHECKIN1 WHERE DATE>='$DATE'";
$result = $connection->query($query);
if (!$result) die ("Database access failed: " . $connection->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
$_SESSION['first_row'] = $row[1];
$_SESSION['second_row'] = $row[2];
$_SESSION['third_row'] = $row[3];
echo <<<_END
<pre>
$row[1] $row[2] $row[3]
</pre>
_END;
}
?>
and, where you have the form and the <input type = "text" value = "" /> so where you need the value, just do it like this:
<input type = "text" value = "<?php echo (isset($_SESSION['first_row']) ? $_SESSION['first_row'] : ''); ?>" />
<input type = "text" value = "<?php echo (isset($_SESSION['second_row']) ? $_SESSION['second_row'] : ''); ?>" />
<input type = "text" value = "<?php echo (isset($_SESSION['third_row']) ? $_SESSION['third_row'] : ''); ?>" />
Hope this helps! :D
I am putting together a site that handles H&S memos, and I need members to confirm that they have read their memos. I have looked at ways of handling this and they are based on assuming that members have read the memos the last time they logged on. I can't use this method as I need to assure their company that their workers have read their safety memos.
I have tried various ways of doing this but none are working well enough.
The main problem is selecting the correct ID number for the memo that they have clicked that they have read and then updating the details to the memo_read table. Any pointers, workarounds or solutions much appreciated.
This is what I have so far:
<?php
$user_id = $user_data['user_id'] ;
$company_id = $user_data['company'];
$nothing = '';
$result1 = mysql_query ("SELECT `user`, `memo_id` FROM`memo_read` WHERE `user`= '$user_id '") or die(mysql_error());
$memo_id_query = '';
while($row = mysql_fetch_array($result1)){
$memo_id_query .= " AND `id`!= '".$row['memo_id']."'";
}
?>
<div id="memo">
<h7>These are your unread memos!</h7>
<table class="bit">
<thead><tr><th>Title</th><th>Author</th><th>Time/Date</th><th>Memo</th></tr></thead>
<?php
$result = mysql_query ("SELECT `id`, `memos`, `author`, `time`, `title` FROM `memo` WHERE `worker`= 1 AND `company`='$company_id' ".$memo_id_query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$memo = $row['memos'];
$author = $row['author'];
$time = $row['time'];
$title = $row['title'];
global $id;
?>
<tbody><tr><td><?php echo $title; ?></td>
<td><?php echo $author; ?></td>
<td><?php echo $time;?></td>
<td id="mem"><a class="toggle" href="#.bit" >read/hide</a>
<div class="hiddenDiv" ><?php echo $memo; ?><br>
<form id="tickmemo" action="" method="post">
<input type="submit" name="submit" value="mark as read">
</form>
</div></td></tr><tbody>
<?php
}
if (empty($_POST['submit']) === false) {
$q=("INSERT INTO `memo_read` VALUES ('$nothing', '$user_id', '$id') ");
$result = mysql_query($q) or die(mysql_error());
}
?></table></div>
(edit)
This is working great now i'll put it up here if anyone else needs it or if you can suggest any tweeks!
<?php
$user_id = $user_data['user_id'] ;
$company_id = $user_data['company'];
$nothing = '';
$result1 = mysql_query ("SELECT `user`, `memo_id` FROM`memo_read` WHERE `user`= '$user_id '") or die(mysql_error());
$memo_id_query = '';
while($row = mysql_fetch_array($result1)){
$memo_id_query .= " AND `id`!= '".$row['memo_id']."'";
}
if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'You have marked that memo as read!<br>We will send you back in 3 seconds!';
header("Refresh: 3; url=\"http://www.testbed1.tk/login/index.php\"");
} else {
?>
<div id="memo">
<h7>These are your unread memos!</h7>
<table class="bit">
<thead><tr><th>Title</th><th>Author</th><th>Time/Date</th><th>Memo</th></tr></thead>
<?php
$result = mysql_query ("SELECT `id`, `memos`, `author`, `time`, `title` FROM `memo` WHERE `worker`= 1 AND `company`= '$company_id' ".$memo_id_query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$memo = $row['memos'];
$author = $row['author'];
$time = $row['time'];
$title = $row['title'];
?>
<tbody><tr><td><?php echo $title; ?></td>
<td><?php echo $author; ?></td>
<td><?php echo $time;?></td>
<td id="mem"><a class="toggle" href="#.bit" >read/hide</a>
<div class="hiddenDiv" ><?php echo $memo; ?><br>
<form id="tickmemo" action="#" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>" />
<input type="submit" name="submit" value="mark as read">
</form>
</div></td></tr><tbody>
<?php
}
$id = $_POST['id'];
if (empty($_POST['id']) === false) {
$q=("INSERT INTO `memo_read` VALUES (''".mysql_real_escape_string($nothing)."'','".mysql_real_escape_string($user_id)."','".mysql_real_escape_string($id)."') ");
$result = mysql_query($q) or die(mysql_error());
header('Location: worker.php?success');
}
?></table></div>
<?php } ?>
There is a wide range of aspects here to comment on:
You have no input field "id" in your form, hence someone submitting the form cannot communicate what id they want to mark as read
You are assuming that register globals is on for the variable $id to be filled with a value. Instead use $_POST['id']
Your database queries lend themselves to SQL injection attacks, as you don't check any input. Use at least intval($_POST['id']) when saving to the database
mysql_query will be turned off in PHP5.5 I think, move to PDO
There is nothing wrong with doing it all in one file, but good programming style is to put the action logic at the top, output at the bottom.
If you need to scale the "read" table to a couple of 100000 rows, use this
I think the business case is flawed as well. It will probably lead to people clicking memos as read, in the best case have them actually read, but not understood and memorized. It may be better to create a peer-to-peer quiz system to memorize the memos.
Your application flow it's not really clear. You should have:
A page where you list the unread memo to the user. Every list item should have a link that points to memo description page.
Memo description page - in this page you actually insert in memo_read table the user_id and memo_id which has been read.