Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter: Using URIs with forms
    text
    copied!<p>I'm using URIs to direct a function in a library... View File (It's huge, so I'm just posting the opening line):</p> <pre><code>&lt;?php print form_open('survey',array('class'=&gt;'horizontal','id'=&gt;'form_survey'))?&gt; </code></pre> <p>Library:</p> <pre><code>function survey_form($container) { $id = $this-&gt;CI-&gt;session-&gt;userdata('id'); // Setup fields for($i=1;$i&lt;18;$i++){ $fields["a_".$i] = 'Question '.$i; } for($i=1;$i&lt;24;$i++){ $fields["b_".$i] = 'Question '.$i; } $fields["firstname"] = "First Name"; $fields["lastname"] = "Last Name"; $fields["phone"] = "Phone"; $fields["company_name"] = "Company Name"; $fields['company_address'] = "company_address"; $fields['company_phone'] = "company_phone"; $fields['company_state'] = "company_state"; $fields['company_city'] = "company_city"; $fields['company_zip'] = "company_zip"; $fields['job_title'] = "job_title"; $fields['job_type'] = "job_type"; $fields['job_time'] = "job_time"; $fields['department'] = "department"; $fields['supervisor'] = "supervisor"; $fields['vision'] = "vision"; $fields['height'] = "height"; $fields['weight'] = "weight"; $fields['hand_dominance'] = "hand_dominance"; $fields['areas_of_fatigue'] = "areas_of_fatigue"; $fields['job_description'] = "job_description"; $fields['injury_review'] = "injury_review"; $fields['job_positive'] = "job_positive"; $fields['risk_factors'] = "risk_factors"; $fields['job_improvement_short'] = "job_improvement_short"; $fields['job_improvement_long'] = "job_improvement_long"; $fields["c_1"] = "Near Lift"; $fields["c_2"] = "Middle Lift"; $fields["c_3"] = "Far Lift"; $this-&gt;CI-&gt;validation-&gt;set_fields($fields); // Set Rules for($i=1;$i&lt;18;$i++){ $rules["a_".$i]= 'hour|integer|max_length[2]'; } for($i=1;$i&lt;24;$i++){ $rules["b_".$i]= 'hour|integer|max_length[2]'; } // Setup form default values $this-&gt;CI-&gt;validation-&gt;set_rules($rules); if ( $this-&gt;CI-&gt;validation-&gt;run() === FALSE ) { // Output any errors $this-&gt;CI-&gt;validation-&gt;output_errors(); } else { // Submit form $this-&gt;_submit(); } //Tool for current user if ($method == 'update') { // Modify form, first load $this-&gt;CI-&gt;db-&gt;from('be_user_profiles'); $this-&gt;CI-&gt;db-&gt;where('user_id' , $id); $user = $this-&gt;CI-&gt;db-&gt;get(); $this-&gt;CI-&gt;db-&gt;from('be_survey'); $this-&gt;CI-&gt;db-&gt;where('user_id' , $id); $survey = $this-&gt;CI-&gt;db-&gt;get(); $user = array_merge($user-&gt;row_array(),$survey-&gt;row_array()); $this-&gt;CI-&gt;validation-&gt;set_default_value($user); // Display page $data['user'] = $user; } $data['header'] = 'Risk Assessment Survey'; $data['page'] = $this-&gt;CI-&gt;config-&gt;item('backendpro_template_public') . 'form_survey'; $this-&gt;CI-&gt;load-&gt;view($container,$data); } </code></pre> <p>Submit function:</p> <pre><code>function _submit() { $URI = $this-&gt;CI-&gt;uri-&gt;uri_string(); $new = "new"; if(strpos($URI, $new) === FALSE){ $method = "update"; } elseif(strpos($URI, $new) !== FALSE){ $method = "create"; } //Submit and Update for current User $id = $this-&gt;CI-&gt;session-&gt;userdata('id'); $this-&gt;CI-&gt;db-&gt;select('users.id, users.username, users.email, profiles.firstname, profiles.manager_id'); $this-&gt;CI-&gt;db-&gt;from('be_users' . " users"); $this-&gt;CI-&gt;db-&gt;join('be_user_profiles' . " profiles",'users.id=profiles.user_id'); $this-&gt;CI-&gt;db-&gt;having('id', $id); $email_data['user'] = $this-&gt;CI-&gt;db-&gt;get(); $email_data['user'] = $email_data['user']-&gt;row(); $manager_id = $email_data['user']-&gt;manager_id; $this-&gt;CI-&gt;db-&gt;select('firstname','email')-&gt;from('be_user_profiles')-&gt;where('user_id', $manager_id); $email_data['manager'] = $this-&gt;CI-&gt;db-&gt;get(); $email_data['manager'] = $email_data['manager']-&gt;row(); // Fetch what they entered in the form for($i=1;$i&lt;18;$i++){ $survey["a_".$i]= $this-&gt;CI-&gt;input-&gt;post('a_'.$i); } for($i=1;$i&lt;24;$i++){ $survey["b_".$i]= $this-&gt;CI-&gt;input-&gt;post('b_'.$i); } for($i=1;$i&lt;12;$i++){ $survey["c_".$i]= $this-&gt;CI-&gt;input-&gt;post('c_'.$i); } $profile['firstname'] = $this-&gt;CI-&gt;input-&gt;post('firstname'); $profile['lastname'] = $this-&gt;CI-&gt;input-&gt;post('lastname'); $profile['test_date'] = date ("Y-m-d H:i:s"); $profile['company_name'] = $this-&gt;CI-&gt;input-&gt;post('company_name'); $profile['company_address'] = $this-&gt;CI-&gt;input-&gt;post('company_address'); $profile['company_city'] = $this-&gt;CI-&gt;input-&gt;post('company_city'); $profile['company_phone'] = $this-&gt;CI-&gt;input-&gt;post('company_phone'); $profile['company_state'] = $this-&gt;CI-&gt;input-&gt;post('company_state'); $profile['company_zip'] = $this-&gt;CI-&gt;input-&gt;post('company_zip'); $profile['job_title'] = $this-&gt;CI-&gt;input-&gt;post('job_title'); $profile['job_type'] = $this-&gt;CI-&gt;input-&gt;post('job_type'); $profile['job_time'] = $this-&gt;CI-&gt;input-&gt;post('job_time'); $profile['department'] = $this-&gt;CI-&gt;input-&gt;post('department'); $profile['vision'] = $this-&gt;CI-&gt;input-&gt;post('vision'); $profile['height'] = $this-&gt;CI-&gt;input-&gt;post('height'); $profile['weight'] = $this-&gt;CI-&gt;input-&gt;post('weight'); $profile['hand_dominance'] = $this-&gt;CI-&gt;input-&gt;post('hand_dominance'); $profile['areas_of_fatigue'] = $this-&gt;CI-&gt;input-&gt;post('areas_of_fatigue'); $profile['job_description'] = $this-&gt;CI-&gt;input-&gt;post('job_description'); $profile['injury_review'] = $this-&gt;CI-&gt;input-&gt;post('injury_review'); $profile['job_positive'] = $this-&gt;CI-&gt;input-&gt;post('job_positive'); $profile['risk_factors'] = $this-&gt;CI-&gt;input-&gt;post('risk_factors'); $profile['job_improvement_short'] = $this-&gt;CI-&gt;input-&gt;post('job_improvement_short'); $profile['job_improvement_long'] = $this-&gt;CI-&gt;input-&gt;post('job_improvement_long'); if ($method == "update") { //Begin db transmission $this-&gt;CI-&gt;db-&gt;trans_begin(); $this-&gt;CI-&gt;home_model-&gt;update('Survey',$survey, array('user_id' =&gt; $id)); $this-&gt;CI-&gt;db-&gt;update('be_user_profiles',$profile, array('user_id' =&gt; $id)); if ($this-&gt;CI-&gt;db-&gt;trans_status() === FALSE) { flashMsg('error','There was a problem entering your test! Please contact an administrator.'); redirect('survey','location'); } else { //Get credits of user and subtract 1 $this-&gt;CI-&gt;db-&gt;set('credits', 'credits -1', FALSE); $this-&gt;CI-&gt;db-&gt;update('be_user_profiles',$profile, array('user_id' =&gt; $manager_id)); //Mark the form completed. $this-&gt;CI-&gt;db-&gt;set('test_complete', '1'); $this-&gt;CI-&gt;db-&gt;where('user_id', $id)-&gt;update('be_user_profiles'); // Stuff worked... $this-&gt;CI-&gt;db-&gt;trans_commit(); //Get Manager Information $this-&gt;CI-&gt;db-&gt;select('users.id, users.username, users.email, profiles.firstname'); $this-&gt;CI-&gt;db-&gt;from('be_users' . " users"); $this-&gt;CI-&gt;db-&gt;join('be_user_profiles' . " profiles",'users.id=profiles.user_id'); $this-&gt;CI-&gt;db-&gt;having('id', $email_data['user']-&gt;manager_id); $email_data['manager'] = $this-&gt;CI-&gt;db-&gt;get(); $email_data['manager'] = $email_data['manager']-&gt;row(); //Email User $this-&gt;CI-&gt;load-&gt;library('User_email'); $data_user = array( 'firstname'=&gt;$email_data['user']-&gt;firstname, 'email'=&gt; $email_data['user']-&gt;email, 'user_completed'=&gt;$email_data['user']-&gt;firstname, 'site_name'=&gt;$this-&gt;CI-&gt;preference-&gt;item('site_name'), 'site_url'=&gt;base_url() ); //Email Manager $data_manager = array( 'firstname'=&gt;$email_data['manager']-&gt;firstname, 'email'=&gt; $email_data['manager']-&gt;email, 'user_completed'=&gt;$email_data['user']-&gt;firstname, 'site_name'=&gt;$this-&gt;CI-&gt;preference-&gt;item('site_name'), 'site_url'=&gt;base_url() ); $this-&gt;CI-&gt;user_email-&gt;send($email_data['manager']-&gt;email,'Completed the Assessment Tool','public/email_manager_complete',$data_manager); $this-&gt;CI-&gt;user_email-&gt;send($email_data['user']-&gt;email,'Completed the Assessment Tool','public/email_user_complete',$data_user); flashMsg('success','You finished the assessment successfully!'); redirect('home','location'); } } //Create New User elseif ($method == "create") { // Build $profile['user_id'] = $id; $profile['manager_id'] = $manager_id; $profile['test_complete'] = '1'; $survey['user_id'] = $id; $this-&gt;CI-&gt;db-&gt;trans_begin(); // Add user_profile details to DB $this-&gt;CI-&gt;db-&gt;insert('be_user_profiles',$profile); $this-&gt;CI-&gt;db-&gt;insert('be_survey',$survey); if ($this-&gt;CI-&gt;db-&gt;trans_status() === FALSE) { // Registration failed $this-&gt;CI-&gt;db-&gt;trans_rollback(); flashMsg('error',$this-&gt;CI-&gt;lang-&gt;line('userlib_registration_failed')); redirect('auth/register','location'); } else { // User registered $this-&gt;CI-&gt;db-&gt;trans_commit(); flashMsg('success',$this-&gt;CI-&gt;lang-&gt;line('userlib_registration_success')); redirect($this-&gt;CI-&gt;config-&gt;item('userlib_action_register'),'location'); } } } </code></pre> <p>Controller function:</p> <pre><code>function survey() { $id = $this-&gt;session-&gt;userdata('id'); $data['member'] = $this-&gt;home_model-&gt;getUsers(array('user_id'=&gt;$id)); //Convert the db Object to a row array $data['manager'] = $data['member']-&gt;row(); $manager_id = $data['manager']-&gt;manager_id; $data['manager'] = $this-&gt;home_model-&gt;getUsers(array('manager_id'=&gt;$id)); $data['manager'] = $data['manager']-&gt;row(); if ($data['manager']-&gt;credits == '0') { flashMsg('warning',"You can't complete the assessment until your manager has purchased credit."); redirect('home','location'); } elseif ($data['manager']-&gt;test_complete == '3'){ flashMsg('warning',"You already completed the Assessment."); redirect('home','location'); } else{ $data['header'] = "Home"; $this-&gt;survey_form_processing-&gt;survey_form($this-&gt;_container,$data); } } </code></pre> <p>The submit function is similar, updating the db if $method == "update", and inserting if the method == "create".</p> <p><strong>The problem is</strong>, when the form is submitted, it doesn't take into account the url b/c the form submits to the function "survey", which passes data to the lib function, so things are always updated, never created.</p> <p><em>How can I pass $method to the _submit() function correctly?!</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload