GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Aug-2004, 01:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Multiple submit buttons


I have a file containing calendar information -- one entry per line. In cal.php I'm displaying the information in a table inside a form for maintenance. I want to also add a submit button to each row labeled delete. In the action file caldel.php I need to get the 'value' of the submit button clicked telling me which line of the file to delete.

I can't figure out how to set up the submit button and where to get the value from in caldel.php. Especially since I have no idea how many values will be on the cal.php page.

In the input tag I obviously need value='delete'. Are hidden values available?

Trying for something like:
PHP Code:

print "<table border=1>";
        for ($n = 0; $n < $num; $n++)
        {
            $dd = explode("|", $filedata[$n]);
            print "<tr>";
            print "<td><input type='submit' name='del$n' value='delete'></td>";
            ...
            print "</tr>\r\n";
        }
        print "</table>"; 



Thanks...
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #2  
Old 08-Aug-2004, 10:10
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Good question... one I have never asked myself, unfortunately. What I might suggest is creating another form for each row, including hidden values containing the id for the row to be deleted. Would that work? I don't know, since I am just guessing here...

So using your example code, let's say you try something like this instead:

PHP Code:

<?php
        print "<table border=1>";
        for ($n = 0; $n < $num; $n++)
        {
            $dd = explode("|", $filedata[$n]);
            print "<tr>";
            print "<td>" . load_form_delete( $n ) . "</td>";
            ...
            print "</tr>\r\n";
        }
        print "</table>";

function load_form_delete( $int )
{
  return( '<form action="caldel.php" method="post">'.
          '<input type="hidden" name="row_id" value="'.$int.'" />'.
          '<input type="submit" name="delete" value="Delete" />'.
          '</form>'
        );
}
?>

  #3  
Old 09-Aug-2004, 04:34
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
I think that you are on the right track WaltP. I don't believe there is any problem with having multiple submit buttons in a form, as long as they've all got unique name/value pairs to distinguish them.

However you might make things a lot easier on yourself by having:
PHP Code:

print "<td><input type='submit' name='delete' value='$n'></td>"; 


so you can access the entry number to be deleted simply by $_GET['delete'].
So
PHP Code:

if(isset($_GET['delete']) && is_numeric($_GET['delete']) && $_GET['delete'] >= 0){
    delete_record($_GET['delete']);
} 


GF

That was unnecessary, but haven't typed PHP in a while
  #4  
Old 09-Aug-2004, 10:31
tubedogg tubedogg is offline
New Member
 
Join Date: Aug 2004
Posts: 11
tubedogg will become famous soon enough
One thing to keep in mind with Garth's method, your buttons will be displayed with $n as the title of them. As long as you keep that in mind, you should have no problems.

If you wish to take a slightly different approach and use "delete" as the title of your submit button, try this:
PHP Code:

<?

        print "<table border=1>";
        for ($n = 0; $n < $num; $n++)
        {
            $dd = explode("|", $filedata[$n]);
            print "<tr>";
            print '<td><input type="submit" name="todelete['.$n.']" value="Delete"></td>';
            ...
            print "</tr>\r\n";
        }
        print "</table>"; 


?>

In this way, you'd just check for a value of "delete" in the processing stage:
PHP Code:

<?

if (!empty($_GET['todelete'])) {
    foreach ($_GET['todelete'] AS $id => $checkval) {
        if ($checkval == 'Delete') {
            delete_record($id);
        }
    }
}

?>



Another thing to think about is if you ever will want to delete multiple rows at once. If you do, change your form to look like this:
PHP Code:

<?

    print "<table border=1>";
    for ($n = 0; $n < $num; $n++)
    {
        $dd = explode("|", $filedata[$n]);
        print "<tr>";
        print '<td><input type="checkbox" name="todelete['.$n.']" value="1" title="Delete this entry" /></td>';
        ...
        print "</tr>\r\n";
    }
    print "<tr><td align="right"><input type="submit" name="submit" value="Delete Checked" /></td></tr>";
    print "</table>"; 

?>


In this case, the processing code would be almost identical to that I have above, except instead of checking for the word "Delete", you'd check for a value of 1, like this:

PHP Code:

<?

if (!empty($_GET['todelete'])) {
    foreach ($_GET['todelete'] AS $id => $checkval) {
        if ($checkval == 1) {
            delete_record($id);
        }
    }
}

?>


Hope this helps.
  #5  
Old 11-Aug-2004, 21:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Thanks for the help, guys. Unfortunately, nothing worked. The foreach gave me nothing but syntax errors -- No matter how I tried to format it. But it sounds like that's what I was looking for.

I wonder if my PHP is too old. I'll check it later.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #6  
Old 11-Aug-2004, 21:51
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Post

This should be a simple problem to solve...

One way to debug these type of problems is to use print_r($_POST) to see what kind of data you're getting (you're using the POST method right?).

Anyway, I've run into stuff like this lots of times....

The way you have your code now, as posted, since you're setting the name differently, .... might I suggest you do things differently to save yourself some hassle?

A picture is worth a thousand words so let me show you a picture of your current code :

PHP Code:

print "<table border=1>";
        for ($n = 0; $n < $num; $n++)
        {
            $dd = explode("|", $filedata[$n]);
            print "<tr>";
            print "<td><input type='submit' name='del$n' value='delete'></td>";
            ...
            print "</tr>\r\n";
        }
        print "</table>"; 



beautiful.... but hmm.... lets do this instead!!

PHP Code:

print "<table border=1>";
        for ($n = 0; $n < $num; $n++)
        {
            $dd = explode("|", $filedata[$n]);
            print "<tr>";
            print "<td><input type='submit' name='del[$n]'  value='delete'></td>";
            ...
            print "</tr>\r\n";
        }
        print "</table>"; 



Note, I created an HTML array variable called 'del'.. and $n is the key value...

Next... how do you get the ID's selected? SIMPLE!


PHP Code:

foreach ($_POST['del'] as $key=>$value) {
            $id_array[] = $key; // as each key value set in
} 




Note that 'del' $_POST variables will NOT be set, unless they are selected. That is why this code works. $id_array with a closed braket simply puts all $key values into each available slot of $id_array. Now, if you want to ensure they're going in, in order do this:

PHP Code:

$index = 0;
foreach ($_POST['del'] as $key=>$value) {
            $id_array[$index] = $key; // as each key value set in
       $index++;

} 



voila! I wrote an article on my webpage about problems like this... I just know this is going to work.. try it out.

Anyway, when I first read this article I was half asleep... now I see what you're looking for. I hope this helps.
Last edited by JdS : 12-Aug-2004 at 08:07. Reason: Please insert your example PHP codes between [php] and [/php] tags
  #7  
Old 12-Aug-2004, 18:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Ahaaaa!! Fantastic!

I have no idea why I kept getting syntax errors... But now it seems to be peachy!

Thanks JasonMichael, and all others who tried to help. I think I've got enough to finish the job now.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
collecting user input from radio buttons. Adelle29 Web Design Forum 1 31-May-2004 16:28
strange sizeof(structure) - multiple of 8 pinkpanther C Programming Language 11 30-May-2004 07:20
GIDForums MemberRank buttons. JdS GIDForums™ 2 24-Jan-2004 21:35
GIDTopsites buttons jrobbio GIDTopsites™ 0 26-Mar-2003 08:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.