how can I use global variables in php

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 64
Joined: Thu Mar 14, 2013 6:10 am

Mon Mar 18, 2013 3:45 am

i have php page where i want to read the values of combo box and concatenate the string and submit pass that string to insert into database to other page.

I need something like global variable, so that i store the concatenated string in that global variable and assess that variable in 2nd class where I am updating the database.

can anyone help me how can I do in php.

in simple term

1st page I read the values from 20 combo boxes and click submit

on Submit event I update the records in Database this is done in 2nd Page

can anyone help and do i need to do database operations in 2nd page or shall I do it in 1st page only

php-forum Fan User
php-forum Fan User
Posts: 974
Joined: Mon Oct 01, 2012 12:32 pm

Mon Mar 18, 2013 7:50 am

for forms, you typically want to use super globals rather than globals. :D The global keyword is a little counter intuitive in PHP if you have experience with other languages in that it doesn't declare the scope of the variable in question, it actually pulls in a variable that has been declared outside of the scope you're trying to use it in. For forms though, you have a few "super globals" that do exactly what you want. There are 3, but only 2 that you should ever really use. $_GET, $_POST, and don't use $_REQUEST. The third grabs information from the first first two, but it can give malicious users the ability to override your posted variables with their own get values which are not bound by your input validation and can cause some real problems. ok, so back to the question. The method of your form determines whether you use $_GET or $_POST but unless you're trying to show something to your visitors in the address bar, post is the way to go. Each of the super globals are arrays that contain elements indexed by name. you can use isset($_POST) or even just if ($_POST) to see if you're responding to a posted form, and then access individual input values by their name property in the form $_POST['input_name']. Here's a little nothing i just wrote in response to another post to show them how to use radio buttons, but it uses a posted form so it fits here too.

Code: Select all


if (!$_POST) { //display the form since we're not responding to it yet
    echo '
<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">
<input type="text" name="cost" value="" /><br />    
<input type="radio" name="pay" value="25" />25<br />
<input type="radio" name="pay" value="50" />50<br />
<input type="radio" name="pay" value="75" />75<br />
<input type="radio" name="pay" value="100" />100<br />
<input type="submit" value="Multiply the text box by the radio button" />
} else { //process form 
    //first for demonstration purposes; here's what we're working with:
    echo '<pre>';
    echo '</pre><br />';
    echo 'The answer is ' . $_POST['cost'] * $_POST['pay'];

Post Reply