Using 2 buttons in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Using 2 buttons in a form

Post by kkurkowski »

I am trying to use 2 buttons in a form and one is edit and other is delete. So they are 2 seperate commands. How does PHP know which one is clicked? Does PHP use the buttons name and then declares the code?
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

Code: Select all

<?
if ($_GET&#1111;'buttonDelete']) &#123;
  print "<p>deleted</p>\n";
&#125; else if ($_GET&#1111;'buttonEdit']) &#123;
  print "<p>edit</p>\n";
&#125;
?>
<form method="get">
<input type="submit" value="Edit" name="buttonEdit">
<input type="submit" value="Delete" name="buttonDelete">
</form>
just like that
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

ok, when I click the radio button now after I added that in and everything. This is what shows up in the URL bar for that file.

deledt.php?deledt=14&del=Delete

That is when I hit the Delete button when I select one of the radio buttons. Then I get to the page and it gives me these 2 errors.
Warning: Undefined index: edt in deledt.php on line 43

Warning: Cannot add header information - headers already sent by (output started at header.php:19) in deledt.php on line 50
This si the code I have so far... Right now I am just trying to get the delete command working, but it seems to be not working. I think I have the wrong command in to delete the table.

Code: Select all

<?
require("news_connect.php");
if ($deledt =="") &#123;
	header("Location: http://www.bve.32k.org/posts.php");
	exit;
&#125;
	if ($_GET&#1111;'edt']) &#123;
		

	&#125;

	else if ($_GET&#1111;'del']) &#123;
		$sql = "DELETE FROM news_news WHERE id = "$deledt"";
		header("Location: http://www.bve.32k.org/posts.php");
		exit;
&#125;
?>
airo
Forum Commoner
Posts: 31
Joined: Thu Apr 24, 2003 4:07 pm

Post by airo »

SImple way to do this: Name the buttons "edit" and "submit". Then in the action url file, run an if($edit) { blah } if($submit) { blah^2 }.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

But it still gives me the same error and won't delete the post that I selected. I changed it around, but then it just says undefined index, so I changed it back.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

post the code
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Action code

Code: Select all

<?
require("news_connect.php");
if ($deledt =="") &#123;
	header("Location: posts.php");
	exit;
&#125;
	if ($_GET&#1111;'edit']) &#123;

	&#125;

	else if ($_GET&#1111;'submit']) &#123;
		$sql = "DELETE FROM news_news WHERE id = "$deledt"";
		header("Location: posts.php");
		exit;
&#125;
?>
Select post code

Code: Select all

<?
require ("news_connect.php");

$result = mysql_query ('SELECT id,headline,user,timestamp FROM news_news WHERE user="'.$user.'" ORDER BY timestamp');

while ($data = mysql_fetch_array ($result))
&#123;
?>
<form name="deledt" method="get" action="deledt.php">
<table width="425" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr> 
    <td width="20" bgcolor="#B3CDDC"><input type="radio" name="deledt" value="<? echo $data&#1111;"id"]; ?>"></td>
    <td width="74" bgcolor="#B3CDDC"><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif"> <? echo date("m-d-Y", $data&#1111;"timestamp"]) ?></font></td>
    <td width="327" bgcolor="#B3CDDC"><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif"> <? echo $data&#1111;"headline"]; ?></font></td>
  </tr>
</table>
<?
&#125;
?>
<br>
<table width="124" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="124"><div align="center">
        <input name="edit" type="submit" class="textbuttons" value="Edit"> 
        <input name="submit" type="submit" class="textbuttons" value="Delete">
      </div></td>
  </tr>
</table>
airo
Forum Commoner
Posts: 31
Joined: Thu Apr 24, 2003 4:07 pm

Post by airo »

Any header information needs to take place before output.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

what version of php are you using??? check viewtopic.php?t=511
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have a try of this for debugging:

Code: Select all

<?php

//trim all the user input to make sure there aren't any stray spaces
foreach ($_GET as $key => $value) {
	$_GET[$key] = stripslashes($value);
}

// print_r the array to check the data
echo '<pre>';
print_r($_GET);
echo '</pre>';

require 'news_connect.php';

// use isset() and empty() to test if values are set

// use $_GET['deledt'] instead of $deledt
if (empty($_GET['deledt'])) { 
   header('Location: posts.php');
   exit; 
} 

if (isset($_GET['edit'])) { 
	echo 'edit was pressed';
} elseif (isset$_GET['submit']) { 
	echo 'submit was pressed';
	// once again $_GET['deledt'] instead of $deledt
	$sql = "DELETE FROM news_news WHERE id = '".$_GET['deledt']"'"; 
	header('Location: posts.php'); 
	exit; 
} 

?>
When you test for the existance of variables you should be using isset() or empty() - only use this construct:

Code: Select all

if ($variable) {
    // blah
} elseif (!$othervariable) {
   // blah
}
if you are testing to see whether a variable has a value of TRUE or FALSE.

Mac
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

I am using version 4.11 for the PHP
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

For some reason it does not like this line of code.
$sql = "DELETE FROM news_news WHERE id = '".$_GET['id']"'";

It says parse error. It did the same thing for that line in the script I wrote.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

try

$sql = 'DELETE FROM news_news WHERE id = "'.$_GET['id'].'"';

check the single quotes and the double quotes

$sql = opensignlequote DELETE .... id = doublequote singlequote .$GET['id']. singlequote doublequote " doublequote
Post Reply