I want to make the copy button copy the value of user, but the problem is the button copied only the value of first user. When move to second user, it remain copied the valued of the first user. Here are my code.
<?php
require_once('connection.php');
$sql = "SELECT ftname, menu, longlan FROM
ownerinfo";
$result = $con->query($sql);
if ($result) {
// output data of each row
while($row = $result->fetch_assoc()) {
$a=1;
$long[$a]=$row["longlan"];
echo "Food Truck: " . $row["ftname"]. "
<br>Menu: " . $row["menu"]. "
<br>Longitude Latitude: <input id='he '
value='".$long[$a]."'></input><button
onclick='myFunction()'>Copy</button><br>
<a
href='https://maps.google.com/'>Maps</a>
<hr>";
$a=$a+1;
}
} else {
echo "0 results";
}
?>
<script>
function myFunction() {
var copyText = document.getElementById("he");
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
Considering your loop outputs an ID, your JavaScript will only ever be able to target the first instance of that ID (as multiple IDs are considered invalid markup).
To correct this, output classes from your PHP instead:
<input class='he' value='".$long[$a]."'>
And loop over each of the classes in your JavaScript:
const copyText = document.getElementsByClassName("he");
for (let i = 0; i < copyText.length; i++) {
copyText[i].select();
document.execCommand("copy");
alert("Copied the text: " + copyText[i].value);
}
Related
I have a new question about a project I had been working on. I was designing a grid with different colored cells. it has a hidden div which shows when a cell is clicked, however I realized that only one cell(the last one of it's type) will show. i.e. if I have 2 objects with the column "objaffinity" as 0 ("enemy") it will show both red cells on the grid, however only the last one will actually work.
how can I make it so that it will show the proper information for each cell?
here's my code:
mapgen.php:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="cellinfo.js"></script>
<script src="cmenu.js"></script>
<?php
require("sql.php");
$sql = <<<SQL
SELECT *
FROM `maps`
WHERE `objpresent` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
} // ran the query
//$xobj = array();
//$yobj = array();
$otype = array();
$oname = array();
$xyobj = array();
while($row = $result->fetch_assoc()){
$xyobj[$row['x']][$row['y']] = true;
$otype[$row['id']]=$row['objaffinity'];
$oname[$row['id']]=$row['object'];
}
// get the rows
$cellid=1;
//find whether the row is obstructed
for ($y = 0; $y < 20; $y++) {
echo '<tr>';
for ($x = 0; $x < 25; $x++) {
echo "<td>";
//Detect what type of object it is
if (isset($xyobj[$x][$y])) {
if($otype[$cellid] == 2)
{
echo "<a href='#'> <div class='foe'> </div><div class='foepopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
elseif($otype[$cellid] == 1)
{
echo "<a href='#'><div class='friend'></div><div class='friendpopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
else
{
echo "<a href='#'> <div class='neutral'></div><div class='neutralpopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
$cellid++;
}
echo '</td>';
}
echo '</tr>';
}
?>
Cellinfo.js:
$(document).ready(function(){
//initially hide all popups
$(".foepopup").hide();
$(".neutralpopup").hide();
$(".friendpopup").hide();
//foebutton selected
$(".foe").on("click",function(e){
$(".friendpopup").hide();
$(".neutralpopup").hide();
$(".foepopup").show();
});
//close foe when selected
$(".foepopup").on("click",function(e){
$(".foepopup").hide();
});
//neutral button pressed
$(".neutral").on("click",function(e){
$(".foepopup").hide();
$(".friendpopup").hide();
$(".neutralpopup").show();
});
//close neutral
$(".neutralpopup").on("click",function(e){
$(".neutralpopup").hide();
});
//friend button pressed
$(".friend").on("click",function(e){
$(".foepopup").hide();
$(".neutralpopup").hide();
$(".friendpopup").show();
});
//close friend
$(".friendpopup").on("click",function(e){
$(".friendpopup").hide();
});
});
In your functions you use selectors, so for the script it does not matter which div was clicked.
Let me show you some examples:
$(".foepopup").on("click",function(e){
$(".foepopup").hide();
});
It should be something like this rather:
$(".foepopup").on("click",function(e){
$(this).hide();
});
And another example:
$(".neutral").on("click",function(e){
$(".foepopup").hide();
$(".friendpopup").hide();
$(".neutralpopup").show();
});
Rewrite it like this:
$(".neutral").on("click",function(e){
var td_tag = $(this).parent().parent();
td_tag.children(".foepopup").hide();
td_tag.children(".friendpopup").hide();
td_tag.children(".neutralpopup").show();
});
Rewrite other code on your own. this is the element on which click was triggered. td_tag will contain parent cell of a div clicked. After that, children method will allow you to find needed elements already inside specific cell.
Good luck!
I have a table with many auto generated forms (there could literally be hundreds of forms). The code for that is php based and looks like this:
$cellPosition = 0;
$rowCounter = 1;
$infoCounter = 1;
for ($x=0;$x <= count($assetName);$x++)
{
for ($i=0;$i < count($currentJobs);$i++)
{
$rowCounter= 1;
if ($currentJobs[$i][0] == $table->getCellContents(0,$x))
{
for ($y =0; $y < $currentJobs[$i][10];$y++)
{
$rowCounter++;
}
$table->setCellAttributes ($rowCounter,$cellPosition,"id='jobCell' bgcolor = ". $currentJobs[$i][4]. " rowspan=" . $currentJobs[$i][9]);
$table->setCellContents($rowCounter++,$cellPosition,
"<form id='scheduleForm".$infoCounter++."' method='POST' action='../forms/updateJobForm.php'>".
"<input type='hidden' name='jobInfo' value='" . $currentJobs[$i][1] . "'/>" . " " . "Job# (".$currentJobs[$i][2] . ")<br>" . $currentJobs[$i][3] .
"</form>");
}
else
{
$rowCounter = 1;
}
}
$cellPosition++;
}
echo $table->display();
I have the jobCell (a td element) bound to the following javascript/jquery code:
<script>
$(document).ready(function()
{
$("#jobCell").click(function()
{
$(this).children('form').submit();
//$('#scheduleForm').submit();
});
});
</script>
I each jobcell is click-able and I previously had it to where clicking anyone would submit the form. The problem is that it would only send the information for the hidden information for the last jobcell in the table. Now with my current code, it only allows the user to click the first cell and it does submit. How accomplish submitting the hidden data in the jobcell that is clicked when I have many forms?
There should only ever be one of each ID on the page. You have you have lots of forms all with the same ID it may well only submit the last. Change id to class, there can be any elements on the same page with the same class.
At a guess something like this. But without seeing your actual HTML output this could be wrong.
<script>
$(document).ready(function()
{
$("#jobCell").click(function()
{
$(this).children('form').submit();
//$('.scheduleForm').submit();
});
});
</script>
Check that id='jobCell' is only set once.
I would expect the code to look more like the form id code:
id='scheduleForm".$infoCounter++."'
I'm making a travelguide. I'm displaying data(activity) from a database(mysql). Each activity has his own button. When you push this button you add that specific activity to your travelguide. This all works, but i must refresh the page to display the activities that are added to the guide. Now i have made a working javascript script, but it's not dynamic.
function MakeRequest()
{
var test = 1;
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("GET", "get_test.php?q="+test, true);
xmlHttp.send(null);
}
You see de variable: "var test = 1;"
The number 1 stands for the ActivityID 1. So now if you push the button it shows the activity with the ActivityID = 1. If i change the number to 2, is shows the activity with the ActivityID = 2.
I want to change the variable: "var test = 1;" The number must automaticly be inserted, it must be the same number as the ActivityID of the activity where is push the button.
<?php
$sql = "SELECT * FROM activity";
$stm = $db->prepare($sql);
$result = $stm->execute(array());
while($row = $stm->fetch(PDO::FETCH_ASSOC))
{
echo '<div id="activity'.$row['ActivityID'].'">';
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['ActivityIMG'] ) . '" >', '<br>';
//echo $row['Activityname'], '<br>';
//echo $row['Activitydescription'];
echo '<input type="button" class="addActivity" onclick="MakeRequest();" value="Activiteit toevoegen" data-activity="' . $row['ActivityID'] . '">';
echo '</div>';
}
?>
This is the php file:"get_activity". This is the code that displays the data en display the button. This button gets automaticly the ActivityID from the specific activity. This way i only need one script for all the activities. I want to do the same with the javascript, but i don't know how?
If I understand you want to give var test the correct id depending on the button you press.
Why don't you just pass a parameter to your js function?
function MakeRequest(test){
...............
}
$test = $row['id'];//your id field
<input type="button" class="addActivity" onclick="MakeRequest(<?=$test?>);" value="Activiteit toevoegen" data-activity="' . $row['ActivityID'] . '">
I am using jQuery sortable to manipulate image order and write to a DB. That functionality works well.
PHP
echo "<div class='revisionNum'>";
echo "<ul id='sortable_" . $count ."'>";
while($row = mysql_fetch_array($result)) {
$sortImageName = $row['OrgImageName'];
$sortPath = "../data/gallery/" . $galleryID . "/images/album/" . $sortImageName;
echo "<li class='sortPhotos' id='item_{$row['id']}' >";
echo '<img class="sortImage" src="'. $sortPath .'"/>';
echo "<p>" . $sortImageName . "</p>";
echo "</li>";
}
echo "</ul>";
echo "</div>";
jQuery
//make sortable
$(".revisionNum").each(
function(e) {
num = e + 1;
$("#sortable_" + num).sortable(
{stop:function(i) {
serial = $("#sortable_" + num).sortable("serialize");
$.ajax({
type: "GET",
url: "../albumUploader/queries/sort.php",
data: serial
});
},
opacity:1.0,
//cursor: move
});
});
MYSQL
foreach($_GET['item'] as $key=>$value) {
mysql_query(" UPDATE galleryimage
SET sort = '{$key}'
WHERE id = '{$value}'
");
}
The issue is when I have multiple <div class=''revisionNum> i am only grabbing the serial = $("#sortable_" + num) of the last UL if the [.revisionNum], not the actual UL that I am sorting. Thanks for the help on this. Let me know if further clarification is needed.
I am not sure I fully understand your question, but I think you are looking for the following:
The variable num will change every loop you make in the each-loop. But at the end it will have the value of the last loop. Because num seems to be a global variable you can't call it in the stop function. Then it will just use the last value it had. The value of the last loop. (Explains your problem)
To solve this I recommend to change your code to:
$(".revisionNum").each(
function(e) {
$(this).children("ul").sortable(
{stop:function(i) {
num = $(this).children("ul").attr("id").replace("sortable_", "");
serial = $(this).children("ul").sortable("serialize");
...
$(this) refers to the $(".revisionNum") you are looping through and it will be remembered, also in the stop function.
Basically by clicking the "comment" link the last result of the query should show and by clicking again it should be hidden. I have tried Rocket's code as well but I get an error message in the bottom of the browser and when I click "comments" it just takes me to the top of the page. I would apprieciate some advice on this
$i = 1; // ID Counter
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'>$row[comment]</div>";
$i++; // Increment counter
}
This is a loop, echoing the same thing over and over, thus making all the divs have the same ID, something2.
IDs need to be unique, you gonna have to make unique IDs for each div.
Something like: <div id='something$i' style='display: none;'> (remembering to increment $i).
Also, you're gonna to escape the quotes in your onclick attribute.
<a href='#' onclick=\"toggle_visibility('something$i');\">
The code should look something like this:
$i = 1; // ID Counter
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'>$row[comment]</div>";
$i++; // Increment counter
}
Escape the quotes :
$blah = "onclick='toggle_visibility(\"something2\");'>Comments</a>"
There is an easier way to hiding / showing the next sibling ....
try this
<div style="display:none">some hidden content</div>
function toggle(el,ev) {
ev.preventDefault(); // prevent the link from being followed
el = next(el); // get the next element
if (el.style.display == "none") { // toggle the display
el.style.display = "block";
} else {
el.style.display = "none";
}
}
/*
Credit to John Resig for this function
taken from Pro JavaScript techniques
*/
function next(elem) {
do {
elem = elem.nextSibling;
} while (elem && elem.nodeType != 1);
return elem;
}
Working example
You can throw in a counter into your code as the while loop is executing to dynamically generate unique id's for each comment div. Or, you can pull a unique field out of the query result for the id's, as long as you hook up to it appropriately later if it ends up being used and remain consistent in the rest of the code.
either
$count = count($result);
...
while (...){
$count--;
echo '... id="something'. $count .'" ...'
}
or...
while (...){
echo '... id="something'. $row['ID'] .'" ...'
}