CodeIgniter 3:在Ajax中提交给Controller

时间:2018-01-03 10:55:51

标签: javascript ajax codeigniter

我正在尝试创建通知功能,我希望ajax能够通过按钮单击控制器运行查询

这是我的脚本

 $('#noti_Button').click(function (e) {
            e.preventDefault();
             $.ajax({
                url: '<?php echo site_url("profile/read_notif")?>'

            });
});

控制器

public function read_notif(){
        $this->profile_model->read_notifs($data['id']);
        return;
    }

和模型

 function read_notifs($id)
    {
        $read = array(
        'read' => '1'
        );
        $this->db->where('recipient', $id);
        $this->db->update('tbl_notifications', $read);
        return;
    }

我试过了,数据库中的数据没有更新。 在我的HTML中它只是一个简单的按钮

3 个答案:

答案 0 :(得分:1)

这是在rails上调用ruby中的ajax的示例。在此代码中,我们调用控制器来获取值。

$('#Button').on('change', function(event) {
  var selected_resource_id = $(this).val();
  $.ajax({ 
   type: 'GET', 
   url: "<%= Home_index_path %>",
   data: { id: selected_resource_id }, 
   success: function (data) {  
    alert("Ajax success");
   }
  });
});

答案 1 :(得分:1)

脚本

public function read_notif(){
        $this->profile_model->read_notifs($data['id']);
        echo $this->db->last_query();
    }

控制器

<div data-section-id="{{ section.id }}" data-section-type="slideshow-section">
    {% if section.blocks.size > 0 %}
<div class="slideshow-wrapper">
  <button type="button" class="visually-hidden slideshow__pause" data-id="{{ section.id }}" aria-live="polite">
    <span class="slideshow__pause-stop">
      {% include 'icon-pause' %}
      <span class="icon__fallback-text">{{ 'sections.slideshow.pause_slideshow' | t }}</span>
    </span>
    <span class="slideshow__pause-play">
      {% include 'icon-play' %}
      <span class="icon__fallback-text">{{ 'sections.slideshow.play_slideshow' | t }}</span>
    </span>
  </button>

  <div class="slideshow slideshow--{{ section.settings.slideshow_height }}" id="Slideshow-{{ section.id }}" data-autoplay="{{ section.settings.autoplay }}" data-speed="{{ section.settings.autoplay_speed }}">
    {% for block in section.blocks %}
      {%- assign is_background_video = false -%}
      {% if block.type == 'video' %}
        {% if block.settings.video_type == 'background' or block.settings.video_type =='background-chrome' %}
          {%- assign is_background_video = true -%}
        {% endif %}
      {% endif %}

      <div class="slideshow__slide slideshow__slide--{{ block.id }}{% if is_background_video %} slideshow__slide--background-video{% endif %}" {{ block.shopify_attributes }}>
        {% if block.settings.link != blank %}
          <a href="{{ block.settings.link }}" class="slideshow__link">
        {% endif %}

        {% if block.type == 'video' %}
          {% if block.settings.video_url != blank %}
            <div class="video-loader"></div>
          {% endif %}
          {% unless block.settings.video_type == 'background' %}
            <button type="button" class="text-link slideshow__video-control slideshow__video-control--close" data-controls="SlideshowVideo-{{ block.id }}">
              {% include 'icon-close' %}
              <span class="icon__fallback-text">{{ 'sections.slideshow.close_video' | t }}</span>
            </button>
          {% endunless %}
          {% if block.settings.video_url != blank %}
            <div id="SlideshowVideo-{{ block.id }}" class="slideshow__video {% if is_background_video %}slideshow__video--background{% endif %} slideshow__video--{{ block.settings.video_type }}"
              data-id="{{ block.settings.video_url.id }}"
              data-type="{{ block.settings.video_type }}"
              data-slideshow="Slideshow-{{ section.id }}"></div>
          {% endif %}
        {% endif %}

        {% if block.settings.image == blank %}
          <div class="slideshow__image js">
            <div class="placeholder-background">
              {% capture current %}{% cycle 1, 2 %}{% endcapture %}
              {{ 'lifestyle-' | append: current | placeholder_svg_tag: 'placeholder-svg' }}
            </div>
          </div>
        {% else %}
          <div class="slideshow__image box ratio-container lazyload{% unless forloop.first == true %} lazypreload{% endunless %} js"
               data-bgset="{% include 'bgset', image: block.settings.image %}"
               data-sizes="auto"
               data-parent-fit="cover"
               style="background-position: {{ block.settings.alignment }};{% if forloop.first == true %} background-image: url('{{ block.settings.image | img_url: '300x300' }});{% endif %}">
          </div>
        {% endif %}

        <noscript>
          <div class="slideshow__image"{% if block.settings.image %} style="background-image: url('{{ block.settings.image | img_url: '2048x' }}'); background-position: {{ block.settings.alignment }};"{% endif %}>
            {% if block.settings.image == blank %}
              <div class="placeholder-background">
                {% capture current %}{% cycle 1, 2 %}{% endcapture %}
                {{ 'lifestyle-' | append: current | placeholder_svg_tag: 'placeholder-svg' }}
              </div>
            {% endif %}
          </div>
        </noscript>
        <div class="slideshow__text-wrap{% if block.settings.title != blank or block.settings.subheading != blank %} slideshow__overlay{% endif %}">
          <div class="slideshow__text-content">
            <div class="page-width">
              {% unless block.settings.title == blank %}
                <h2 class="h1 mega-title slideshow__title{% if settings.link != blank %} slideshow__title--has-link{% endif %}{% if section.settings.text_size == 'large' %} mega-title--large{% endif %}">
                  {{ block.settings.title | escape }}
                </h2>
              {% endunless %}
              {% unless block.settings.subheading == blank %}
                <span class="mega-subtitle slideshow__subtitle{% if section.settings.text_size == 'large' %} mega-subtitle--large{% endif %}">
                  {{ block.settings.subheading | escape }}
                </span>
              {% endunless %}
              {% if block.type == 'video' %}
                {% unless block.settings.video_type == 'background' %}
                  <div class="slideshow__video-control--play-wrapper{% if block.settings.title != blank or block.settings.subheading != blank %} slideshow__video-control--play-wrapper--push{% endif %}">
                    <button type="button" class="text-link slideshow__video-control slideshow__video-control--play" data-controls="SlideshowVideo-{{ block.id }}">
                      {% include 'icon-play-video' %}
                      <span class="icon__fallback-text">{{ 'sections.slideshow.play_video' | t }}</span>
                    </button>
                  </div>
                {% endunless %}
              {% endif %}
            </div>
          </div>
        </div>

        {% if block.settings.link != blank %}
          </a>
        {% endif %}
      </div>
    {% endfor %}
  </div>
