Jump to content

Pass values array into form


rschaft

Recommended Posts

Hi,

I'm trying to pass values from an array into a form. As a newbie, I've came up with the code underneath, but I want to pinpoint all the first values toward 1 selection list in the form. Any suggestions how to accomplish this?

Thanks in advance.

 

Ruud

<html>

<head>

<title>Test formulier</title>

</head>

<body>

<h2>Test formulier</h2>

<form action="test.php" method="post">

 

<p>

<?php

$prijzen = array(   

array("01", "125","150", "A" => true,"B" => true),

array("02", "125","150", "A" => true,"B" => true),

array("03", "125","150", "A" => true,"B" => false),

array("04", "125","150", "A" => true,"B" => true)

);

?>

 

</P>

<select name="prijzen[]" id="prijzen" multiple="multiple" size="7" class="none">

 

<?php

while(list($key, $val) = each($prijzen)){

  while(list($key2, $val2) = each($val)){

    echo '<option value="'.$key2.'">'.$val2.'</option>'.PHP_EOL;

 

  }

}

                    ?>

 

</select>

</body>

</html>

 

Link to comment
Share on other sites

array("01", "125","150", "A" => true,"B" => true),

 

This is an associative array. It actually is the same as:

 

array(0=>"01",1=> "125",2=>"150", "A" => true,"B" => true),

 

The keys are numeric for first 3 elements and A and B for 4th and 5th element respectively.

 

do you really want to do this?

 

You code will consider the keys 1,2,3,A,B as drop down values.

 

Also what is the result you are getting now? and what is the result you are expecting?

 

Link to comment
Share on other sites

With what you just put, you are asking to print a mix of your array's keys and its values.

 

What ever list you want to print into your should either be keys or it should be values of an array. Mixing them is not good or make printing them difficult.

 

What do the "A" and "B" need to be in your selection element? Does the 'true' value need to be passed on once the form is submitted and A or B is selected?

 

if you are trying to print this list: 01, 125, 150, A, B into your selection then your array should just look similar to this:

 

<?php
$list1 = array("01","125","150","A","B");
echo "<select name='firstSelector'>".PHP_EOL;
foreach ($list1 as $key=>$value) {
       echo '<option value="'.$key."'>'.$value.'</option>'.PHP_EOL;
}
echo "</select>".PHP_EOL;

But really if you need specific values passed with each option when the form is submitted then you can make it an associative array where the array keys get printed into the options value like so:

<?php
$list1 = array("prijzen0"=>"01","prijzen1"=>"125","prijzen2"=>"150","prijzenTrue1"=>"A","prijzenTrue2"=>"B");
echo "<select name='firstSelector'>".PHP_EOL;
foreach ($list1 as $key=>$value) {
       echo '<option value="'.$key.'">'.$value.'</option>'.PHP_EOL;
}
echo "</select>".PHP_EOL;
?>

The above would print this:

<select name='firstSelector'>
<option value="prijzen0">01</option>
<option value="prijzen1">125</option>
<option value="prijzen2">150</option>
<option value="prijzenTrue1">A</option>
<option value="prijzenTrue2">B</option>
</select>

let me know if that helps or not

 

thanks for sharing :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.