Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 7553 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

Hi,

I am taking my first steps towards learning fx.php to connect to my databases. I can do the simple stuff like show a list of records and move from a list to a details page for a record. Now I am trying to put together an edit form with dropdownlists and radiobuttons. I am stuck here.

This is what I tried:

Drop down:

<?

$TypeList = '<select name="Type">';

foreach ($ReturnedData['data'] as $key => $value){

$TypeList .= '<option selected>'.$value['Type'][0].'</option>';

}

$TypeList .= '<option>&lt;----------------------------&gt;</option>';

foreach ($ReturnedValues['valueLists']['Type'] as $value) {

$TypeList .= '<option>'.$value.'</option>

';

}

$TypeList .= '</select>

';

echo $TypeList;

?>

This does display my dropdown, but the only way I found to show the actual data is to put it on top of the list in an extra line. Although not elegant this could work for a dropdown box. For a radio button I tried:

<?

$StatusList = '';

$Checked ='';

foreach ($ReturnedValues['valueLists']['Status'] as $value) {

foreach ($ReturnedData['data'] as $key1 => $value1){

if ($value1['Status'][0] = $value)

{

$Checked='checked';

}}

$StatusList .= '<INPUT TYPE="radio" NAME="Status" VALUE="'.$value.'" '.$Checked.'>'.$value;

}

echo $StatusList;

?>

Wich, needless to say, does not work. It will show the radiobutton but it will mark all values as checked.

There must be a better (more simple) way to do this.

Any help would be appriciated.

Wouter

Posted

On line 4 of your first snippet, you have:

$TypeList .= '<option selected>'.$value['Type'][0].'</option>';

This will assign set every single option as selected. If your HTML select is set to allow multiple selections, every option will be highlighted. If your HTML select does not allow multiple selections, only the last option in the list will be selected.

The problem here is that you need to programmatically determine, on an option-by-option basis, which option(s) should be selected.

Dynamically generating a select list can be a bit tedious. It can be helpful to encapsulate this bit of code into a function which you call each time you need to draw a select. Encapsulating your code into a function also helps reduce bugs. Here is an example:


<?

// Create an array of values to use for our select list

$a = array(

                "1" => "ASP",

                "2" => "Lasso",

                "3" => "Perl",

                "4" => "PHP"

            );



// Create an array of attributes for the HTML "select" element

$options = array(

                    "size=5",

                    "multiple",

                );

 

// Draw the HTML select element

print selectList($a, array("2", "3"), $options);

 

 

// THIS IS THE FUNCTION THAT DOES ALL THE WORK

//

function selectList ($list, $selection, $attribute) {

    $out = "<select";

    foreach ($attribute as $a) {

        $out .= " $a";

    }

    $out .= ">n";

    

    foreach ($list as $key => $value) {

        $out .= "<option value=$key";

        if (in_array($key, $selection))

            $out .= " selected";

        $out .= ">".$value."</option>n";

    }

    

    $out .= "</select>";

    return $out;

}

?>





The radio buttons work on the same principle.  The problem with your radio button code is that on line 8 you set the $checked variable - but you never reset it to blank.  As soon as your code encounters the first checked item, every item thereafter will be checked as well.  Here is an example that dynamically generates a set of radio buttons:





<?

// Create an array of values to use for our select list

$a = array(

            "1" => "ASP",

            "2" => "Lasso",

            "3" => "Perl",

            "4" => "PHP"

		);



// Draw the HTML "input type=radio" elements

print radio($a, "2", "test");





// THIS IS THE FUNCTION THAT DOES ALL THE WORK

//

function radio ($list, $selection, $name) {

    $out = "";

    foreach ($list as $key => $value) {

        $out .= "<input type=radio name=$name value=$key";

        if ($key == $selection)

            $out .= " checked";

        $out .= ">$value<br>n";

    }

    return $out;

}

?>

By the way, in PHP (and most languages), it is customary to begin your variable names with lower case letters. Upper case letters are used for class names -- that's why FX starts with caps, but $query starts with a lower case letter. This is not a rule by any means, just a fairly standard convention.

Congrats on getting started with PHP!

Posted

Thanks Mario!

This works great. I am sure I will need more help along the way, but with help like this I am sure I will be up and running in no time.

Wouter

P.S. I already have another question. How do you handle username/password submissions. Do I use $_SESSION to store the variables or is there a better way.

Version: Developer v6

Platform: Windows XP

Posted

Well, you can have an HTML form that submits the username and password to a PHP script. Your PHP script can then take that information and check if the user should be granted access or not (probably by querying the database). Once the user is granted access, you can store something to that effect in the $_SESSION.

At the beginning of each protected page, I'd check the $_SESSION to see if the user is logged in. If not, switch them to a login screen. If the user is already logged in, then continue with the page.

This topic is 7553 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.