使用PHP和JavaScript动态更改链接目的地
在我的网站上,我有一个用户必须能够编辑/删除等瓷砖系列。。。通常情况下,互动程序(与Facebook等其他网站的链接)只会直接链接到其标记的目的地,但我希望用户能够启用“删除模式”,允许他们通过单击互动程序来删除互动程序 这些分幅对于每个用户都是唯一的,并且通过PHP从数据库中调用。我已经在tile的代码中添加了一个脚本,该脚本应该允许根据JavaScript变量更改链接,但是我觉得PHP存在一个问题,即在最初加载链接后不允许更改链接 这是我的密码:使用PHP和JavaScript动态更改链接目的地,javascript,php,html,Javascript,Php,Html,在我的网站上,我有一个用户必须能够编辑/删除等瓷砖系列。。。通常情况下,互动程序(与Facebook等其他网站的链接)只会直接链接到其标记的目的地,但我希望用户能够启用“删除模式”,允许他们通过单击互动程序来删除互动程序 这些分幅对于每个用户都是唯一的,并且通过PHP从数据库中调用。我已经在tile的代码中添加了一个脚本,该脚本应该允许根据JavaScript变量更改链接,但是我觉得PHP存在一个问题,即在最初加载链接后不允许更改链接 这是我的密码: <script> var
<script>
var mode = 1;
function setMode(a) {
mode = a;
}
</script>
<?php
$uid = $_COOKIE["UID"];
include '../config.php';
if (empty($uid)) {header('Location: /error?error=7');};
$tile_counter = 0;
error_reporting(0);
$mysqli = new mysqli($db_servername, $db_username, $db_password, $db_name);
// check connection
if ($mysqli->connect_errno) {
die("Connect failed: ".$mysqli->connect_error);
}
$query = "SELECT * FROM tiles where uid='$uid'";
$result = $mysqli->query($query);
while($row = $result->fetch_array()){
if ($tile_trans == 1) {
$tileColour = "rgba(0, 0, 0, 0.3)";
}else{
$tileColour = $row[colour];
}
//echo '<div class="tile" style="background-color:'.$tileColour.';width:'.$row[width].'px;"><img class="tileimg" src="http://'.$row[img_url].'"></div>';
echo '
<a id="'.$row[id].'" href="">
<div class="tile hvr-reveal" id="tile" style="background-color:'.$tileColour.';width:'.$row[width].'px;">
<img class="tileimg" src="http://'.$row[img_url].'">
</div>
</a>
<script>
if (mode == 1){document.getElementById("'.$row[id].'").setAttribute("href", "http://'.$row[link_url].'")}
else if (mode == 2){document.getElementById("'.$row[id].'").setAttribute("href", "/php/tile/delete.php?id='.$row[id].'")}
</script>';
}
?>
var模式=1;
功能设置模式(a){
模式=a;
}
此代码:
if (mode == 1){document.getElementById("'.$row[id].'").setAttribute("href", "http://'.$row[link_url].'")}
else if (mode == 2){document.getElementById("'.$row[id].'").setAttribute("href", "/php/tile/delete.php?id='.$row[id].'")}
将仅在加载页面时运行一次。您应该将其包装在一个函数中,该函数可以在用户触发setMode
函数时调用
function updateLinks()
{
if (mode == 1){document.getElementById("'.$row[id].'").setAttribute("href", "http://'.$row[link_url].'")}
else if (mode == 2){document.getElementById("'.$row[id].'").setAttribute("href", "/php/tile/delete.php?id='.$row[id].'")}
}
然后
您将遇到的问题是,您不能在PHP中多次定义该函数,而。您需要在
时将其移到之外,并找到一种不同的方式来引用正确的链接。在setMode函数中以及php脚本中的script标记中添加一些调试。可能是console.log(模式);这会解决你的问题。我想这是“模式”范围的问题,你想过用AJAX进行某种验证吗?另外,我建议您为脚本创建一个单独的js文件。类似于Delete
$(“#mybutton”)。在(“单击”,function(){setMode(2);})
@SourabReddy上,这是控制台的屏幕截图。2在我单击删除模式按钮后出现。我将脚本包装在函数中,这可以工作,但仅适用于加载的最后一个磁贴。这是控制台现在的屏幕截图:每行开头的数字是每个磁贴的id。@Splycd这是因为只有函数的最后一个定义将适用,并且它们特定于每个磁贴。你想点击按钮同时设置所有瓷砖的模式吗?是的,这正是我想要的。
function updateLinks()
{
if (mode == 1){document.getElementById("'.$row[id].'").setAttribute("href", "http://'.$row[link_url].'")}
else if (mode == 2){document.getElementById("'.$row[id].'").setAttribute("href", "/php/tile/delete.php?id='.$row[id].'")}
}
function() setMode(mode) {
... whatever is already happening
updateLinks()
}