I have a project in which I am displaying a button tag in a while loop. On every button click I want to display an alert box with the respective UserId. Here is my code:
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo1" value="<?php echo "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div>
<?php } ?>
Here is my validate function:
function validate1(id2)
{
// var id2;
id2 = document.getElementById('demo2').getAttribute('value');
alert(id2);
}
But it is always showing me last user id .. whereas i want to display userid for every user on everyclick.
Can someone help?
Here man, the function you were calling was undefined validate1, also you don't need to get any arguments on your function declaration, since you are not passing any arguments when you invoke it.
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo" value="<?php echo "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div>
JS
function validate(){
var id2 = document.getElementById('demo').getAttribute('value');
alert(id2);
}
try this in your code
HTML:
<button onClick="validate('<?php echo $RegisterId; ?>')">Show Interest</button>
Javascript:
function validate(id2)
{
alert(id2);
}
Your code needs some modifications.
Firstly, you have made a provision to send id to javascript function, but, you are not passing id to it.
PHP
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo1" value="<?php echo $dis1['RegisterId'];?>">
<button onClick="validate('<?php echo $dis1['RegisterId'];?>')">Show Interest</button>
</div>
<?php } ?>
Javascript:
function validate1(id2) {
// var id2;
//id2 = document.getElementById('demo2').getAttribute('value');
alert(id2);
}
With this code, your clicks shouldn't even return the last id. Your javascript function is not looking good.
This should work;
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<!-- removed id attribute, as they're unique, you can't set it to every div.
If you really need id attribute, you can set one by using your RegisterId (e.g. id="demo-<?php echo $RegisterId; ?>)
And moved value attribute to button tag, it's much more useful in it. -->
<div>
<button onClick="validate(this)" value="<?php echo "$RegisterId" ?>">Show Interest</button>
</div>
<?php } ?>
Javascript
function validate(element){
alert(element.value)
}
This way, you can use it in other stuff much easier.
Related
I have a code in PHP that prints HTML elements based on the information we got in the database, so if we have for example 8 products the code will print 8 divs with the following elements inside
h1 the id of this element will be the same as the product in the database
input for client name
buy button that send the ID of the H1 to the database
here is some code i made
<?php
while($row=$consulta->fetch())
{
?> <img src="<?php echo $row["Imagen"];?>"style="width:12rem;height:12rem;" alt="Scotter" >
<br>
<div>
<h4 id="<?php echo $row["ID_Scooter"]; ?>"> <?php echo $row["ID_Scooter"]; ?> </h4>
<br>
<?php
echo $row["Nombre"];
?>
<br>
<?php
echo $row["Descripcion"];
?>
<br>
<?php
?>
<button onclick="abrir()">Alquilar</button>
<br>
</div>
<?php
}
?>
JavaScript
function abrir(){
var dialog = document.getElementById('favDialog');
dialog.show();
var id=document.getElementById("heres is where im supposed to get the id of the id i clicked");
alert("div id is="+id);
}
php assing ID for example (3,4,5,6,7) but when i click the button inside the div with id=5 it prints the id=3 no matter which button i click
please note that you've set onclick="abrir()" without passing any specific data!
you have got 2 way to do it:
1- passing data with php:
HTML:
abrir(<?php echo $row["ID_Scooter"]; ?>)
and use it in your js :
function abir(id){
let id_scooter = id;
...
}
2- passing clicked element with js:
HTML:
onclick="abrir(this)"
JS:
function abir(el){
let id_scooter = (el).attr(id);
...
}
The copy to clipboard feature with a button works if it is only has exact variable however if it is a echo'd variable which is different on each row then it only copys the first row no matter which button you click. I have tried many different methods and they all seem to do the same. Is there something which I can add in the code which would make the correct row button copy the correct row $dir ?
function copy(element_id) {
var aux = document.createElement("div");
aux.setAttribute("contentEditable", true);
aux.innerHTML = document.getElementById(element_id).innerHTML;
aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)");
document.body.appendChild(aux);
aux.focus();
document.execCommand("copy");
document.body.removeChild(aux);
}
<p id="demo">
<?php echo $dir ?>
</p>
<button onclick="copy('demo')">Copy Keeping Format</button> <br><br>
Code:
$numresults=$info["count"];
echo"<div style='position: fixed; float: right; padding-left: 450px;'><a class=clear href=javascript:history.go(-1)>Search again</a></div>";
echo "<div><p>There are <b>$numresults</b> results for your search '<i><b>$SearchFor</i></b>'";
if ($numresults>0){
echo " these are:</p></div>";
echo "<div>";
for ($x=0; $x<$numresults; $x++) { //display the results
$sam=$info[$x]['samaccountname'][0];
$disp=$info[$x]['displayname'][0];
$dir=$info[$x]['homedirectory'][0];
$fil=$info[$x]['homedirectory'] [0];
$displayout=substr($sam, 0, 4);
echo "User Name : $sam";
echo "<br>Name : $disp";
echo "<br>Home Drive : <a class=clear href=$dir>$dir</a><br>";?>
<p id="demo<?php echo $i; ?>">
<?php echo $dir ?>
</p>
<button onclick="copy('demo<?php echo $i; ?>')">Copy Keeping Format</button> <br><br>
<script>
function copyTo(input){
input.select();
document.execCommand("copy");
}
</script>
If <p id="demo"><?php echo $dir ?></p> can repeat many times in the code (due to being contained within a loop) then obviously you can't identify it using a single id, because - by definition - an ID must be unique, and that's not the case here.
You'd need to generate a different ID for each <p> element.
In the simplest case you can just use PHP to add a counter to the ID.
First, outside your loop, initialise a counter:
$i = 0;
Then, within the loop, use that to make a unique element ID:
<p id="demo<?php echo $i; ?>">
<?php echo $dir ?>
</p>
<button onclick="copy('demo<?php echo $i; ?>')">Copy Keeping Format</button> <br><br>
Finally, before your loop ends, increment the counter:
$i++;
I've been able to get my arrays working to echo text from a database in HTML. Fine. However, I now want to send this text to another page onClick (JQuery or JavaScript). So here is what I already have (I know it's not sending anything, but this is not the problem right now) :
PHP
$h = "SELECT * FROM table";
$h=$pdo->prepare($h);
$h->execute();
$i=1;
while ($row = $h->fetch()) {
$text[$i] = $row['content'];
$id[$i] = $row['id'];
$i++;
}
HTML
<h1 id="<?php echo $id[0]; ?>" onClick="edit(this)"><?php echo $text[0]; ?>
</h1>
<h2 id="<?php echo $id[1]; ?>" onClick="edit(this)"><?php echo $text[1]; ?></h2>
<p id="<?php echo $id[2]; ?>" onClick="edit(this)"><?php echo $text[2]; ?></p>
JavaScript
function edit(element) {
alert(element.id);
}
So, this gives me the "id" of the content in the database. However, I'd like to know if there is a way to get this information without having the "id" in the HTML tags, a way to click on the text and have the number that is inside the square brackets.
Example : you click on the text inside the "h1" tag and a pop up tells you what is the number in <?php echo $text[2]; ?>. In this case, I would like the number 2 to show up in the alert box, without using id="<?php echo $id[2]; ?>".
How can i get a variable in PHP with the value of a ID on a div.
Example: <div class="style" id="13" name="var">text...</div>
how can i on a PHP or JS, etc..., get the id value for a variable on PHP, to i show.
Example: $id_div = id from div with the name var.
<?php echo $id_div; ?> // 13.
Please help me, i just want to know the value of id propriety and replace on a php variable.
<div name="var" id="<?php echo $get["id"]; ?>">
Here i want to show the id propriety on a php variable
</div>
First the $get["id"]; should be $_GET["id"];, and you could store it in some variable and use it where you want :
<?php $id = $_GET["id"]; ?>
<div name="var" id="<?php echo $id; ?>">
<?php echo $id; ?>
</div>
Hope this helps.
You can do this:
<div name="var" id="<?php echo $get["id"]; ?>">
<?php echo $get["id"]; ?>
</div>
or with javascript:
var divs = document.querySelectorAll('div[name="var"]');
[].forEach.call(divs, function(div){
div.textContent = div.id;
});
I have products in table I wish to display some when page load and remain data should display on button click, I have set limit as 6 per page, when page loading it is displaying first six rows and if I click on Load More button it is displaying another six rows here problem coming after got 12 rows from table when I click again on Load More button it is not working even I have more data in table, I have cross checked parameters also, it is fine parameters also sending with request. please help me how to solve this.
NOTE: when I test this script in core php it is working fine, when I implement this script in codeigniter first time click load more data working second time when i click it is not wokring.
Javascript Code Block
$('.more').click(function(){
var ID = $(this).attr("id");
var cats = $('input[name=cats]').val();
$("#more"+ID).html('<img src="<?=ROOT;?>resources/moreajax.gif" />');
$.post('<?=ROOT;?>product/dataLoad', {id:ID,cats:cats}, function(data){
if(data)
{
$('ol#updates').append(data);
$("#more"+ID).remove();
}
else
{
$(".morebox").html('The End');// no results
}
});
return false;
});
codeigniter controller function
public function dataLoad() {
if(isset($_POST['id']))
{
$lastmsg=$_POST['id'];
$cats = $_POST['cats'];
$query=mysql_query("select * from product where id > '".$lastmsg."' AND category_id IN ($cats) order by id asc limit 4");
$numrows = mysql_num_rows($query);
while($ob = mysql_fetch_array($query))
{
?>
<div>
<p>ID: <?=$ob['id']; </p>
<p>NAME: <?=$ob['name']; </p>
</div>
<?php
}//while
?>
<?php
if($numrows > 0)
{
?>
<div id="more<?php echo $ob['id']; ?>" class="morebox" style="clear:both">
<input type="hidden" name="cats" value="<?=$cats;?>" />
Load More
</div>
<?php
}
}
?>
product page code
<ol class="timeline" id="updates">
$str = implode(",",$cats);
$query = "select * FROM product WHERE category_id IN (".$str.") order by id asc limit 4";
$result=mysql_query($query);
$numrows = mysql_num_rows($result);
while($ob = mysql_fetch_array($result))
{
?>
<div>
<p>ID: <?=$ob['id']; </p>
<p>NAME: <?=$ob['name']; </p>
</div>
<?php
}//while
?>
</ol>
<?php
if($numrows > 0)
{
?>
<div id="more<?php echo $ob['id']; ?>" class="morebox" style="clear:both">
<input type="hidden" name="cats" value="<?=$cats;?>" />
Load More
</div>
<?php
}
?>
I solved this problem insted of this
$('.more').click(function(){
$("#updates").on("click",".more",function(){