</div>
{% endif %}
{% if section.blocks.size == 0 %}
<div class="placeholder-noblocks">
  {{ 'homepage.onboarding.no_content' | t }}
</div>
{% endif %}
</div>
{% schema %}
{
"name": "Slideshow",
"class": "index-section index-section--flush",
"max_blocks": 4,
"settings": [
  {
    "type": "checkbox",
    "id": "autoplay",
    "label": "Auto-rotate slides",
    "default": true
  },
  {
    "type": "select",
    "id": "autoplay_speed",
    "label": "Change slides every",
    "options": [
      { "value": "5000", "label": "5 seconds" },
      { "value": "6000", "label": "6 seconds" },
      { "value": "7000", "label": "7 seconds" },
      { "value": "8000", "label": "8 seconds" },
      { "value": "9000", "label": "9 seconds" },
      { "value": "10000", "label": "10 seconds" }
    ],
    "default": "7000"
  },
  {
    "type": "select",
    "id": "slideshow_height",
    "label": "Section height",
    "default": "small",
    "options": [
      {
        "label": "Small",
        "value": "small"
      },
      {
        "label": "Medium",
        "value": "medium"
      },
      {
        "label": "Large",
        "value": "large"
      }
    ]
  },
  {
    "type": "select",
    "id": "text_size",
    "label": "Text size",
    "default": "medium",
    "options": [
      {
        "label": "Medium",
        "value": "medium"
      },
      {
        "label": "Large",
        "value": "large"
      }
    ]
  }
],
"blocks": [
  {
    "type": "image",
    "name": "Image slide",
    "settings": [
      {
        "type": "image_picker",
        "id": "image",
        "label": "Image"
      },
      {
        "type": "select",
        "id": "alignment",
        "label": "Image alignment",
        "default": "top",
        "options": [
          {
            "value": "top",
            "label": "Top"
          },
          {
            "value": "center",
            "label": "Middle"
          },
          {
            "value": "bottom",
            "label": "Bottom"
          }
        ]
      },
      {
        "type": "text",
        "id": "title",
        "label": "Heading",
        "default": "Image slide"
      },
      {
        "type": "text",
        "id": "subheading",
        "label": "Subheading",
        "default": "Tell your brand's story through video and images"
      },
      {
        "type": "url",
        "id": "link",
        "label": "Slide link"
      }
    ]
  },
  {
    "type": "video",
    "name": "Video slide",
    "settings": [
      {
        "type": "video_url",
        "id": "video_url",
        "label": "Video link",
        "accept": ["youtube"],
        "default": "https:\/\/www.youtube.com\/watch?v=_9VUPq3SxOc"
      },
      {
        "type": "select",
        "id": "video_type",
        "label": "Style",
        "options": [
          {
            "value": "chrome",
            "label": "Image with play button"
          },
          {
            "value": "background-chrome",
            "label": "Background video with play button"
          },
          {
            "value": "background",
            "label": "Background video"
          }
        ]
      },
      {
        "type": "image_picker",
        "id": "image",
        "label": "Image",
        "info": "Fallback when autoplaying video is not supported on mobile devices"
      },
      {
        "type": "text",
        "id": "title",
        "label": "Heading",
        "default": "Video slide"
      },
      {
        "type": "text",
        "id": "subheading",
        "label": "Subheading",
        "default": "Tell your brand's story through video and images"
      }
    ]
  }
],
"presets": [{
  "name": "Slideshow",
  "category": "Image",
  "settings": {
    "autoplay": true,
    "autoplay_speed": "5000"
  },
  "blocks": [
    {
      "type": "image"
    },
    {
      "type": "image"
    }
    ]
   }]
  }
{% endschema %}

答案 2 :(得分:1)

使用错误:检查提交表单时是否有任何错误。

$('#button').click(function (e) {
        e.preventDefault();
         $.ajax({
            url: '<?php echo site_url("profile/read_notif")?>',
            success:function(data)
            {
                alert(data);
            }

        });
    });