alert not showing jquery - javascript

What could be wrong with my code.
when I click the text enclosed with anchor tags. alert function is not showing up.
but when i try console.log();
it shows the id in the console.
I generated the table data with php.
please help.
thanks
<tbody>
<?php
include('config.php');
$qry = $handler->prepare( "SELECT * FROM inspection WHERE inspect_status = ?");
$qry->execute(array('Booked'));
$row = $qry->rowCount();
while($row = $qry->fetch(PDO::FETCH_ASSOC)){
$iid = $row['inspect_id'];
$service = $row['inspect_service'];
$refno = $row['inspect_refnum'];
$insdate = $row['inspect_date'];
$intrno = $row['inspect_internalrno'];
$inspect_status = $row['inspect_status'];
?>
<?php
echo"
<tr>
<td>$iid</td>
<td>$refno</td>
<td>$intrno</td>
<td>$service</td>
<td>$insdate</td>
<td>
<a idd=$iid class='marks' href='?i=$iid'>Confirm</a>
</td>
</tr>
";
?>
<?php } ?>
<script type="text/javascript">
$(function () {
$('body').delegate('.marks', 'click', function () {
var idconf = $(this).attr('idd');
alert(idconf);
return false;
});
});
</script>
</tbody>

You should rather use .on() for event delegation:
$('body').on('click','.marks',function(){
var idconf = $(this).attr('idd');
alert(idconf);
return false;
});

Try with -
$('.marks').on('click', function(e) {
alert($(this).attr('id'));
return false;
})
And change -
<a id=$iid class='marks' href='?i=$iid'>Confirm</a>
If you dont want to use id then you can try data attribute also.

Related

jQuery setInterval update PDO content

I'm wanting to run a jquery script that updates a content box every so often. I want the contents on this box to be from my database.
So far I have this, which isn't much, but not sure how the best way would be to replace the content. This is how my approach would be, but i'm sure its incorrect and there is a better way, also this does not work anyways.
<div id="list"> List here </div>
<script>
$(document).ready(function() {
setInterval(function(){ updateList(); }, 8000);
});
function updateList(){
$('#list').html(
<?php
$sql = "SELECT * FROM list WHERE enable = 1 ORDER BY id DESC";
$stm = $dbh->prepare($sql);
$stm->execute();
$u = $stm->fetchAll();
foreach ($u as $list) {
?>
"<?php echo $list['name']; ?>";
<?php
} ?>
);
}
</script>
My question is, how would I be able to do this? Thanks
You need to execute an Ajax function that call PHP server side operations like this (with <ul><li> list to show all elements separeted, but optional) :
list.php
<?php
function get_list() {
$sql = "SELECT * FROM list WHERE enable = 1 ORDER BY id DESC";
$stm = $dbh->prepare($sql);
$stm->execute();
return $stm->fetchAll();
}
if($_GET['update']) {
$html = '<ul>';
foreach(get_list() as $element) {
$html. = '<li>'.$element['name'].'</li>';
}
echo $html . '</ul>';
die();
}
?><html>
<head>
<title>...</title>
Some css and js...
</head>
<body>
<div id="list">
</div>
<script>
$(document).ready(function() {
setInterval(function(){
$.get('list.php?update=1', function(data) {
$('#list').html(data);
});
}, 8000);
});
</script>
</body>
</html>
As stated in my comment :
PHP is just a preprocessor
Therefore I recommend the use of AJAX and a separated PHP script to do the job (and an <ul>):
getEnabledsNames.php
<?php
$sql = "SELECT * FROM list WHERE enable = 1 ORDER BY id DESC";
$stm = $dbh->prepare($sql);
$stm->execute();
$u = $stm->fetchAll();
$u = array_map(function($elem){
return $elem['name'];
}, $u);
header('Content-Type: application/json');
echo json_encode($u);
?>
The JS code
let interval;
$(document).ready(()=>{
interval = setInterval(updateFromDb, 8000);
});
function updateFromDb(){
$.get(
"getEnabledsNames.php",
(data)=>{
$("#list > ul").html("");
data.forEach(e=>{
$("#list > ul").append(`<li>${e}</li>`);
});
}
).fail(/* function to handle failure here */);
}
The HTML
<div id="list">
<ul></ul>
</div>
Try This
var gtimer;
$(document).ready(function() {
clearInterval(gtimer);
gtimer = window.setInterval(function() {
clearInterval(gtimer);
updateList();
},8000);
});

