Solved

Keeping radio button state

Posted on 2014-10-10
21
286 Views
Last Modified: 2014-10-20
I have created several radio buttions with yes or no values. The degault values are checked yes. I want to be able to change the answer to no next time a visit my page in any browser. SO not just local storage or cookies. THis would be for anyone who goes to my page. I am currently disabling the radio buttons after making a change but not sure how to make the value stay.

Thanks in advance.
her is my jsfiddle
http://jsfiddle.net/augustkrys/rm5qLy4k/
I am running on PHP and Mysql.
0
Comment
Question by:augustkrys
  • 11
  • 10
21 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 40374002
If you want to keep stateful information about clients, your most mainstream approach will be to use a database table that stores the state information and a cookie that associates the client with the information in the database.  If this is just a short-term requirement, you may be able to use the PHP session to store the information.  Either way, you can store the information about the status of the radio buttons and retrieve it when your script produces the form.  This will require submitting the form to a PHP action script.  I'll try to show you an example in a moment.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40374027
Here is a way to remember the selections from one request to the next.
http://iconoun.com/demo/temp_augustkrys.php

<?php // demo/temp_augustkrys.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Database/MySQL/Q_28535559.html

// ALWAYS START THE SESSION ON EVERY PAGE
session_start();

// IF NEEDED, INITIALIZE THE RADIO BUTTON SETTING
if (empty($_SESSION['mood'])) $_SESSION['mood'] = 'meh';

// IF THERE IS A POST-METHOD REQUEST, SAVE THE STATUS OF THE RADIOS
if (!empty($_POST['mood']))
{
    $_SESSION['mood'] = $_POST['mood'];
}

// GENERATE THE RADIO BUTTONS
$radio_buttons = NULL;

$radio_buttons .= '<input type="radio" name="mood" value="happy" ';
if ($_SESSION['mood'] == 'happy') $radio_buttons .= 'checked';
$radio_buttons .= '/>Yes, happy ' . PHP_EOL;

$radio_buttons .= '<input type="radio" name="mood" value="unhappy" ';
if ($_SESSION['mood'] == 'unhappy') $radio_buttons .= 'checked';
$radio_buttons .= '/>No, unhappy ' . PHP_EOL;

$radio_buttons .= '<input type="radio" name="mood" value="meh" ';
if ($_SESSION['mood'] == 'meh') $radio_buttons .= 'checked';
$radio_buttons .= '/>No Answer ' . PHP_EOL;

// CREATE A FORM USING THE RADIO BUTTONS
$form = <<<EOD
<form method="post">
<b>Happy?</b> $radio_buttons <br>
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

Please post back if you have questions, ~Ray
0
 

Author Comment

by:augustkrys
ID: 40374204
ray,

Thanks for the answer. I am looking for a permanent solution.  for example. any user goes to the site and selects happy, and the another user goes to the site they will see that selection. so not a cookie/ session. The selection doesn't need to be per users just for all users .

if i choose happy in chrome and then go to the site from say IE - the last selection from any user is the new default.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40374237
Ahh, now I understand better.  There are two ways to handle this.  One of them is to use a small text file stored on your server.  All it will need is a single indicator of Happy or Not Happy.  When your script creates the HTML form, it will make reference to the text file for the way to set the radio buttons.  When your action= script receives the form input, it will replace the small text file with the appropriate indicator, as designated in the request.

The other way to handle this would be to use a database table to store the current state of the radio button.  If you're not already using a database, this is more work to set up, but it also gives you more flexibility and capability to do things like record all of the updates to the state of happiness.  For example, you could use the database to answer the question, "When did we stop being happy?"
0
 

Author Comment

by:augustkrys
ID: 40374293
I am using MySQL - in the real scenario I have several  radio buttons . I am not currently posting to the db but could . I am unsure of how to  get the values back out .  Let me know what test bed you need and I can set it up.  Could you provide a few basic examples of posting a form to a db and the. When I load the form again the Kate's values are returned back to the same radio buttons.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40374313
Here is the CREATE TABLE script.
<?php // demo_temp_augustkrys.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Database/MySQL/Q_28535559.html

// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// THE DATA SET IS UTF-8
$mysqli->set_charset("utf8mb4");
mb_internal_encoding("UTF-8");

// CREATING A TABLE FOR OUR TEST DATA
$sql
=
"
CREATE TABLE `temp_augustkrys_radios`
( `id`                 INT           NOT NULL AUTO_INCREMENT PRIMARY KEY
, `happy`              CHAR(1)       NOT NULL DEFAULT 'Y'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"
;
// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}

// START WITH "happy = 'Y' STATUS
$sql = "INSERT INTO temp_augustkrys_radios (happy) VALUES ('Y')";
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}

Open in new window

And here is the script that uses the database table to pre-select the state of happiness.

