Get an argument

GET only using $_GET
  $MyVariable = "";
  if (isset($_REQUEST['MyVariable']))
    $MyVariable = $_REQUEST['MyVariable'];
POST only using $POST
  $MyVariable = "";
  if (isset($_POST['MyVariable']))
    $MyVariable = $_POST['MyVariable'];
GET or POST using $_REQUEST
  $MyVariable = "";
  if (isset($_REQUEST['MyVariable']))
    $MyVariable = $_REQUEST['MyVariable'];
  if (isset($_POST['MyVariable']))
    $MyVariable = $_POST['MyVariable'];
Did GET or POST occur
if ($_SERVER["REQUEST_METHOD"] === "POST")

Example GET URL

http://www.somedomain.com/somedir/somepage.php?name_a=value_a&name_b=value_b&name_c=value_c

Start with an ‘?’, then separate other values with an ‘&’

To read:

  $MyVariable = "";
  if (isset($_REQUEST['my_variable']))
    $MyVariable = $_REQUEST['my_variable'];

   if (!empty($_REQUEST['redirect_to']))    //Note that a value of zero will cause empty() to be true!!!!!!!

   if (isset($_REQUEST['source']))    //A value of zero will still give isset() being true so this is often the better choice then empty() 

   echo $_REQUEST['name_a'] . "<br/>";
   echo $_REQUEST['name_b'] . "<br/>";
   echo $_REQUEST['name_c'] . "<br/>";

   $name_a= $_REQUEST['name_a'];


  if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == 'yes'))

  if (isset($_REQUEST['image_upload_filename']))

  if (isset($_REQUEST['image_upload_filename'])) && $_REQUEST['image_upload_filename']  == 'abc')

  $arg1 = 0;
  if ($_REQUEST['arg1'])		//<<Remember this will be false if the value is 0!
    $arg1 = (int)$_REQUEST['arg1'];

Example For POST Parameters

   echo $_POST['name_a'] . "<br/>";
   echo $_POST['name_b'] . "<br/>";
   echo $_POST['name_c'] . "<br/>";

   $name_a= $_POST['name_a'];


  if (isset($_POST['submitted']) && $_POST['submitted'] == 'yes')

  if(isset($_POST['image_upload_filename']))

Sanitise Them!!!

htmlentities() converts things like < > ” \ etc into HTML strings like < so they become harmless.

     echo htmlentities($_REQUEST['name_a']) . "<br/>";

POST OR GET

  $group_id = mysql_real_escape_string($_GET['gid']);
  if($group_id == "")
    $group_id = mysql_real_escape_string($_POST['gid']);

Debug Show All Posted Values

//----- DISPLAY ALL POSTED VALUES -----
echo"[POSTED VALUES START]<br />";
foreach($_POST as $posted_var => $posted_value)
	echo $posted_var . ' : ' . $posted_value . "<br>";
echo"[POSTED VALUES END]<br />";

Is A Form Value Present

	if ($_POST['delete_user'])

Getting Form Values

Each form item with a name will be posted.

Multiple Images As Submit Buttons

This example witth POST ‘delete_user’ if the button is pressed but not if it isn’t

  <input type="image" src="/button_delete.png" alt="Delete" value="Delete" name="delete_user" onClick="return(window.confirm('[var.confirm_popup_javascript]'))" />

  //This is the PHP test for was the button pressed
  if ($_POST['delete_user_x'])

Getting Arguments From A URL String

	$url = "http://www.mydomain.com/watch?v=fdagikhfdaq&res=45";
	parse_str(parse_url( $url, PHP_URL_QUERY ), $array_of_vars);
	echo $array_of_vars['v'];    
  //Outputs: fdagikhfdaq

Storing A Value In A Page Using POST

This can be a way of overwriting an existing POST value or passing a value to another php file which will get used as a script include later for an example

	$_POST["player_video_id"] = $player_video_id;
	$video_index = (int)$_POST['player_video_id'];
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *