2015-08-18 1 views
0

J'essaie de transmettre une donnée de formulaire comme le nom, l'email d'une simple page html à une application CodeIgniter. direcotry Structute: SampleDirTransmettre des données de formulaire de l'extérieur à CodeIgniter

  • CodeIgniterApp
  • form.html

Je suis en train de passer sous forme (POST) et recevoir à l'intérieur du CodeIgniter. Je suis nouveau à CodeIgniter et essaie de connecter mon application à une application tierce. De ce que j'ai cherché CodeIgniter a des contrôleurs et des vues. Les contrôleurs étant appelés en premier, ils inturnent la vue.

J'ai essayé

$view = array (
       'available_services' => $available_services, 
       'available_providers' => $available_providers, 
       'company_name'   => $company_name, 
       'manage_mode'   => $manage_mode, 
       'appointment_data'  => $appointment, 
       'provider_data'   => $provider, 
       'customer_data'   => $customer, 
       'post_data'    => json_decode($_POST) 
      ); 

et en passant à voir, mais il n'a pas apparaît.

Code HTML:

<form action="/appointment" method="POST" target="_blank"> 
      <div style="display:none!important;"> 
       <input type="text" placeholder="name" name="name" id="name" ng-model="cust.name"> 
       <input type="text" placeholder="email" name="email" id="email" ng-model="cust.email"> 
       <input type="text" placeholder="telephone" name="phone" id="phone" ng-model="cust.phone"> 
      </div> 

      <div class="text-center btn-toolbar" style="margin-top: 30px;"> 
       <button class="btn btn-primary" ng-click="cancel()" style="font-size: 20px;">OK</button> 
       <button type="submit" name="process" class="btn btn-success" style="font-size: 20px;">Schedule a Call</button> 
      </div> 
     </form> 

Code du contrôleur:

public function index($appointment_hash = '') { 
    // echo $this->input->post('email'); 
    var_dump($_SERVER['REQUEST_METHOD']); 
    if (!$this->check_installation()) return; 

    $this->load->model('appointments_model'); 
    $this->load->model('providers_model'); 
    $this->load->model('services_model'); 
    $this->load->model('customers_model'); 
    $this->load->model('settings_model'); 

    if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') { 
     try { 
      $available_services = $this->services_model->get_available_services(); 
      $available_providers = $this->providers_model->get_available_providers(); 
      $company_name  = $this->settings_model->get_setting('company_name'); 

      // If an appointment hash is provided then it means that the customer 
      // is trying to edit a registered appointment record. 
      if ($appointment_hash !== ''){ 
       // Load the appointments data and enable the manage mode of the page. 
       $manage_mode = TRUE; 

       $results = $this->appointments_model->get_batch(array('hash' => $appointment_hash)); 

       if (count($results) === 0) { 
        // The requested appointment doesn't exist in the database. Display 
        // a message to the customer. 
        $view = array(
         'message_title' => $this->lang->line('appointment_not_found'), 
         'message_text' => $this->lang->line('appointment_does_not_exist_in_db'), 
         'message_icon' => $this->config->item('base_url') 
             . '/assets/img/error.png' 
        ); 
        $this->load->view('appointments/message', $view);       
        return; 
       } 

       $appointment = $results[0]; 
       $provider = $this->providers_model->get_row($appointment['id_users_provider']); 
       $customer = $this->customers_model->get_row($appointment['id_users_customer']); 

      } else { 
       // The customer is going to book a new appointment so there is no 
       // need for the manage functionality to be initialized. 
       $manage_mode  = FALSE; 
       $appointment = array(); 
       $provider  = array(); 
       $customer  = array(); 
      } 

      // Load the book appointment view. 
      $view = array (
       'available_services' => $available_services, 
       'available_providers' => $available_providers, 
       'company_name'   => $company_name, 
       'manage_mode'   => $manage_mode, 
       'appointment_data'  => $appointment, 
       'provider_data'   => $provider, 
       'customer_data'   => $customer, 
       'post_data'    => json_decode($_POST) 
      ); 

     } catch(Exception $exc) { 
      $view['exceptions'][] = $exc; 
     } 

     $this->load->view('appointments/book', $view); 

    } else { 
     // The page is a post-back. Register the appointment and send notification emails 
     // to the provider and the customer that are related to the appointment. If google 
     // sync is enabled then add the appointment to the provider's account. 

     try { 
      $post_data = json_decode($_POST['post_data'], true); 
      $appointment = $post_data['appointment']; 
      $customer = $post_data['customer']; 

      if ($this->customers_model->exists($customer)) 
        $customer['id'] = $this->customers_model->find_record_id($customer); 

      $customer_id = $this->customers_model->add($customer); 
      $appointment['id_users_customer'] = $customer_id; 

      $appointment['id'] = $this->appointments_model->add($appointment); 
      $appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']); 

      $provider = $this->providers_model->get_row($appointment['id_users_provider']); 
      $service = $this->services_model->get_row($appointment['id_services']); 

      $company_settings = array( 
       'company_name' => $this->settings_model->get_setting('company_name'), 
       'company_link' => $this->settings_model->get_setting('company_link'), 
       'company_email' => $this->settings_model->get_setting('company_email') 
      ); 

      // :: SYNCHRONIZE APPOINTMENT WITH PROVIDER'S GOOGLE CALENDAR 
      // The provider must have previously granted access to his google calendar account 
      // in order to sync the appointment. 
      try { 
       $google_sync = $this->providers_model->get_setting('google_sync', 
         $appointment['id_users_provider']); 

       if ($google_sync == TRUE) { 
        $google_token = json_decode($this->providers_model 
          ->get_setting('google_token', $appointment['id_users_provider'])); 

        $this->load->library('google_sync'); 
        $this->google_sync->refresh_token($google_token->refresh_token); 

        if ($post_data['manage_mode'] === FALSE) { 
         // Add appointment to Google Calendar. 
         $google_event = $this->google_sync->add_appointment($appointment, $provider, 
           $service, $customer, $company_settings); 
         $appointment['id_google_calendar'] = $google_event->id; 
         $this->appointments_model->add($appointment); 
        } else { 
         // Update appointment to Google Calendar. 
         $appointment['id_google_calendar'] = $this->appointments_model 
           ->get_value('id_google_calendar', $appointment['id']); 

         $this->google_sync->update_appointment($appointment, $provider, 
           $service, $customer, $company_settings); 
        } 
       } 
      } catch(Exception $exc) { 
       $view['exceptions'][] = $exc; 
      } 

      // :: SEND NOTIFICATION EMAILS TO BOTH CUSTOMER AND PROVIDER 
      try { 
       $this->load->library('Notifications'); 

       $send_provider = $this->providers_model 
         ->get_setting('notifications', $provider['id']); 

       if (!$post_data['manage_mode']) { 
        $customer_title = $this->lang->line('appointment_booked'); 
        $customer_message = $this->lang->line('thank_you_for_appointment'); 
        $customer_link = $this->config->item('base_url') . '/index.php/appointments/index/' 
          . $appointment['hash']; 

        $provider_title = $this->lang->line('appointment_added_to_your_plan'); 
        $provider_message = $this->lang->line('appointment_link_description'); 
        $provider_link = $this->config->item('base_url') . '/index.php/backend/index/' 
          . $appointment['hash']; 
       } else { 
        $customer_title = $this->lang->line('appointment_changes_saved'); 
        $customer_message = ''; 
        $customer_link = $this->config->item('base_url') . '/index.php/appointments/index/' 
          . $appointment['hash']; 

        $provider_title = $this->lang->line('appointment_details_changed'); 
        $provider_message = ''; 
        $provider_link = $this->config->item('base_url') . '/index.php/backend/index/' 
          . $appointment['hash']; 
       } 

       $this->notifications->send_appointment_details($appointment, $provider, 
         $service, $customer,$company_settings, $customer_title, 
         $customer_message, $customer_link, $customer['email']); 

       if ($send_provider == TRUE) { 
        $this->notifications->send_appointment_details($appointment, $provider, 
          $service, $customer, $company_settings, $provider_title, 
          $provider_message, $provider_link, $provider['email']); 
       } 
      } catch(Exception $exc) { 
       $view['exceptions'][] = $exc; 
      } 

      // :: LOAD THE BOOK SUCCESS VIEW 
      $view = array(
       'appointment_data' => $appointment, 
       'provider_data'  => $provider, 
       'service_data'  => $service, 
       'company_name'  => $company_settings['company_name'] 
      ); 

     } catch(Exception $exc) { 
      $view['exceptions'][] = $exc; 
     } 

     $this->load->view('appointments/book_success', $view); 
    } 
} 
$this->load->view('appointments/book', $view); 

