Welcome Guest, Not a member yet? Register   Sign In
How do I repopulate fields BEFORE validation is passed?
#1

[eluser]RalphLeMouf[/eluser]
The fields I am making required to fill out, should repopulate the correctly filled out fields, while NOT submitting the form and posing errors for the incorrectly filled out fields. What is the best way to do that?

Please note that with this code, I am repopulating the fields as they should be upon submitting the form correctly and they are all displaying when page is reloaded.

Code:
<div class="dropdown_structure">
    &lt;?php
  
     if($user['location'] == "empty")
     {
      echo country_dropdown('location');
      
     }
     else
     {
      echo country_dropdown('location',$user['location']);
        
     }
  
    ?&gt;
   </div>

Also please note that I've tried inserting the value in the input fields.

Code:
$data = array( 'name' => 'location', 'value' => $this->input->post('location'));
echo relation_dropdown('location');

thanks in advance
#2

[eluser]Aken[/eluser]
Huh? How are you going to know what errors there are if the form is not submitted? How are you going to know what was submitted at ALL without submitting the form?
#3

[eluser]RalphLeMouf[/eluser]
That's what I'm saying.

1. someone types in their info and get's SOME fields correct and some not correct.

2. they hit submit but the incorrect fields cause the form validation to work, so the edit page is reloaded.

3. as of now - the correct fields I filled out in the failed submission are returning to default, so I was wondering if there was a way to pre-populate those in this scenario.
#4

[eluser]RalphLeMouf[/eluser]
I guess I'm wanting to get them saved when the submit button ( page is reloaded ) is hit but FAILED form validation.
#5

[eluser]RalphLeMouf[/eluser]
I just realized that is actually is repopulating the one text input field I am using ( without it inserting it in the database yet because the form did not get submitted due to validation failure ). The rest are dropdowns and are not prepopulating.
#6

[eluser]Aken[/eluser]
I think you're confusing people by the way you're using the word "submit". When a user fills out your form and hits the submit button, the page WILL refresh (unless you are using some form of Javascript ajax stuff). Your form validation code will then check the submitted POST data and see if it is valid based on the rules you supply. If you lay out your code properly (there's an example on the form validation user guide page), you should have separate chunks of code for if the submission passes or if it fails.

It sounds like your form is working properly, your drop downs just aren't repopulating properly. Since you appear to be using your own form helpers, I can't comment exactly on why that might be. But if you view the form helper user guide page, you'll see functions dedicated to repopulating form items.
#7

[eluser]RalphLeMouf[/eluser]
I think my list I posted up above is pretty straight forward and not confusing. All of thing's you are telling me to do I have already done. Let me refine my list to make it a little more detailed so there is no confusion and then I'll post some code.

1. someone types in their info and get’s 5 fields to fill at that are all required fields with set form_validation rules.

Code:
// CONTROLLER

if($this->form_validation->run() == FALSE)
   {
    
    $data['main_content'] = 'account/edit';
    $this->load->view('includes/templates/main_page_template', $data);
    
   }
   elseif($this->form_validation->run() == TRUE)
   {
    $birthdate = $this->input->post('year') . "-" . $this->input->post('month') . "-" . $this->input->post('day');
    $save_data = array(
        'relation' => $this->input->post('relation'),
     'location' => $this->input->post('location'),
     'birthday' => date('Y-m-d',strtotime($birthdate)),
     'gender' => $this->input->post('gender'),
     'mylife' => $this->input->post('mylife'),
     'occupation' => $this->input->post('occupation'),
     'hobbies' => $this->input->post('hobbies'),
     'books' => $this->input->post('books'),
     'music' => $this->input->post('music'),
     'movies' => $this->input->post('movies'),
     'quote' => $this->input->post('quote'),
     'mycysticlife' => $this->input->post('mycysticlife'),
     'medicines' => $this->input->post('medicines'),
     'treatments' => $this->input->post('treatments'),
     'PFTs:FVC' => $this->input->post('PFTs:FVC'),
     'FEV1' => $this->input->post('FEV1'),
     'mutation' => $this->input->post('mutation'),
     'twitter' => $this->input->post('twitter'),
     'myspace' => $this->input->post('myspace'),
     'facebook' => $this->input->post('facebook'),
     'blogspot' => $this->input->post('blogspot'),
     'youtube' => $this->input->post('youtube'),
     'url' => $this->input->post('url'),
     'city' => $this->input->post('city'),
            );
  
    
    $query = $this->account_model->update_edit($user['id'],$save_data);
    $data['main_content'] = 'account/welcome';
    $this->load->view('includes/templates/main_page_template', $data);
   }
  
  }
}

