Jump to content

how do I use something like ?action=add


toolman

Recommended Posts

Hi there,

 

I am setting up a simple events system which will allow me to add events to my website.

 

I have a page called events.php, but wanted to set it up so when I add an event, the page is something like: events.php?action=add

 

How do I get the ?action=add part? Do I just insert my form on the events.php page?

 

Any help would be appriciated :)

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/185797-how-do-i-use-something-like-actionadd/
Share on other sites

These are called "GET Requests".

 

To use them here is a simple template:

 

<?php
// isset() = check if a variable is set (isset). $_GET is an assosciation array (names of the variables with the values).

if(!isset($_GET['action'])){
   echo("Page 1 <br />");
   echo("<a href='?action=page2'>Go to page 2</a>");

}elseif($_GET['action'] == "page2"){
  echo("Page 1");

}else{
  echo("Page doesnt exist");
}

?>

 

Hope this helps,

-CB-

In addition, you should use switch() for the action, like

 

 

<?php
$action = isset($_GET['action']) === true ? $_GET['action'] : "";

switch($action){

// case matches action
    case 'foo':
      
        echo "foo";

   break; // exit switch were done

     case 'bar':
        echo "bar";
   break;

    // defaults here if action does not exist, you can use this to your advantage to just throw an error 
    default:
    header("HTTP/1.0 404 Not Found");       
     $action = htmlentities($action);
    exit("Action ( {$action} ) undefined.");
    break;
}

 

You can copy that and save it as a PHP file and do this

 

test.php?action=foo

test.php?action=bar

test.php?action=will_throw_error

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.