2017-03-10 3 views
0

Ich bin neu im CakePhp-Framework und derzeit teste ich ein Projekt aus. Problem, das ich habe, ist, ich kann die Dropdown-Option ausfüllen, aber wenn ich eine Option auswählen möchte, wird es nicht in der Datenbank gespeichert. Habe ich etwas verpasst?Speichern von Daten aus einem Dropdown-Menü in eine Datenbank CakePHP

diesen Code ich mit dem Drop-down-bevölkern können - bookings.js

$("#contact_submit").click(function() { 

    var name = $("#name").val(); 
    var contact_email = $("#contact_email").val(); 
    var subject = $("#subject").val(); 
    var message = $("#txtmessage").val(); 
    var service = $("#sel1").val(); 

    console.log(name+contact_email+subject+message+service); 

    $.post("/inquiries/inquiry", //Required URL of the page on server 
      {// Data Sending With Request To Server 
       name: name, 
       email: contact_email, 
       subject: subject, 
       txt: message, 
       service_id: service, 
      }, 
      function (response) { // Required Callback Function 
      if (jQuery.parseJSON(response).status == 'success') { 
      if ($("#available").hasClass("hide")) { 
       $("#available").removeClass("hide"); 
      } 
      if (!$("#available").hasClass("hide")) { 
       $("#notavailable").addClass("hide"); 
      } 

      }else{ 
      if ($("#notavailable").hasClass("hide")) { 
       $("#notavailable").removeClass("hide"); 
      } 
      if (!$("#available").hasClass("hide")) { 
       $("#available").addClass("hide"); 
      } 
      } 
      }); 
}); 

InquriesController.php

public function inquiry() 
    { 
    $inquiry = $this->Inquiries->newEntity(); 
    if ($this->request->is('post')) { 

    $emailaddress = $this->request->data['email']; 
    $subject = $this->request->data['subject']; 
    $txt = $this->request->data['txt']; 
    $services = $this->request->data['service_id']; 
    $message = "this is a message"; 
    $inquiry = $this->Inquiries->patchEntity($inquiry, $this->request->data); 

     if ($this->Inquiries->save($inquiry)) { 

     $email = new Email('default'); 
     $email->from(['[email protected]' => 'Testing Hotel']) 
     ->to($emailaddress) 
     ->subject('Inquiry Mail') 
     ->emailFormat('html') 
     ->send($message); 

     echo json_encode(['status' => 'success']); 
     exit; 

     } else { 

     echo json_encode(['status' => 'failed']); 
     exit; 

     } 
    } 

} 

HTML-Code-Snippet

 <div class="col-md-6 col-sm-6 col-xs-12"> 
      <input type="text" name="contact_email" id="contact_email" class="form-control" placeholder="Email *"> 
     </div> 
     <div class="col-md-12 col-sm-12 col-xs-12"> 
      <input type="text" id="subject" class="form-control" placeholder="Subject"> 
     </div> 
     <div class="col-md-12 col-sm-12 col-xs-12"> 

      <input class="form-control" name="txtmessage" id="txtmessage" rows="8" placeholder="Messages goes here.."></input> 

      <div class="form-group"> 
       <select class="form-control" id="sel1"> 
        <option>Select a service</option> 
        <?php 
         if (!empty($ServicesInfo)) { 
          foreach ($ServicesInfo as $Services):   
          ?> 
         <option value="<?= h($Services->name)?>"><?= h($Services->name)?></option> 
        <?php endforeach; } ?> 
       </select> 
      </div> 
+0

Können andere Felder nicht auch speichern? – Aarrbee

+0

Nur die service_id, die nicht gespeichert wird. –

+0

Sie senden '$ Services-> name' in' service_id'. Vielleicht hat Ihre Datenbank das Feld ** int ** und deshalb wird der Name des Dienstes nicht gespeichert. – Aarrbee

Antwort

1

Wie gesagt, Ihr int Feld in der Tabelle kann nicht die varchar von Service-Namen speichern. Daher müssen Sie die Service-ID als Wert aus der Auswahloption senden. Wie so:

<option value="<?= h($Services->id)?>"><?= h($Services->name)?></option> 
+0

Ja, dein Code funktioniert. Danke eine Million. –

Verwandte Themen