Using radio button to determine further code on same page

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
SparrowHawk7
Forum Newbie
Posts: 1
Joined: Thu May 28, 2009 12:24 pm

Using radio button to determine further code on same page

Post by SparrowHawk7 »

I'm new to PHP but been programming in various other languages since 1970. I may be trying to do something here that PHP won't want to do but it's worth a try. I also have never learned HTML so I'm working on learning that as I go along. So far I'm not having much trouble outside of syntax errors .. but now I am wanting something a little more involved.

Basically I have a pair of radio buttons displayed (Edit/Delete) and based upon the user's selection of these I want to display a submit button either to submit the edit or submit the deletion to a dB. Here's a snippet of the code that is confusing me ... it's all pretty obviously in PHP tags

Code: Select all

 
        echo "<form action=\"update_gallery.php\" method=\"post\">";
        echo "<INPUT TYPE = \"Radio\" Name =\"edit\"  value= \"edit\" CHECKED>Edit";
        echo "<INPUT TYPE = \"Radio\" Name =\"edit\"  value= \"del\" >Delete";
        $sel_edit=$_POST["edit"];
 
When I try to run this I get an undefined index error in the last line. What my idea was to try after that is this::

Code: Select all

 
         $sel_edit=$_POST["edit"];
        {
            echo "<input type=\"submit\" value=\"Update Gallery Name\"/>";
        } else {
            echo "<input type=\"submit\" value=\"Delete Gallery Name\"/>";
        }
 
I've only been working with PHP for a couple days so I'm probably not grasping nuances here. My idea is to use 1 page to handle 3-4 jobs instead of coding individual pages for each job. Functions and OOP isn't very hard though the syntax requires a bit of trial and error. So far I've managed to get the one page to edit, delete and add but this code would tend to let me combine the edit and delete into 1 block. But I may be asking it to do too much.

TIA for your help,
Ken
Last edited by Benjamin on Fri May 29, 2009 10:24 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using radio button to determine further code on same page

Post by requinix »

You won't have $_POST["edit"] until the form has been submitted. The first time you view the page it won't exist so you get those errors.

Use a function like isset to check if it exists (ie, whether the form has been submitted).

Code: Select all

       echo "<form action=\"update_gallery.php\" method=\"post\">";
        echo "<INPUT TYPE = \"Radio\" Name =\"edit\"  value= \"edit\" CHECKED>Edit";
        echo "<INPUT TYPE = \"Radio\" Name =\"edit\"  value= \"del\" >Delete";
 
        if (isset($_POST["edit"])) {
            // form was submitted
            $sel_edit=$_POST["edit"];
            if ($sel_edit == "edit") {
                echo "<input type=\"submit\" value=\"Update Gallery Name\"/>";
            } else /* if ($sel_edit == "del") */ {
                echo "<input type=\"submit\" value=\"Delete Gallery Name\"/>";
            }
Post Reply