dongye3917 2017-03-10 08:58
浏览 51
已采纳

将数据从下拉列表保存到数据库CakePHP

I'm new to the CakePhp framework and currently i'm testing out a project. Issue i have is, i can populate the dropdown option but when i want to select an option it won't get saved on the database. Did i miss something?

Using this code i can populate the drop down - 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(['myemail@gmail.com' => '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>
  • 写回答

1条回答 默认 最新

  • dongye9191 2017-03-10 10:33
    关注

    As i told you, your int field in the table cannot save the varchar of service name. So, you need to send the service id as value from the select option. Like so :

    <option value="<?= h($Services->id)?>"><?= h($Services->name)?></option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献