0

I'm trying to make a button that deletes a certain item with a certain FeatureID in my database. I have to do this using ajax, which worked but I also need a backup for people who don't have javascript enabled.

if ($_SESSION['UserID'] == "1") 
                {
                    echo '<form method="post" id ="featureDelete" name="featureDelete" action="ajax-follow.php?type=delete&amp;id=' . $row['FeatureID'] . '"><button data-id=' . $row['FeatureID'] .' class="delete" > Delete </button></form>' . '<br>';

                }

that was the code I used for my button and it gets me to this link

http://localhost:8080/phptesting/ajax-follow.php?type=delete&id=27

with the error:

Notice: Undefined index: featureDelete in C:\xampp\htdocs\phptesting\ajax-follow.php on line 23

` and this is the code I'm using on my ajax-follow.php

<?php

session_start();
include_once('class/db.class.php');
include_once('class/features.class.php');
include_once 'class/vote.class.php';


$v = new vote();
$f = new feature();

if (!empty($_POST['featureID'])) 
{
    if (($_POST['type']) == "vote") 
    {
        $v->setM_iFeatureID($_POST['featureID']);
        $v->setM_iUserID($_SESSION['UserID']);

        $v->save();      
    }

}
      if (($_POST['type']) == "delete") 
      {
        $f->Delete($_POST['featureID']);
        header('Location: features.php');
       }

I have a few questions:

  • Is this a good way to do this (This is my first time doing a delete button)
  • How can I fix my error?

2 Answers 2

1

No. You are adding a query string to the action attribute of the form. These variables will be available in $_GET and not in $_POST. Apart from that, when you manipulate a database you should always use POST instead of GET.

An easy way to get your form work the regular / no-ajax way, is to add hidden fields to your form:

<form method="post" ...>
  <input type="hidden" name="the_name_you_need" value="the_value_required">
  ...
</form>

Now the variables will be available in $_POST like you expect them to be.

0

Never include the ? and parameters in a form's action attribute, ever. They will get truncated from the request. Put the parameters that are to be hardcoded (so far as the webbrowser knows) in <input type='hidden' name='param' value='whatever' />

Instead of

<form method="post" id="featureDelete" name="featureDelete" 
action="ajax-follow.php?type=delete&amp;id={$row['FeatureID']}">

Do

<form method="post" id="featureDelete" name="featureDelete" 
action="ajax-follow.php">
   <input type='hidden' name='type' value='delete' />
   <input type='hidden' name='id' value='<?php echo $row['FeatureID']; ?>' />
....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.