I have 2 files that assist me in removing the document and updating the database. My first file consists of 1 form with a remove button and a javascript function called remove(). Second php page would be removing the file which it does not return any results.
Code for Remove.php (Upon calling out remove() function):
$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
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;
}
I did try to alert(Doc) out and the document name does show but on the second remove.php, it does not run any of the coding. Tried "GET"/"POST" also the same results. Kindly advise.
You have to send the values ad post to access from $_POST superglobal. Just modify the Javascript code
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;
}
It looks like your sending a post request, but sending your document name in the url, a $GET variable.
either switch to a get request:
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;
}
or send the document name as a post parameter:
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;
}
Also, your not waiting for a response from the server.
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;
}