Disable right click not working

<head>
<script>
$("body").on("contextmenu", "img", function(e) {
return false;
});
</script>
</head>
<?php
session_start();
include 'connection.php';
include 'sessions.php';
$image_id = $_GET['id'];
$query = "SELECT * FROM medical_data WHERE md_id = '$image_id'";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$image = $row['md_pt_image'];
}
}
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'" height="auto" width="auto"/><br>';
?>
This is my code, I want display an image from database and disable right click on it.
But the JS is not working, i still can right click on the image.
Is it because I am using < img > in echo ? or my JS is wrong!?
As #haxxxton already said that you need to wrap your code in document.ready to make it work.
<script type="text/javascript">
$(document).ready(function(){
$('img').on('contextmenu', function(e) {
return false;
});
});
</script>
Another way of doing it with pure javascript is
<script type='text/javascript'>
function nocontext(e) {
var clickedTag = (e==null) ? event.srcElement.tagName : e.target.tagName;
if (clickedTag == "IMG") {
return false;
}
}
document.oncontextmenu = nocontext;
</script>
Neel had a perfectly good answer and in fact I upvoted it.
Consider this a second implementation: You can use preventDefault();
function nocontext(e) {
var clickedTag = (e==null) ? event.srcElement.tagName : e.target.tagName;
if (clickedTag == "IMG") {
e.preventDefault();
}
}
document.oncontextmenu = nocontext;
Just Use Like:
<div id="disabled"></div>
$(document).ready(function() {
$("#disabled").on("contextmenu",function(e){
return false;
});
});
Working JS FIDDLE LINK

How can I make the delete buttons fully functional with right ID using php, AJAX or MySQL?

