Page 1 of 1

NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 7:49 am
by ljpv14
GUYS I NEED HELP. I use echo to print my form. there is a submit button on the first set of forms. i tried posting it and it works. then i need to have another submit button and used echo again to display that. but there is an error saying: unidentified index delete. I've been trying to fix my code but I can't seem to find what's wrong. HELP GUYS here is the code.

Code: Select all

echo "
<h1>Delete Accounts</h1>
<form action = 'delete.php' =  method= 'post'>
Enter Username:<input type = 'text' name = 'lookup'>
<input type = 'submit' name = 'submit' value='Search'>";

if(isset($_POST['submit']))
{
$x = mysql_query("SELECT * FROM tblaccounts WHERE username = '$_POST[lookup]'");
$row = mysql_fetch_array($x);
        if($row)
        {
                $user = $row['username'];
                $eadd = $row['email'];
                $fname = $row['firstname'];
                $nname = $row['nickname'];
                $lname = $row['lastname'];
               
        echo"
        <table border = 0>
        <tr><td>Username :&nbsp; </td><td>$user<td></tr>
        <tr><td>Password :&nbsp; </td><td>*****<td></tr>
        <tr><td>Email-Address :&nbsp; </td><td>$eadd<td></tr>
        <tr><td>First Name :&nbsp; </td><td>$fname<td></tr>
        <tr><td>Nickname :&nbsp; </td><td>$nname<td></tr>
        <tr><td>Last Name :&nbsp;</td><td>$lname<td></tr>
        </table>";
        echo "<input type = 'submit' name = 'delete' value = 'Delete'></td></tr><br><br>";
        if($_POST['delete'])
        {
                echo "Are you sure you want to delete $user's details?
                <tr><td><input type = 'submit' name = 'yes' value = 'SUBMIT'><input type = 'submit' name = 'no' value = 'No'><br><br></td></tr>
                </form>";
               
                if($_POST['yes'])
                {
                        mysql_query("DELETE FROM tblaccounts Where username = '$_POST[lookup]'");
                }
        }
        }

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:02 am
by icesolid
Can you show us the entire error? Index might have something to do with your SQL?

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:29 am
by ljpv14
Notice: Undefined index: delete in C:\xampp\htdocs\xampp\mprob1\delete.php on line 36

I don't think there's something wrong with my query. because when i remove my if condition the query works fine Just having some trouble doing the process inside the if($_POST['delete']){} it seems that the code does not recognize the name 'delete' from my forms.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:36 am
by Celauran
It's a notice, not an error. It's because you're trying to reference an array index -- $_POST['delete'] in this case -- which may not have been set. Check if it's set first and the notice goes away.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:41 am
by ljpv14
Already tried that. The notice is gone but still the process i defined inside my condition does not happen. When I try to echo my $_POST['delete'] there will be also a notice like what I posted.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:44 am
by Celauran
ljpv14 wrote:the process i defined inside my condition does not happen
What does this mean?

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 9:46 am
by ljpv14

Code: Select all

if($_POST['delete'])
{
echo "Are you sure you want to delete $user's details?
<tr><td><input type = 'submit' name = 'yes' value = 'SUBMIT'><input type = 'submit' name = 'no' value = 'No'><br><br></td></tr>
</form>";
}
I'm referring to this part. It's not happening.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Thu Nov 17, 2011 3:30 pm
by pickle
That if() statement isn't checking if the value exists - it is only checking if the value evaluates to TRUE. You can use the isset() function to check for a variable's existence:

Code: Select all

if(isset($_POST'[delete']))

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Mon Nov 21, 2011 3:57 am
by ljpv14
I already did that too. But I there's still no result. When I try to display all buttons at the same time, they all seem to work but when I display the secona button after the pressing the first one, the second button won't work.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Mon Nov 21, 2011 4:23 am
by maxx99
Just use:

Code: Select all

var_dump($_POST);
Problem will be quite easy to spot :)
Watch out for nested IF-s. Be sure that you're sending $_POST['yes'] and $_POST['delete'] at the same time!

Always use "empty" or "isset" when operating on array indexes.

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Mon Nov 21, 2011 4:30 am
by ljpv14
So I see that my delete button is not being posted. Because the value that I determine to my delete button is not listed at the $_POST array. What should I do? I really need to make my delete button work?

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Mon Nov 21, 2011 4:37 am
by maxx99
Easy :)
Set all "name" = "submit" in all buttons:

Code: Select all

<input type = 'submit' name = 'submit' value='Search'>
<input type = 'submit' name = 'submit' value = 'Delete'>
etc...

Create

Code: Select all

if(!empty($_POST('submit')){
 switch($_POST['submit']{
  case 'Delete':
    //print out "yes" "no" buttons
   break;
  case  'submit': //or search
    //print out what you need
   break;
  case 'yes':
    //exec query
  case 'no':
  default:
   break;
 }
}

Re: NEED HELP for processing 3 submit buttons inside a echo

Posted: Mon Nov 21, 2011 4:46 am
by ljpv14
That did the trick! I owe you one! Maybe I really need to learn when to use if statements and case statements. I've been fond on using if statements since I started programming in C. Thank you!