<?php // demo/temp_augustkrys.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Database/MySQL/Q_28535559.html

// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";
require_once('RAY_live_data.php');
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// THE DATA SET IS UTF-8
$mysqli->set_charset("utf8mb4");
mb_internal_encoding("UTF-8");

// RECOVER THE CURRENT STATE OF THE 'happy' RADIO
$sql = "SELECT happy FROM temp_augustkrys_radios WHERE id=1 LIMIT 1";
$res = $mysqli->query($sql);

// IF mysqli_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESULT OBJECT IN $res
$row = $res->fetch_object();
$sig = $row->happy;

// IF THERE IS A POST-METHOD REQUEST, SAVE THE STATUS OF THE RADIO
if (!empty($_POST['happy']))
{
    $sig = trim($_POST['happy']);
    $sig = substr($sig,0,1);
    $sig = strtoupper($sig);
    $sql = "UPDATE temp_augustkrys_radios SET happy = '$sig' WHERE id=1 LIMIT 1";
    $res = $mysqli->query($sql);
    if (!$res)
    {
        $err
        = 'QUERY FAILURE:'
        . ' ERRNO: '
        . $mysqli->errno
        . ' ERROR: '
        . $mysqli->error
        . ' QUERY: '
        . $sql
        ;
        trigger_error($err, E_USER_ERROR);
    }
}

// GENERATE THE RADIO BUTTONS
$radio_buttons = NULL;
if ($sig == 'Y')
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y" checked />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N"         />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X"         />No Answer '   . PHP_EOL;
}
elseif ($sig == 'N')
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y"         />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N" checked />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X"         />No Answer '   . PHP_EOL;
}
else
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y"         />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N"         />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X" checked />No Answer '   . PHP_EOL;
}

// CREATE A FORM USING THE RADIO BUTTONS
$form = <<<EOD
<form method="post">
<b>Happy?</b> $radio_buttons <br>
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

You could have other columns in the table beside "happy" and you could make these correspond to other radio buttons in the form - just follow the same design pattern.  You might want to consider packaging some parts of this as a class or function, just to make the PHP code cleaner.  You might also want to consider using jQuery to make an AJAX request when the radio button is changed - that would make for a more "real-time" experience for the user community.
0
 

Author Comment

by:augustkrys
ID: 40374364
I have have the data  from my for going into the new table i created in MyPHPadmin.
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost:3306';
$dbuser = xxx';
$dbpass = 'xxx1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $radio1 = addslashes ($_POST['radio1']);
   
}
else
{
   $radio1 = $_POST['radio1'];
  
}


$sql = "INSERT INTO xxx".
       "(radio1) ".
       "VALUES('$radio1')";
mysql_select_db('xxx'<wbr ></wbr>);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<p>
                    <input id="radio3" class="radio" checked="checked" name="radio1" type="radio" value="on_tap" />
                    <label for="radio3">On Tap Now</label>
                </p>
                <p class="fl">
                    <input id="radio4" class="radio" name="radio1" type="radio" value="off_tap"/>
                    <label for="radio4">Back Soon!</label>
                </p>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>
<?php
}
?>

Open in new window

I see your code to recall it - but where does that go? so when the page loads it corrects the form with the previous values.
It also looks like you have static entries like id=1. as this is auto increment how do you match up  the right one?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40374382
The table has one row in my example with only one column.  If you have multiple radio buttons, you might have multiple columns and therefore you would probably have different WHERE clauses.

If you're new to PHP and MySQL and want to get a good foundation, this article may have some helpful learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:augustkrys
ID: 40374388
got it - That was just  your example for that row- I am still a bit confused on where the next snippet you osted goes though. Do i just include it on page loadso the form is correct?

I have it setup like-

form.php calls demo.php in form action of course to save to the DB
how to i call the other say recall.php ?
Does that make sense
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375066
Here is the script reorganized a bit with comments to show a bit more about what we're doing.  This is a complete script, handling both the form creation and the processing of POST requests to update the database.

<?php // demo/temp_augustkrys.php
error_reporting(E_ALL);


// SEE: http://www.experts-exchange.com/Database/MySQL/Q_28535559.html


/****************** THIS PART OF THE SCRIPT IS COMMON TO BOTH ******************/
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// THE DATA SET IS UTF-8
$mysqli->set_charset("utf8mb4");
mb_internal_encoding("UTF-8");