I am having a little problem with this, every delete button is supposed to delete the record of its own id. If we click 164 it must delete the record of 164. It works fine if I remove the ajax and ask the form to validate directly, but if I use AJAX it only deletes the record of 1st record regardless of what button I press e.g. in current scenario it will always delete the record of 159 even if I press 164 button. My code gives the following output: Remember it works fine if I ask the form to validate directly from other PHP file.
This is my output please have a look at it. Its quite simple!
if(is_numeric($lumens) && $lumens < 5000 && $lumens >250){
if(is_numeric($THD) && $THD <= 20 && $THD >=0){
if(is_numeric($scaled_power_factor) && $scaled_power_factor >=0.9){
if(is_numeric($scaled_cct) && $scaled_cct <=5700){
if(is_numeric($scaled_cri) && $scaled_cri >=65){
if(is_numeric($scaled_input_power)){
$con = new mysqli(localhost, asd, myp, rec);
if(!$con){
echo "Couldn't connect to the database";
}
else{
$id = $_SESSION['user_id'];
$query = "INSERT INTO scaling_performance_data SET
MODEL_NUMBER = '$model_number',
LUMENS = '$lumens',
scaled_luminaire_efficacy = '$lm_w',
scaled_input_power = '$scaled_input_power',
THD = '$THD',
SCALED_POWER_FACTOR = '$scaled_power_factor',
SCALED_CCT = '$scaled_cct',
SCALED_CRI = '$scaled_cri',
HOUSING_VARIATION = '$housing_variation',
user_id = '$id'
";
if($con->query($query)){
$sql = "SELECT * FROM scaling_performance_data WHERE user_id='$id';";
$result = $con->query($sql);
if($result){
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<form>
<table>
<tr>
<th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th><input type="button" name ="delete_id" id="delete_id" value="<?php echo $row['ID'];?>" onclick="vlid();"/></th>
</tr>
</table>
<script type="text/javascript">
function vlid(){
var delete_id = $('#delete_id').val();
alert(delete_id);
$.post('validator.php',{
postdelete_id : delete_id
},
function(data){
$('#del').html(data);
}
)
}
</script>
</form>
<?php
}
}
validator.php is:
$id = $_POST['postdelete_id'];
$con = new mysqli(localhost, asd, myp, rec);
if(!$con){
echo "Couldn't connect to the database";
}
else{
$query="DELETE FROM scaling_performance_data WHERE ID='$id';";
if($con->query($query)){
echo "Your Result was deleted successful";
echo $id;
}else{
echo "There was a problem Please try again later";
}
}
The problem is that in your vlid() function, JQuery is only selecting the first element with id = delete_id. I would try passing the ID to the vlid() function like this:
<input type="button" ... onclick="vlid(<?php echo $row['ID'];?>)"/>
And then modify your vlid() function to accept the ID parameter.
Try var delete_id = $(event.target).val(); instead of: var delete_id = $('#delete_id').val();
1st ID must be unique so use
class="delete_id"
instead of
id="delete_id"
2nd remove onclick="vlid();" and use
$(document).ready(function(){
$('body').on('click','.delete_id',function(){
var getValue = parseInt($(this).val());
$.post('validator.php',{postdelete_id : getValue},function(data){
$('#del').html(data);
});
});
});
and to remove the tr which deleted use
$(document).ready(function(){
$('body').on('click','.delete_id',function(){
var thisBtn = $(this);
var getValue = parseInt(thisBtn .val());
$.post('validator.php',{postdelete_id : getValue},function(data){
$('#del').html(data);
thisBtn.closest('tr').remove();
});
});
});

how to pass value of td(which has been created by while loop) and pass it to the textbox?

While loop creates rows, but below function only pick value of first row td and put in textbox, i want it pick the value of td which has been clicked. not first one. Please help
while($row = $result->fetch_assoc()){
<tr>
<td id='tbltd' onclick='getstock_id();'>",$row["stock_id"],"</td></tr>
}
<script>
function getstock_id() {
var myBox1 = document.getElementById("ab").innerHTML;
var result = document.getElementById('stk');
result.value = myBox1;
}
</script>
You should not have multiple id with same value in a document. In your case, first element is being selected in document.getElementById("ab").innerHTML
Try this:
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td id='tbltd' onclick='getstock_id("<?php echo $row["stock_id"]; ?>");'><?php echo $row["stock_id"]; ?></td></tr>
<?php
}
?>
<script>
function getstock_id(val) {
var result = document.getElementById('stk');
result.value = val;
}
</script>
Try to passed in your function param the node reference like that:
while($row = $result->fetch_assoc()){
<tr>
<td id='tbltd' onclick='getstock_id(this);'>",$row["stock_id"],"</td></tr>
}
<script>
function getstock_id(refNode) {
document.getElementById('stk').value = refNode.innerHTML;
}
</script>

Display a page after an ajax request

I need a web page where users can modify a data already captured in database. So I made a tab with the list of every data and when a user click on one, I want to redirect the user in a page with matching data.
So this is my tab :
<?php
$conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'";
$dbconn = pg_connect($conn_string);
$sql = "SELECT id_essai, nom_local_essai, thematique FROM public.essai";
$res = pg_query($sql) or die("Pb avec la requete: $sql");
echo'<table class="table table-hover" border="1">';
echo'<thead><tr><th>id_essai</th><th>Nom local</th><th>Thématique</th></tr></thead>';
while ($data = pg_fetch_array($res))
{
echo' <tbody>
<tr>
<td><a id="'.$data['id_essai'].'">
'.$data['id_essai'].'
</a></td>
<td>
'.$data['nom_local_essai'].'
</td>
<td>
'.$data['thematique'].'
</td>
</tr>
</tbody>
';
}
echo'</table>';
?>
And I get the ID of the data selected like this :
<script>
$(document).ready(function(){
$('a').click(function(){
var id_essai = $(this).attr('id');
$.post('traitement_modif_essai.php',{id_essai:id_essai});
});
});
</script>
Now, on the other file, I want to get the ID and display the right page, but I can't get thought.
<?php
session_start();
$_SESSION['id_essai'] = $_POST['id_essai'];
echo $_SESSION['id_essai'];
header('Location:essai_modif.php');
?>
You can redirect the user after the session has been set by you in the PHP in the javascript callback of post
<script>
$(document).ready(function(){
$('a').click(function(){
var id_essai = $(this).attr('id');
$.post('traitement_modif_essai.php',{id_essai:id_essai}, function(){
window.location = "./essai_modif.php";
});
});
});
</script>
and in the PHP
<?php
session_start();
$_SESSION['id_essai'] = $_POST['id_essai'];
echo $_SESSION['id_essai'];
?>

Categories

Resources