Results 1 to 4 of 4

Thread: 2 button form...how to choose which is pressed.

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default 2 button form...how to choose which is pressed.

    Ok so I have two buttons in a form. One is to modify text and one is to delete the photo. My problem is that I only want one to be read. Here is the code I tried:

    Code:
    if(isset($_POST['edit']))
    	{
    		$sql = "UPDATE pictures SET pictext = '".$_POST['pictext']."' WHERE id = '".$_POST['id']."'";
    		if (!mysql_query($sql))
    		  {
    			die('Error: ' . mysql_error());
    		  }
    		  else
    		  {
    			echo "1 record updated";
    		  }	
    	}
    	else if(isset($_POST['delete']));
    	{
    		$sql = "DELETE FROM pictures WHERE id = '".$_POST['id']."'";
    		if (!mysql_query($sql))
    		  {
    			die('Error: ' . mysql_error());
    		  }
    		  else
    		  {
    			echo "1 record deleted";
    		  }
    	}
    Now this doesn't work because both buttons are being posted obviously. Any quick ways to fix this? All help is appreciated.

  2. #2
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    One quick fix: give the buttons a different
    name=foo / name=foo2 and check with isset for example

    or...

    use two forms (if possible).


    here the example with name=
    Code:
    <form action="thispageorsomeother.php" method="post">
    <input type="submit" name="buttonone" value="boo" />
    
    <input type="submit" name="buttontwo" value="foo" />
    <form/>
    Now you can simply check
    $_POST["buttonone"] and $_POST["buttontwo"]
    Last edited by schorhr; 09-22-2008 at 08:31 AM.

  3. #3
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    I'm not sure if you looked at my code or not. That is exactly what I'm doing, but it is not working. And I don't want to do two forms..unless I ABSOLUTELY have to. Thank you for the reply.

  4. #4
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Strange, works fine here.
    Still, could you post the HTML-Part (at least the form)?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •