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
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-

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.