【问题标题】:Remove document and update database删除文档并更新数据库
【发布时间】:2012-07-12 11:17:49
【问题描述】:

我有 2 个文件可以帮助我删除文档和更新数据库。 我的第一个文件包含一个带有删除按钮的表单和一个名为 remove() 的 javascript 函数。第二个 php 页面将删除它不返回任何结果的文件。

Remove.php 的代码(调用 remove() 函数时):

$Doc=$_GET['Doc']; //Value get from remove() function
$ID= intval($_POST['ID']);
$FirstReport= $_POST['FirstReport'];
$SecReport = $_POST['SecReport'];

$FirstReportPath= $_POST['FirstReportPath'];
$SecReportPath = $_POST['SecReportPath '];

$DB = new PDO('sqlite:/database/Student.db');
//If i click remove firstreport button, i'll check it to see if it's the same
if(($Doc) == ($FirstReport))
{
   $result= $DB->query("Update Student set FirstReport='No' WHERE ID=".$ID);    
//This unlink should remove the document file from the path.
    unlink($FirstReportPath);
echo "success";
 }
 else
 {
    //same code repeated as if statement
 }

Javascript 函数

 function RemoveDoc(Doc)
 {
 xmlhttp=new XMLHttpRequest();
 xmlhttp.open("GET","functions/Resubmit.php?Doc="+Doc,true);
 xmlhttp.send();
 document.getElementById("RemoveMsg").innerHTML=xmlhttp.responseText;
 return false;
 }

我确实尝试过警告(Doc)并且文档名称确实显示但在第二个 remove.php 上,它没有运行任何编码。尝试“GET”/“POST”也得到相同的结果。好心提醒。

【问题讨论】:

  • 请注意,您的 PHP 代码原样将允许他人删除您服务器上的任意文件。不要忘记验证您的输入。
  • 我可以将文件移动到该文件夹​​中。但我现在无法取消链接。
  • 您是否输出了 firstreport 和 firstreportpath 并确保它们的大小写相同、长度相同、字符相同(包括空格)?使用 var_dump 并检查

标签: php javascript sqlite


【解决方案1】:

看起来你发送了一个 post 请求,但是在 url 中发送了你的文档名称,一个 $GET 变量。

要么切换到获取请求:

function RemoveDoc(Doc)
 {
 xmlhttp=new XMLHttpRequest();
 xmlhttp.open("GET","functions/Resubmit.php?Doc="+Doc,true);
 xmlhttp.send();
 document.getElementById("RemoveMsg").innerHTML=xmlhttp.responseText;
 return false;
 }

或将文档名称作为 post 参数发送:

function RemoveDoc(Doc)
 {
 xmlhttp=new XMLHttpRequest();
 xmlhttp.open("POST","functions/Resubmit.php",true);
 xmlhttp.send("Doc="+Doc);
 document.getElementById("RemoveMsg").innerHTML=xmlhttp.responseText;
 return false;
 }

另外,您无需等待服务器的响应。

function RemoveDoc(Doc)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","functions/Resubmit.php",true);
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("RemoveMsg").innerHTML=xmlhttp.responseText;
    }
  }
 xmlhttp.send('Doc='+Doc);
return false;
}

【讨论】:

  • 您需要等待服务器响应才能尝试使用它
  • 嗨,我确实尝试了所有方法来调用我的 javascript 函数。我确实尝试过提醒(Doc)。它确实指出了我需要的正确文档。但是对于Resubmit.php,它并没有调出...
  • 另外,您如何将其他参数发布到页面?因为它们不在 xmlHTTPRequest 中。
  • 我认为 $_POST 会将变量传递给 resubmit.php ?我为其他人做了同样的事情,它的工作原理......
【解决方案2】:

您必须发送价值广告帖子才能从 $_POST superglobal 访问。只需修改 Javascript 代码

function RemoveDoc(Doc,FirstReport,SecReport,FirstReportPath,SecReportPath)
{
 xmlhttp=new XMLHttpRequest();
 xmlhttp.open("POST","functions/Resubmit.php",true);
 xmlhttp.onreadystatechange=function(){
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
      document.getElementById("RemoveMsg").innerHTML=xmlhttp.responseText;
 }
 xmlhttp.send("Doc="+Doc+"&FirstReport="+FirstReport+"&SecReport="+SecReport);
 //do for others also in the same way
 return false;
}

【讨论】:

  • 在这种情况下,我如何调用不同的按钮?现在是 RemoveDoc(this.name).. 这是 RemoveDoc(this.doc)。我如何在 FirstReport 中添加不同的按钮?
  • 为每个按钮使用自定义标签,然后尝试 RemoveDoc(this.name,this.getAttribute("FirstReport"),.....etc)
  • 它在 HTML 而不是 php
  • like 隐藏时如何实现这里的功能?
  • 告诉我一件事是什么启动了您的 RemoveDoc 功能?
猜你喜欢
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多