Carter Roeser Carter Roeser - 1 year ago 70
PHP Question

PHP - If variable is not set, get post variable

I am trying to pass through a variable to a new name if it is set, or get a POST variable if the first variable is not set.

This is my code but it is not working:

if (isset($randombg)) {
$background = $randombg;
} else {
$background = $_POST['bg'])

What is wrong with it, or how can I fix this?

Answer Source

You have typos in your code. There is a ) in $background = $_POST['bg']) and a semicolon

you this should work:

$defaultbg = '#FFFFFF';

if (isset($randombg)) {
    $background = $randombg;    
} elseif( isset( $_POST['bg'] ) ) {  
   $background = $_POST['bg'];
       $background = $defaultbg;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download