如何使用HTML链接传递几个PHP变量而不刷新页面


How to pass several PHP variables using a HTML link and do not refresh the page?

我有一些代码:
$page = $_GET['page'];

然后我想在index.php中调用另一个页面?页面=

if($page == "view_data") 
{
  include "view_data.php";
}
else if($page == "edit_data")
{
  include "edit_data.php?id=";
}
else if($page == "delete_data")
{
  include "delete_data.php?id=";
}
else
{
  echo "Cannot found the page";
}

然后我创建一个链接来生成上面的if函数。

<a href="index.php?page=edit_data&id=<?= $row['id_data'] ?>">Edit</a>
<a href="index.php?page=delete_data&id=<?= $row['id_data'] ?>">Delete</a>

我的问题是如何传递编辑数据或删除数据,如果我必须在我的链接中使用2个变量,以便编辑和删除数据只能执行一个页面?

我建议在您的客户端使用ajax:

function doAction(page,id)
{
  var xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        var response=xmlhttp.responseText;
        alert(response);//if you want to notify the user that the job was done successfully
      }
    }
    xmlhttp.open("GET","index.php?page="+page+"&id="+id,true);
    xmlhttp.send();
  }
}
你的HTML将使用onClick属性
<button onclick="<?php echo "doAction('"edit_data'",".$row['id_data'].")"; ?>">Edit</button>
<button onclick="<?php echo "doAction('"delete_data'",".$row['id_data'].")"; ?>">Data</button>

其余的PHP代码可以保持不变