/****************** THIS PART OF THE SCRIPT IS THE ACTION SCRIPT ******************/
// IF THERE IS A POST-METHOD REQUEST, SAVE THE STATUS OF THE RADIO
if (!empty($_POST['happy']))
{
    $sig = trim($_POST['happy']);
    $sig = substr($sig,0,1);
    $sig = strtoupper($sig);
    $sql = "UPDATE temp_augustkrys_radios SET happy = '$sig' WHERE id=1 LIMIT 1";
    $res = $mysqli->query($sql);
    if (!$res)
    {
        $err
        = 'QUERY FAILURE:'
        . ' ERRNO: '
        . $mysqli->errno
        . ' ERROR: '
        . $mysqli->error
        . ' QUERY: '
        . $sql
        ;
        trigger_error($err, E_USER_ERROR);
    }
    // AT THIS POINT, THE ACTION SCRIPT IS COMPLETE.  SAY THANK YOU AND END THE SCRIPT
}


/****************** THIS PART OF THE SCRIPT IS THE FORM SCRIPT ******************/
// RECOVER THE CURRENT STATE OF THE 'happy' RADIO
$sql = "SELECT happy FROM temp_augustkrys_radios WHERE id=1 LIMIT 1";
$res = $mysqli->query($sql);

// IF mysqli_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESULT OBJECT IN $res
$row = $res->fetch_object();
$sig = $row->happy;


// GENERATE THE RADIO BUTTONS
$radio_buttons = NULL;
if ($sig == 'Y')
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y" checked />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N"         />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X"         />No Answer '   . PHP_EOL;
}
elseif ($sig == 'N')
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y"         />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N" checked />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X"         />No Answer '   . PHP_EOL;
}
else
{
    $radio_buttons .= '<input type="radio" name="happy" value="Y"         />Yes, happy '  . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="N"         />No, unhappy ' . PHP_EOL;
    $radio_buttons .= '<input type="radio" name="happy" value="X" checked />No Answer '   . PHP_EOL;
}


// CREATE A FORM USING THE RADIO BUTTONS
$form = <<<EOD
<form method="post">
<b>Happy?</b> $radio_buttons <br>
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:augustkrys
ID: 40375191
Okay so all the code goes into the action script .  I notice that there is no close tag for the php. Is that not required ? I am not able to get your scripts to run or connect to my db - but the one i posted works - is that accurate ? - ths for the extra effort .
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 40375233
I would recommend that you always avoid the close-PHP tag.  There are many reasons to avoid it.  Please see this article for some other things that we want to avoid in PHP programming.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html
not able to get your scripts to run or connect to my db
It would be helpful to see your implementation.  We need to see the URL, the code, the error messages, etc., to discern what might be wrong.
0
 

Author Comment

by:augustkrys
ID: 40375236
Of course - I send you the info for the real site .
0
 

Author Comment

by:augustkrys
ID: 40375238
What's  your email ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375241
Please see my public profile here.  I'll be glad to take a look.
http://www.experts-exchange.com/members/Ray_Paseur.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375650
Here is a slightly different approach to the design.  First the CREATE TABLE statement, where we create one row for each radio button.

<?php // demo_temp_augustkrys.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Database/MySQL/Q_28535559.html

// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// THE DATA SET IS UTF-8
$mysqli->set_charset("utf8mb4");
mb_internal_encoding("UTF-8");

// CREATING A TABLE FOR OUR TEST DATA
$sql = "DROP TABLE `temp_augustkrys_radios`";
!$res = $mysqli->query($sql);

