weixin_33699914 2018-04-03 16:07 采纳率: 0%
浏览 19

Cakephp 2.x中的Ajax

I have a problem, i need to use, ajax in a form, in my page i have to change the color of a label after i search if a data is in a data base, if the data exist i must change the color of the label to red, if not i have to changed to green, i know how to use this in pure php, but i don´t know how to do that in cakephp, if i am not wrong in pure php this is the forme to do it:

View

<form action="prueba.php" method="post">
<input type="text" id="txt_prueba" class="validador" />
<submit value="enviar"/>
</form>

View in Cake

<?php
  echo $this->Form->create('Prueba', array('url' => 'prueba.php', 'type' => 'post'));

         echo $this->Form->input('textoPrueba', array('label' => false,
            'class' => 'validador'));
   echo $this->Form->end(); ?>

Script

    $(".validador").on('keyup keypress blur change', function (tecla) {
      $.ajax({
          method: "POST",
          url: "algun.php",
          data: { 
           name: $("#txt_prueba").val();
          }
      })
      .done(function( msg ) {
           if (msg=="Yes"){
                  $("#txt_prueba").css('background-color', 'red');
            } else{
                  $("#txt_prueba").css('background-color', 'green');
            }
       });
});

Controller

require('conexion.php');

$consulta = $_POST['name'];

if (isset($consulta)) {

    $consulta = mysqli_query($conexion, "SELECT * FROM tabla1
    WHERE nombre LIKE '$consulta'");

   $filas = mysqli_num_rows($consulta);

   if ($filas === 0) {
            echo 'Not';

   }else {
     echo 'Yes';
   }
};
  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码