PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Oct 15, 2018 1:23 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Nov 05, 2003 6:03 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 08, 2003 6:53 pm
Posts: 2101
Location: Sweden
Recently, forum users has asked about making checkboxes/radio/select elements in forms automaticly selected after the form is submited.
So I thought it would be in order to post a brief demonstration to help guiding those in the future.

Note: It can be written shorter, but I tried aiming at understanding it rather than saving bytes.

Syntax: [ Download ] [ Hide ]
<pre> <!-- I use the pre tag to make it easy to read -->

<?php

    if (!empty($_POST['foo'])) {    // check if the $_POST['foo'] was sendt.

        print_r($_POST);            // display debugging information.

        $variable = $_POST['foo'];  // add the sendt value to a variable

    } else {

        $variable = '';             // ...but mark it blank if it was not sendt at all.

    }

?>

<form method="post">

<input type="radio" name="foo" value="1" <?php echo ($variable == 1 ? 'CHECKED ' : ''); ?>/>1

<input type="radio" name="foo" value="2" <?php echo ($variable == 2 ? 'CHECKED ' : ''); ?>/>2

<input type="radio" name="foo" value="bar" <?php echo ($variable == 'bar' ? 'CHECKED ' : ''); ?>/>bar

<input type="submit" />

</form>

Result:
Image

Hope I gave answers to some questions.

Note: Im using alternative if-then-else syntax above, described here: http://forums.devnetwork.net/viewtopic.php?t=14331


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group