$sql
=
"
CREATE TABLE `temp_augustkrys_radios`
( `id`                 INT           NOT NULL AUTO_INCREMENT PRIMARY KEY
, `name`               VARCHAR(16)   NOT NULL DEFAULT ''
, `status`             VARCHAR(16)   NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"
;
// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}

// START WITH INITIAL STATES FOR DIFFERENT BEERS
$sql = "INSERT INTO temp_augustkrys_radios (name, status) VALUES ( 'Budweiser', 'On' )";
$res = $mysqli->query($sql);
$sql = "INSERT INTO temp_augustkrys_radios (name, status) VALUES ( 'Miller', 'On' )";
$res = $mysqli->query($sql);
$sql = "INSERT INTO temp_augustkrys_radios (name, status) VALUES ( 'Coors', 'On' )";
$res = $mysqli->query($sql);

// CHECK THE INITIAL STATUS
$sql = "SELECT name, status FROM temp_augustkrys_radios ORDER BY name";
$res = $mysqli->query($sql);
while ($row = $res->fetch_object())
{
    print_r($row);
}

Open in new window

Next the form and action scripts.  I've tried to pare this down to the essentials for the demonstration script.  In real life you would want error checking, data filtering, etc.
http://iconoun.com/demo/temp_augustkrys.php

<?php // demo/temp_augustkrys.php
error_reporting(E_ALL);


// SEE: http://www.experts-exchange.com/Database/MySQL/Q_28535559.html


/****************** THIS PART OF THE SCRIPT IS COMMON TO BOTH ******************/
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// THE DATA SET IS UTF-8
$mysqli->set_charset("utf8mb4");
mb_internal_encoding("UTF-8");

// ACQUIRE THE NAMES AND CURRENT STATE OF THE RADIO BUTTONS
$rad = array();
$sql = "SELECT name, status FROM temp_augustkrys_radios ORDER BY name";
$res = $mysqli->query($sql);
while ($row = $res->fetch_object())
{
    $radios[$row->name] = $row->status;
}


/****************** THIS PART OF THE SCRIPT IS THE ACTION SCRIPT ******************/
// IF THERE IS A POST-METHOD REQUEST, SAVE THE STATUS OF THE RADIO BUTTONS
if (!empty($_POST))
{
    // NOTE: WE USE OUR INTERNAL DATA MODEL TO GUIDE EXAMINATION OF THE EXTERNAL INPUT
    foreach ($radios as $name => $status)
    {
        $status = $mysqli->real_escape_string($_POST[$name]);
        $sql = "UPDATE temp_augustkrys_radios SET status = '$status' WHERE name = '$name' LIMIT 1";
        $res = $mysqli->query($sql);
    }
}


/****************** THIS PART OF THE SCRIPT IS THE FORM SCRIPT ******************/
// RECOVER THE CURRENT STATE OF THE RADIO BUTTONS
$sql = "SELECT name, status FROM temp_augustkrys_radios ORDER BY name";
$res = $mysqli->query($sql);

$radio_buttons = array();
while ($row = $res->fetch_object())
{
    make_radio_button($row, $radio_buttons);
}

// ACQUIRE THE AGGREGATED RADIO BUTTONS
$radio_buttons_html = make_radio_button(NULL, $radio_buttons);

// CREATE A FORM USING THE RADIO BUTTONS
$form = <<<EOD
<form method="post">
$radio_buttons_html <br>
<input type="submit" />
</form>
EOD;

echo $form;


// A FUNCTION TO GENERATE RADIO BUTTONS
function make_radio_button($row, &$radio_buttons)
{
    // RETURN ALL OF THE CURRENT RADIO BUTTONS
    if (empty($row))
    {
        return implode(PHP_EOL, $radio_buttons);
    }

    // GENERATE ONE RADIO BUTTON
    $rb = '<br><span style="display:inline-block;width:6em;"><b>' . $row->name . '</b></span>';
    if ($row->status == 'On')
    {
        $rb .= '<input type="radio" name="' . $row->name . '" value="On"  checked />On '  . PHP_EOL;
        $rb .= '<input type="radio" name="' . $row->name . '" value="Off"         />Off ' . PHP_EOL;
    }
    elseif ($row->status == 'Off')
    {
        $rb .= '<input type="radio" name="' . $row->name . '" value="On"          />On '  . PHP_EOL;
        $rb .= '<input type="radio" name="' . $row->name . '" value="Off" checked />Off ' . PHP_EOL;
    }
    else // UNKNOWN CONDITION OCCURRED
    {
        $rb .= '<input type="radio" name="' . $row->name . '" value="On"          />On '  . PHP_EOL;
        $rb .= '<input type="radio" name="' . $row->name . '" value="Off"         />Off ' . PHP_EOL;
        $rb .= '<input type="radio" name="' . $row->name . '" value="X"   checked />? '   . PHP_EOL;
    }

    // APPEND TO THE ARRAY
    $radio_buttons[] = $rb;
}

Open in new window

0
 

Author Comment

by:augustkrys
ID: 40392862
Ray I setup your scenario and  I just get a blank page- I did add  an if to check the connection and do get successful connection.  not sure what is different - I am also unable to use
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

Open in new window

I have to use
$mysqli = mysql_connect($dbhost, $dbuser, $dbpass);

Open in new window

to connect
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40392999
I think at this point I'll have to step away and recommend that you hire a professional programmer to help you with this.  You need someone who can get "hands on" with the code and data, and it's just not feasible to do that over a forum like this one.  I've given you a working example showing the design pattern, but I can't install that on your server, so we've kind of come to the end of what I can do for you.  The demonstration script is on my server here; this is the last code snippet that I posted.
http://iconoun.com/demo/temp_augustkrys.php

Best of luck with your project, ~Ray
0
 

Author Comment

by:augustkrys
ID: 40393444
Ray,

Thanks for sticking it out! I spent alot of time reading the suggested materials. As you said  don't use snippets from across the net. I wrote the code from scratch and took out all the fluff.  I took the time to actually research your examples and what you are actually doing.  What helped the most was your comments so I could understand how to think through the workflow.
There is no need for a pro - This is just a project for learning. Thanks to you  and pushing me to read
0
 

Author Closing Comment

by:augustkrys
ID: 40393446
ALso There is a ton of great info on your page that I read through that really helped. I look forward to your teachings again
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40393458
Thanks for your kind words, and thanks for using E-E, ~Ray
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now