tags:

views:

32

answers:

3

Having a little bit of trouble with this section of code:

if(!empty($_POST['RemoveRequest']) && isset($_POST['RemoveRequest'])){ $action = $_POST['RemoveRequest']; }
echo '<form method="post" action="'.$page_name.'">
<input type="submit" value="[Remove Request]" name="RemoveRequest" /></form></td>'
.'</tr></table>';

if($action == 'RemoveRequest'){

$sql = "DELETE FROM ".TBL_FACTION_REQUESTS." WHERE r_id=$id LIMIT 1";
mysql_query($sql);
header("Location: ".$page_name."");

}

This form and server code is throwing this error:

Notice: Undefined variable: action in C:\xampp.. on line 97.

Then the record isn't deleted from my DB.

I'm really confused at what i'm doing wrong here? Tried a bit of debugging myself, but haven't had any luck yet. Hoping someone can help me out!

+1  A: 

It looks your opening condition isn't evaluating to true -

if(!empty($_POST['RemoveRequest']) && isset($_POST['RemoveRequest']))

If this statement doesn't evaluate to true, $action doesn't get set. The warning you're seeing ("Undefined variable..") means that the first time $action is being encountered in the above script flow it hasn't had any value set

// action hasn't been set by here..
if($action == 'RemoveRequest'){

If $action isn't being set, then it obviously won't evaluate to RemoveRequest, so the block of code with your db query will never run.

When debugging it, try to print out the value of $action and $_POST['RemoveRequest'] on the second line of your code. It may be that the variables are not getting set in the way you anticipate by the form which is posting to this script.

ConroyP
I echoed: echo $_POST['RemoveRequest'];and got:Notice: Undefined index: RemoveRequest. I don't know what it means for my problem?
Callum Johnson
Try a print_r on $_POST as the first line in the script - it sounds like your post data isn't being populated in the way you think it is (usually a typo in field name/looks like extra brackets in your field "value" above - should become clear if you print_r and see what's actually being sent in $_POST.
ConroyP
print_r($_POST); gives the following result: Array ( ) I have no idea what it means still.
Callum Johnson
i removed the header location part and got: Array ( [RemoveRequest] => RemoveRequest )
Callum Johnson
A: 

Have you tried wrapping the if command like so:

if( !(empty($_POST['RemoveRequest'])) && (isset($_POST['RemoveRequest'])) ){ 
   $action = $_POST['RemoveRequest']; 
}

That's the only thing that will stop your variable from not being defined. Also, you may want to change value="[Remove Request]" because it will never evaluate true for if ($action =='RemoveRequest')

Robert
A: 

error is of level E_NOTICE. Need to add this code before the code:

 $action = "";
Callum Johnson