繁体   English   中英

使用ajax提交表单后无法显示消息

[英]Not able to display message after submitting form using ajax

我正在尝试使用CodeIgniter AJAX提交表单。 表单的值已保存在数据库中,但在控制器中设置的回复未在console.log显示或在AJAX代码中显示为alert

形式代码

<form class="form-signup" id="signup-form" method="post">
    <input type="email" class="form-control" placeholder="Email" name="email" id="email">
    <input type="password" class="form-control" placeholder="Password" name="password" id="password">
    <button type="submit" class="btn btn-primary btn-lg btn-signup col-sm-offset-1" id="submit_form">SIGN UP</button>
</form>

脚本代码

<script type="text/javascript">
    $(document).ready(function() {
        $("#submit_form").click(function(event) {
        event.preventDefault();
        var email = $("input#email").val();
        var password = $("input#password").val();
        jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "student/register",
        dataType: 'json',
        data: {email: email, password: password},
        success: function(res) {
        if (res)
            {
                console.log(res); //need to print the result here
                //alert(res);
            }
        }
        }); 
   });  
});

控制器代码

public function register()
{
     $data = array(
          'email' => $this->input->post('email'),
          'password'=>$this->input->post('password')
     );

     $email = $data['email'];
     $password = $data['password'];

     $this->db->where('email',$email);
     $query = $this->db->get('student'); 
     if ($query->num_rows() > 0)
     {
         echo "Email already exist";
     }
     else
     {
        $data1=array(
            'email' => $email,
            'password' => md5($password)
        );

        $final=$this->signin_model->register_user($data1);
        return $final;     
     }   
}

型号代码

public function register_user($data1)
{        
     $success=$insert_data = $this->db->insert('student', $data1);
     if($success)
     {
         $result= "success ";
     }
     else
     {
         $result= "register unsuccessful";
         return $result;           
     }
 }

如代码中所示,有3条消息

电子邮件已经存在

成功

注册失败

在AJAX中,如果我执行console.logalert ,则希望根据流显示上述3条消息中的任何console.log

如何在前端显示回复?

您必须使用echo而不是返回成功。

请如下更改

if ($query->num_rows() > 0)
{
   echo "Email already exist";
}
else
{
   $data1=array(
         'email' => $email,
         'password' => md5($password)
       );

   $final=$this->signin_model->register_user($data1);
   echo $final;     
}   

并删除一起初始化的2个变量。 那是没有必要的。 这可以。 $success = $this->db->insert('student', $data1);

希望这可以帮到你。

您使用的ajax的数据类型为json。 因此,如果您希望数据显示在前端,则可以将回复编码为json,或者需要从ajax中更改或删除json数据类型

请将dataType:'json'更改为dataType:'text'

<script type="text/javascript">
    $(document).ready(function() {
        $("#submit_form").click(function(event) {
        event.preventDefault();
        var email = $("input#email").val();
        var password = $("input#password").val();
        jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "student/register",
        dataType: 'text',
        data: {email: email, password: password},
        success: function(res) {
        if (res)
            {
                console.log(res); //need to print the result here
                //alert(res);
            }
        }
        }); 
   });  
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM