Learning php - Help with superglobals

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
ericstanicki
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2010 5:35 pm

Learning php - Help with superglobals

Post by ericstanicki »

This is just a script from the book so im not looking for help with the code persay but rather i seem to be having a hard time if i dont use superglobals to retrieve information from the forms...... anything im missing? im running apache with php5 local

so for example this code executes correctly


if (isset($_GET['source'])) {

if (is_numeric($_POST['quantity'])) {
$_POST['total'] = (($_POST['quantity']) * ($_POST['price'])) * ($taxrate + 1);

but this one dosent


if (isset($source)) {

if (is_numeric($quantity)) {
$total = ($quantity * $price) * ($taxrate + 1);


Code: Select all

<?php

function calculate_total($quantity, $price, $taxrate) {
    $total = ($quantity * $price) * ($taxrate + 1);
          $total = number_format ($total, 2);
          echo 'You are purchasing $quantity widget(S) at a cost of \$$price each for a total of \$$total \n' ;
        }


if (isset($_GET['source'])) {
    
        if (is_numeric($_POST['quantity'])) {
          $_POST['total'] = (($_POST['quantity']) * ($_POST['price'])) * ($taxrate + 1);
          $_POST['total'] = number_format (($_POST['total']), 2);
          echo "You are purchasing {$_POST['quantity']} widget(S) at a cost of \${$_POST['price']} each for a total of \${$_POST['total']} \n" ;
        } else {
            echo 'Please enter a valid quantity for purchase';
            }
        } else {
            echo 'you have accessed this page inappropriately';
            }
    


?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Learning php - Help with superglobals

Post by superdezign »

There is a setting in PHP called register_globals. It creates variables based off of the $_REQUEST superglobal array. The $_REQUEST array is built out of the other superglobal arrays based on priorities. The general priority, from greatest to least, is GPCS which means "GET => POST => COOKIE => SESSION". GET data comes from the URL. POST data comes, more securely, through form posts. Using $_REQUEST leaves you at the risk of using GET data when you mean to use POST data.

This is partly why register_globals has been deprecated. You know where you expect your request to come from. So, get it from there.
ericstanicki
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2010 5:35 pm

Re: Learning php - Help with superglobals

Post by ericstanicki »

well the book im reading had me create the form

<form action="handle_calculator.php?source=calculator.html"
method="post">
<select name="quantity">
<option value="">Select a quantity</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<div align="left"><input type="submit" name="submit" value="TOTAL!" /></div>
<input type="hidden" name="price" value="19.95" />
<input type="hidden" name="taxrate" value=".05" />
<input type="hidden" name="source" value="'calculator.html'" /></form>


it says the that the "name" of the inputs will create variables with the names with a preceeding $ but it dosent seem to be working although if i use the $_GET or rather $_POST superglobal i can get the values from the form. why isnt it creating $price and $taxrate $quantity for the php script to use
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Learning php - Help with superglobals

Post by superdezign »

Because register_globals is deprecated. It is disabled by default. The book that you are reading is outdated.
ericstanicki
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2010 5:35 pm

Re: Learning php - Help with superglobals

Post by ericstanicki »

ahh ic i was thinking about it all day at work obviously i have much to learn i appreciate your time. My first mistake i guess was assuming the code from the book would work :-D
Post Reply