Switch Statements and Form Variables

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
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

Switch Statements and Form Variables

Post by rprins »

Ok, so I have a few switch statements set up to do various things. These things include updating the DB, deleting something from the DB, and viewing a list of items in the DB.

Code: Select all

<?php
if($a){
    switch($a){
        case "update":
               //some code
        break;
        case "delete":
              //some code
        break;
        // etc
    }
}
?>
Now, I can get it from the list to the screen to update beacuse that is done via a link, but on the update screen there are 2 form buttons.

Code: Select all

&lt;input name="update" type="submit" value="Update"&gt;
&lt;input name="delete" type="submit" value="Delete from DB"&gt;
The beginning of the form looks like:

Code: Select all

&lt;form name="addrecord" method="post" action="&lt;?php echo"$PHP_SELF?a=$name"; ?&gt;"&gt;
The problem that I run into is that I cannot pass the var from the buttons to the address bar to trigger my switch statements. Any idea on how to do this?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

use
<?php
<form name="addrecord" method="get" action="<?php echo"$PHP_SELF?a=$name"; ?>">
?>
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Sorry, double post.
Last edited by qartis on Sun Jun 29, 2003 4:48 pm, edited 1 time in total.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

The form you have, wouldn't set the variable $a.

Code: Select all

&lt;form name="addrecord" method="post" action="&lt;?php echo"$PHP_SELF?a=$name"; ?&gt;"&gt;
&lt;input name="update" type="submit" value="Update"&gt;
&lt;input name="delete" type="submit" value="Delete from DB"&gt;
So, if you clicked "Update", you would be sent to $PHP_SELF?a=$name, with $_POST['update'] equal to "Update". Perhaps you wanted something like this:


HTML

Code: Select all

&lt;form name="addrecord" method="post" action="action.php"&gt;
&lt;input name="action" type="submit" value="Update"&gt;
&lt;input name="action" type="submit" value="Delete from DB"&gt;

action.php:

Code: Select all

<?
switch ($_POST['action']){
	case "Update":
		update()
		break;
	case "Delete from DB":
		delete();
		break;
}
?>
Post Reply