// VIEW

See what I originally posted for the field data in the form on the view ( one field for example out of five required.

// FORM HELPER

function relation_dropdown($relation="relation", $top_relations=array()) {
$relations = array(
   ""=>"Choose One",
   "Fibro"=>"I am a Fibro",
   "Cyster"=>"I am a Cyster",
   "Aunt"=>"Aunt",
   "Brother"=>"Brother",
   "Caregiver"=>"Caregiver",
   "Child"=>"Child",
   "Child Life Specialist"=>"Child Life Specialist",
   "Cousin"=>"Cousin",
   "Doctor"=>"Doctor",
   "Dad"=>"Dad",
   "Friend"=>"Friend",
   "Grandma"=>"Grandma",
   "Grandpa"=>"Grandpa",
   "Guardian"=>"Guardian",
   "Husband"=>"Husband",
   "Mom"=>"Mom",
   "Nephew"=>"Nephew",
   "Niece"=>"Niece",
   "Nurse"=>"Nurse",
   "Nutritionist"=>"Nutritionist",
   "Partner"=>"Partner",
   "Respiratory Therapist"=>"Respiratory Therapist",
   "Sister"=>"Sister",
   "Social Worker"=>"Social Worker",
   "Uncle"=>"Uncle",
   "Wife"=>"Wife"
);

$html = "<select name='{$relation}'>";

   foreach($relations as $key => $relation){
      $selected = "";
      if( $key == $top_relations ) {
          $selected = "selected='selected'";
      }

      $html .="<option value='{$key}' $selected >{$relation}</option>";
   }
   $html .="</select>";
   return $html;

}

2. In this scenario I'm trying to solve: The user fills 4 out of the 5 required fields correctly and one is incorrect. As you and I have both already stated in this post more than once...the page reloads because the submit button ( form ) was set and the above form validation and if statements stopped the form and reloads the edit view

So as it stands now when that happens, the fields that were CORRECT are not pre-populating, In the case that it's a new user filling out their information for the first time ever after joining the site. Those columns for those five fields in the database are going to be empty so there is going to be nothing to return before the form is submitted for the first time.

My question is how can I repopulate them before the form is validated and fails form validation and the page is reloaded to give them another try.

thanks
#8

[eluser]Unknown[/eluser]
I think this could be done as

// before validation
Code:
$location=$_POST['location'];
if(empty($location)){
$location='default location';
$_POST['location']=$location;
}
// In short form
if(isset($_POST) && empty($_POST['location'])){
$_POST['location']='default';
}
#9

[eluser]CroNiX[/eluser]
when you say "prepopulating", I believe you really mean "repopulating". If the form fails validation, you just want to repopulate the fields with the values that were submitted, correct? If so, have you read the docs for the form validation class? It explicitly, even with examples, shows how to do that, but I don't see where you are using it in the inputs of your form anywhere.

I'd thoroughly read the form_helper docs along with the form_validation. The answers are in there.



#10

[eluser]RalphLeMouf[/eluser]
Thanks guy's that helps. I have been able to get all of these to work correctly except my birthday drop down. I can't quite find the correct way to repopulate those. Here is my code.

// FORM HELPER

Code:
function month_dropdown($month="month", $top_month='' ) {

   $months = array(
        "choose"=>"Month",
        "Jan"=>"Jan",
        "Feb"=>"Feb",
        "Mar"=>"Mar",
        "Apr"=>"Apr",
        "May"=>"May",
        "Jun"=>"Jun",
        "Jul"=>"Jul",
        "Aug"=>"Aug",
        "Sep"=>"Sep",
        "Oct"=>"Oct",
        "Nov"=>"Nov",
        "Dec"=>"Dec"
  );

  $html = "<select name='{$month}'>";

  foreach($months as $key => $month){

      $selected = "";
      //this will match for selected value and set the selected attribute
      if( $key == $top_month ) {
          $selected = "selected='selected'";
      }
      $html .="<option value='{$key}' $selected>{$month}</option>";
  }
  $html .="</select>";
  return $html;

}

function day_dropdown($day="day", $top_days="") {
   $days = array(
        "choose"=>"Day",
        "01"=>"01",
        "02"=>"02",
        "03"=>"03",
        "04"=>"04",
        "05"=>"05",
        "06"=>"06",
        "07"=>"07",
        "08"=>"08",
        "09"=>"09",
        "10"=>"10",
        "11"=>"11",
        "12"=>"12",
        "13"=>"13",
        "14"=>"14",
        "15"=>"15",
        "16"=>"16",
        "17"=>"17",
        "18"=>"18",
        "19"=>"19",
        "20"=>"20",
        "21"=>"21",
        "22"=>"22",
        "23"=>"23",
        "24"=>"24",
        "25"=>"25",
        "26"=>"26",
        "27"=>"27",
        "28"=>"28",
        "29"=>"29",
        "30"=>"30",
        "31"=>"31"
   );

   $html = "<select name='{$day}'>";

   foreach($days as $key => $day){
      $selected = "";
      if( $key == $top_days ) {
          $selected = "selected='selected'";
      }

      $html .="<option value='{$key}' $selected >{$day}</option>";
   }
   $html .="</select>";
   return $html;

}

function year_dropdown($year="year", $top_years='') {
   $years = array(
        "choose"=>"Year",
        "1997"=>"1997",
        "1996"=>"1996",
        "1995"=>"1995",
        "1994"=>"1994",
        "1993"=>"1993",
        "1992"=>"1992",
        "1991"=>"1991",
        "1990"=>"1990",
        "1989"=>"1989",
        "1988"=>"1988",
        "1987"=>"1987",
        "1986"=>"1986",
        "1985"=>"1985",
        "1984"=>"1984",
        "1983"=>"1983",
        "1982"=>"1982",
        "1981"=>"1981",
        "1980"=>"1980",
        "1979"=>"1979",
        "1978"=>"1978",
        "1977"=>"1977",
        "1976"=>"1976",
        "1975"=>"1975",
        "1974"=>"1974",
        "1973"=>"1973",
        "1972"=>"1972",
        "1971"=>"1971",
        "1970"=>"1970",
        "1969"=>"1969",
        "1968"=>"1968",
        "1967"=>"1967",
        "1966"=>"1966",
        "1965"=>"1965",
        "1964"=>"1964",
        "1963"=>"1963",
        "1962"=>"1962",
        "1961"=>"1961",
        "1960"=>"1960",
        "1959"=>"1959",
        "1959"=>"1959",
        "1958"=>"1958",
        "1957"=>"1957",
        "1956"=>"1956",
        "1955"=>"1955",
        "1954"=>"1954",
        "1953"=>"1953",
        "1953"=>"1953",
        "1952"=>"1952",
        "1951"=>"1951",
        "1950"=>"1950",
        "1949"=>"1949",
        "1948"=>"1948",
        "1947"=>"1947",
        "1946"=>"1946",
        "1945"=>"1945",
        "1944"=>"1944",
        "1943"=>"1943",
        "1942"=>"1942",
        "1941"=>"1941",
        "1940"=>"1940",
        "1939"=>"1939",
        "1938"=>"1938",
        "1937"=>"1937",
        "1936"=>"1936",
        "1935"=>"1935",
        "1934"=>"1934",
        "1933"=>"1933",
        "1932"=>"1932",
        "1931"=>"1931",
        "1930"=>"1930"

   );

   $html = "<select name='{$year}'>";

   foreach($years as $key => $year){

      $selected = "";
      if( $key == $top_years ) {
          $selected = "selected='selected'";
      }

     $html .="<option value='{$key}' $selected >{$year}</option>";
   }
   $html .="</select>";
   return $html;

}

// VIEW

Code:
&lt;?php
    
     echo month_dropdown('month', $user['birthday'])." ".day_dropdown('day', $user['birthday'])." ".year_dropdown('year', $user['birthday']);
      
?&gt;





Theme © iAndrew 2016 - Forum software by © MyBB