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;
});
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);
...
}
I want make link # with id to div id in one page
but id not working in:
<div id="single-news" class="reveal-modal single-news small" data-reveal>
<?php
$member = mysql_query("SELECT*FROM team WHERE id = '$_GET[id]'");
$m = mysql_fetch_array($member);
?>
<div class="twelve columns first-column">
<p class="single-news-content"><?php echo $m[quote]; ?></p>
</div>
id is key of global array GET, so you need to put single or double quote around it.
Try this code :
<?php
$member = mysql_query("SELECT*FROM team WHERE id = '".$_GET['id']."'");
$m = mysql_fetch_array($member);
?>
<div class="twelve columns first-column">
<p class="single-news-content"><?php echo $m[quote]; ?></p>
</div>
$id = $_GET['id'];
$member = mysql_query("SELECT*FROM team WHERE id = '$id'");
$m = mysql_fetch_array($member);
You are passing variables through URLs.
And PHP can access those variables in the $_GET array.
In your case, you are adding variables after hash #.
PHP ignores every $_GET variable after #.
Therefore, you are not getting $_GET['id']
Corrected HTML Code:
And corrected PHP code:
$member = mysql_query("SELECT*FROM team WHERE id = '" . $_GET['id'] . "');
Also, please be careful while accessing array variables.
You are using $_GET[id],
It is best practise to use $_GET['id'] as in case, PHP does not get any array element with key id, it will consider id as a constant which is obviously not defined.
This may show/log an error.
try this
test
<div id="single-news" class="reveal-modal single-news small" data-reveal>
<?php
$id=$_GET[id];
$member = mysql_query("SELECT*FROM team WHERE id = ".$id."");
$m = mysql_fetch_array($member);
?>
<div class="twelve columns first-column">
<p class="single-news-content"><?php echo $m[quote]; ?></p>
</div>
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]; ?>".
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.
I have some images that I create using data from a MySQL database. The data in the database are a title and a description. This is not visible on the website until a user clicks the image. Javascript then loads it in some other place, not anywhere near my image.
My problem is the accessing of this data. I create my elements in PHP with this structure:
<div class="image-container">
<img src="<?php echo $url; ?>" alt="<?php echo $title; ?>" />
<div class="title" style="display: none;"><?php echo $title; ?></div>
<div class="description" style="display: none;"><?php echo $description; ?></div>
</div>
When the image is clicked, I call for the data like this:
$('.image-container').click(handle_image_click);
function handle_image_click(e) {
var title = jQuery(e.currentTarget).find('.title').html();
var description = jQuery(e.currentTarget).find('.description').html();
(...)
}
Allthough this works, I don't find this a very 'clean' solution. It requires me having to hide div elements and 'store' data in them. I know you can store data to elements in jQuery using the following code:
$('#myElement').data('someName', 'someValue');
Now my question is: is there any way to not have to create the div elements in php (as seen in code block 1), but directly add the description and title to the 'data'-property of the element, ready to be fetched by javascript/jQuery?
you can use the data-* html5 attribute to store data
<div class="image-container" data-title="<?php echo $title; ?>" data-description="<?php echo $description; ?>">
<img src="<?php echo $url; ?>" alt="<?php echo $title; ?>" />
</div>
then in the handler
$('.image-container').click(handle_image_click);
function handle_image_click(e) {
var title = jQuery(this).data('title');
var description = jQuery(this).data('description');
(...)
}
yes you can.. using html5 data attribute
<img src="<?php echo $url; ?>" data-title="<?php echo $title; ?>" data-description="<?php echo $description; ?>" alt="<?php echo $title; ?>" />
and fetch it with jquery using data()
...
function handle_image_click(e) {
var title = jQuery(e.currentTarget).find('img').data('title'); //gives title
var description= jQuery(e.currentTarget).find('img').data('description'); //gives description
....