Pour être plus précis, ce la l'application que je suis en train de se connecter à https://github.com/alextselegidis/easyappointments

Si le dossier src racine est rendez-vous, puis http://localhost/appointment me prend rendez-vous/application/views/rendez-vous/book.php et rendez-vous/application/controllers/rendez-vous.php

Jetez un coup d'oeil et suggérez quoi faire.

+1

d'où viennent toutes ces variables que vous utilisez dans ce tableau? nous aurons besoin de voir cette partie du code .... – Rooster

+0

Etrange: Quand je fais var_dump ($ _ SERVER ['REQUEST_METHOD']); Je reçois «GET» sur la vue –

+0

@ Rooster: édité la question avec le code du contrôleur ou voir https://github.com/alextselegidis/easyappointments/blob/master/src/application/controllers/appointments.php –

Répondre

0

Je cette difficulté aussi, vous devez ajouter cette ligne sur votre code:

$_POST = json_decode(file_get_contents("php://input"), true); 

qui vous permettra d'utiliser JSON sur poste CodeIgniter donc après cette ligne que vous pouvez faire

$this->input->post('yourkey') 

et vous obtiendrez les choses que vous voulez :)

+0

Non. Je reçois bool (faux) quand je fais $ _POST = json_decode (file_get_contents ("php: // input"), true); var_dump ($ this-> input-> post ('nom')); –

+0

Pourquoi -1? J'essaye juste de vous aider j'ai le même problème, sardez-vous des données de json au point de terminaison d'allumeur de code? pls Ajoutez une copie de votre $ _POST (var_dump ($ _ POST)) pour voir ce que vous avez –

+0

Je ne l'ai pas fait. Pardon. si c'est fait. J'apprécie vraiment votre aide et merci. Vos suggestions ont aidé à avancer et à résoudre la clé. mais je n'ai pas eu besoin d'utiliser $ _POST = json_decode (file_get_contents ("php: // input"), true); –