Forum Moderators: coopster
I have a list of content items that is grabbed from a mysql database through a loop in PHP. For each content item, I have a checkbox. want I found I really needed, was the option to change the forms action URL with Javascript, so I dug up a script to do so...
function OndeleteContent()
{
document.contentForm.action = "?task=content&action=delete_content"
document.contentForm.target = "_self"; // Open in a new window
document.contentForm.submit(); // Submit the page
return true;
}
Thats the Javascript I use for deleting items. Should work in conjunction with this ...
<form name="contentForm" method="post" action="">
<input type="image" src="templates/images/delete_content.gif" alt="Delete Content" class="contentOption" name="OndeleteContent" onclick="return OndeleteContent();">
The Javascript and image submit sends me to the appropriate page, but doesnt appear to be sending any of the data with it. I checked to see if the form submittion was set through PHP, else return a message "Not set" and I costantly retrive Not Set message. Not sure if this is a PHP error or something with the Javascript, but here is also a small bit of the PHP...
if (isset($_POST['OndeleteContent']))
{
foreach($_POST['checkbox'] as $id)
{
$sql = "DELETE FROM content WHERE id = '$id'";
$result = mysql_query($sql) or die("Error in Query");
}
}
else {
echo "Not set";
}
Can anyone help diagnose this issue? If it helps, I have other options to edit items, publish, and unpublish items, which are all controlled by similar methods (Javascript functions). I would link to live example, but I am testing locally through XAMPP.
If your problem is between GET and POST array then as I told you earlier use $_REQUEST['varName'] this will solve it automatically and will check both POST and GEY arrays.
secondly I saw you are using a queryString kinda URL in javascript to change the form action URL so why dont you put your variable in that URL and then check that variable in the receving page with $_GET as it will be a sure shot to have that in GET since it will be going in the query string?
you can skip using the submit button name with this.
<input type="image" src="templates/images/delete_content.gif" alt="Delete Content" class="contentOption" name="OndeleteContent" onclick="return OndeleteContent();">
This looks a bit odd to me, using the same element name as a function name?! Is that valid?! Even if it is, the potential for error/confusion is dramatically increased!