Jump to content

[HELP]Making dropdown Selection Sticky with arry in a loop


jim4nz

Recommended Posts

Hi there i'm trying to make my drop down menu to be sticky, but it doesnt work...can some one please help me out ?? trying to figure it out for ages already. i'm building the form through a class

######Setting up the month Array ######
$monthArray = array("January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"); 

##### Build The Form by calling the class #####
$form = new makerClass();
$form->openForm('step3Form', '');

//This Drop down Here is for user to select the number of INPUTFIELD that want to insert
$form->dropDown('', 'Insert Number of Job Input Field', 'insert', 'this.form.submit()',$insertArray, 'onchange');
$number_of_fields = $_POST['insert'];
for ($i = 0; $i <= $number_of_fields; $i++)
{
$form->dropDown('Select Month', 'From','company['.$i.'][monthStart]', "checkedDefault(this.value, 'company[$i][monthStart]', '')", $monthArray);
}

$form->makeButton('step2','Next Step');
$form->closeForm();

##### Presentation #####
##### echoing out the Form #####
echo $form->wholeForm;

 

The Following Are The Class that makes the Form

 

//Function Opening a form tag
function openForm($formName, $enc_type, $formAction="SCRIPT_NAME"){
    $this->wholeForm = '<form enctype="' . $enc_type . '" name= "'.$formName.'" action="'.$_SERVER[''.$formAction.''].'" method="post">'."\n";
    
}

//Function Building Drop Down Menu
function dropDown($defaultMessage, $label, $fieldname, $jsvalid, $locationArray, $jsEvent='onblur'){
    $selected ="";
    $selected .= '<label for = "'.$fieldname.'">'.$label.'</label>'."\n";
    $selected .= '<select name="'.$fieldname.'" id="'.$fieldname.'" '.$jsEvent.'="'.$jsvalid.'">'."\n";

    $selected .= '<option value="default">'.$defaultMessage.'</option>'."\n";
    foreach($locationArray as $key=>$value){
            $selected .= '<option value="'.$key.'">'.$value.'</option>'."\n";
            var_dump($_POST[$fieldname]);
        }
    $selected .= '</select>'."\n";
    $selected .= '<span id="'.$fieldname.'_msg" class="col3">';
    $selected .= $this->message[$fieldname];
    $selected .= '</span>'."\n";
    $this->wholeForm .= $selected;
}

//Function Closing Form Tag
function closeForm(){
    $this->wholeForm .= '</form>';
}

//Function Makes Button
function makeButton($fieldname, $fieldvalue, $type="submit"){
        
        $this->wholeForm .= '<input type = "'.$type.'" name="'.$fieldname.'" value="'.$fieldvalue.'" class="submitButton" />';
        
    }

 

 

 

Link to comment
Share on other sites

Additional info

##### For $insertArray, The Input Field ##### 
$insertArray = array();
for($i = 1; $i <= 10; $i++) {
    
    $insertArray[] = $i;
}

 

by the way i ve Found a soultion to make it sticky, How Ever it doesnt workign for the Drop Down of the $monthArray

 

##### In the Form Making Class ##### 
function dropDown($defaultMessage, $label, $fieldname, $jsvalid, $locationArray, $jsEvent='onblur'){
    $selected ="";
    $selected .= '<label for = "'.$fieldname.'">'.$label.'</label>'."\n";
    $selected .= '<select name="'.$fieldname.'" id="'.$fieldname.'" '.$jsEvent.'="'.$jsvalid.'">'."\n";

    $selected .= '<option value="default">'.$defaultMessage.'</option>'."\n";
    foreach($locationArray as $key=>$value){
            $selected .= '<option value="'.$key.'"'.(($_POST[$fieldname]==$key)?' selected="selected"':'').'>'.$value.'</option>'."\n";
            var_dump($_POST[$fieldname]);
        }
    $selected .= '</select>'."\n";
    $selected .= '<span id="'.$fieldname.'_msg" class="col3">';
    $selected .= $this->message[$fieldname];
    $selected .= '</span>'."\n";
    $this->wholeForm .= $selected;
}

Link to comment
Share on other sites

Here is a function from CodeIgniter's helpers.

 

function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
{
	if ( ! is_array($selected))
	{
		$selected = array($selected);
	}

	// If no selected state was submitted we will attempt to set it automatically
	if (count($selected) === 0)
	{
		// If the form name appears in the $_POST array we have a winner!
		if (isset($_POST[$name]))
		{
			$selected = array($_POST[$name]);
		}
	}

	if ($extra != '') $extra = ' '.$extra;

	$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

	$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

	foreach ($options as $key => $val)
	{
		$key = (string) $key;
		$val = (string) $val;

		$sel = (in_array($key, $selected))?' selected="selected"':'';

		$form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
	}

	$form .= '</select>';

	return $form;
}

 

To call it you'd do something like the following;

 

 


// example of submit
if (isset($_POST['submit']))
{
$default = $_POST['month'];
}

$options = array(
		'january' => 'January',
		'february' => 'February',
		'march' => 'March',
		'april' => 'April',
		'may' => 'May',
		'june' => 'June',
		'july' => 'July',
		'august' => 'August',
		'september' => 'September',
		'october' => 'October',
		'november' => 'November',
		'december' => 'December'
	);

form_dropdown('month', $options, $default);

 

This is going to produce (assuming form is submitted with the value as "march" and comes back);

 

<select name="month">
<option value="january">January</option>
<option value="february">February</option>
<option value="march" selected="selected">March</option>
// etc
</select>

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.