Reputation: 11275
So I am trying to submit a variable and the name of the variable via a form. I switched a button from submit to button because I need additional validation.
Anyway, here's the button now:
<button type="button" onclick="subForm()" name="del" id="deletebutton" value="'.$org.'">Delete</button>
Here's my current validation:
<script type="text/javascript">
function subForm()
{
if(confirm("Are you sure you want to delete this?"))
document.forms["addorg"].submit();
else
return false;
}
</script>
And here's my script on the other side:
if (isset($_POST["del"]) && ($_POST['del'] !== '')) {
$del = mysql_real_escape_string(html2txt($_POST['del']));
$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);
$org_name = mysql_real_escape_string(html2txt($_POST['orgname']));
if (!$resfile)
header('Location: '.$admin.'?error=query');
while ($filerow = mysql_fetch_array($resfile)) {
$fileplace = $filerow['file_loc'];
unlink(".".$fileplace);
rmdir($org_name);
}
mysql_query("DELETE from organization where org_id='".$del."'");
header('Location: '.$admin);
}
It is not currently deleting the records that I want. How do I pass along the "del" name to the other page?
Upvotes: 3
Views: 4116
Reputation: 14469
I suggest you rethink using a form at all and consider AJAX. This would solve the problem of knowing which button was clicked and the page doesn't need to reload.
Here is a sample of what you're trying to do:
<html>
<head>
<script type="text/JavaScript">
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
function deleteOrganization(orgID)
{
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// in your PHP file, have it echo something like "SUCCESS" or "FAIL", and the response will be alerted here
alert(xmlhttp.responseText);
refreshList(); // either call another function to update the list or return the new list after the success/fail response.
}
};
xmlhttp.open("GET", "delete.php?orgID="+ orgID, true);
// OR - xmlhttp.open("GET", "page.php?action=DELETE&orgID="+ orgID, true);
xmlhttp.send();
}
function refreshList()
{
// either delete the row with JavaScript or make another AJAX call to get the new list after the entry was deleted.
}
</script>
</head>
<body>
<button onclick="deleteOrganization('<?php echo $org; ?>')">Delete</button>
</body>
</html>
Upvotes: 0
Reputation: 5002
instead use onsubmit
<form method='POST' onsubmit='return subForm();'>
and
<script type="text/javascript">
function subForm()
{
if(confirm("Are you sure you want to delete this?"))
return true;
else
return false;
}
</script>
edit: you can also change
if (isset($_POST["del"]) && ($_POST['del'] !== '')) {
to
if ( !empty($_POST['del']) ) {
but i think this line is your problem
$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);
try
$resfile = mysql_query("SELECT file_loc from organization WHERE org_id = '".$del."' ");
Upvotes: 1
Reputation: 5377
You can use <input type="hidden">
:
echo '<input type="hidden" name="org_id" value="'.$org_id.'" />'
This should render something like:
<input type="hidden" name="org_id" value="1" />
Using this code you can access the hidden field data using:
$org_id = $_POST['org_id'];
Upvotes: 3
Reputation: 5069
Looking at http://www.w3schools.com/tags/tag_button.asp suggests that the 'name' of a button isn't submitted as opposed to its 'value' or 'text'. Why not use an input of type hidden as just suggested?
Upvotes: 0