【问题标题】:while clicking on button it should be redirect to another page in ajax在单击按钮时,它应该被重定向到 ajax 中的另一个页面
【发布时间】:2017-03-01 15:26:59
【问题描述】:

我在我的应用程序中有一个查看需求页面..我有一个称为工作的按钮..当用户单击该按钮时,我使用 ajax 函数将需求表 0 中的状态更改为 1..

所以现在的问题是当用户点击工作按钮时,该按钮应该被重定向到另一个页面..

我尝试了很多..但我做不到..

按钮:

<td><button id="<?php echo $idata->req_id; ?>" onClick="CallFunction(this.id)"  class="btn btn-info">working</button></td> 

Ajax 函数:

 function CallFunction(id)

{

  var url =  'http://127.0.0.1/job_portal/index.php/Requirements/change_status/';

        $.ajax({
        url: url,
        type: 'POST',
        data: {req_id: id},
        dataType: 'JSON',
        success: function(data) 
        {    
            if (data === true) {
                $('#button').text('FINISHED');
         }

            else
            {
              alert('error');
            }
        }
      });
}

</script>

我认为成功功能不起作用..

谁能帮帮我..怎么做..

提前谢谢..

【问题讨论】:

  • 您是否在 [127.0.0.1/job_portal/index.php/Requirements/change_status/] 中返回了 json_encoded 数据“真”??
  • 是的,它正在工作。我可以在我的后端更改我的状态..但是成功功能不起作用..这个成功:function(data) { if (data === true) { $('#button').text('FINISHED'); }
  • $('#button').text('FINISHED'); 您点击的是同一个按钮吗?我的意思是&lt;button id="&lt;?php echo $idata-&gt;req_id; ?&gt;" onClick="CallFunction(this.id)" class="btn btn-info"&gt;working&lt;/button&gt;
  • noo 我无法返回该真实值..但状态工作正常..
  • 不,我点击工作按钮.. $('#button').text('FINISHED');这是在成功之后...按钮应该更改为 FINISHED...但这不起作用..

标签: jquery ajax codeigniter


【解决方案1】:

使用以下代码解决您的问题

在脚本文件中

function CallFunction(id) {
var url = 'http://localhost/job_portal/index.php/Requirements/change_status/';

    $.ajax({
    url: url,
    type: 'POST',
    data: {req_id: id},
    dataType: 'test',
    beforeSend: function(){ $("#"+id).html('please Wait...'); },
    success: function(data) 
    {  
        console.log(data);
        var obj = $.parseJSON(data)
        if(obj.status == 'success')
        {
          $("#"+id).html('Updated');
          window.location.href=window.location.href=obj.url;
        }
    },
    error:function(a,b,c)
    {
      console.log(c);
    }
  });

}

在您的控制器中

public function change_status() 
{ 
    $req_id = $this->input->post('req_id'); 
    $this->RequirementModel->update_status($req_id); 
    $d = $this->RequirementModel->send_email($data['req_id']); 
    echo json_encode(array('status' => 'success', 'url' => base_url('Candidate/add_candidate')));  
    //echo true; 
    exit; 
}

在您的模型中

function update_status($req_id) 
{ 
    $this->db->select('*'); 
    $this->db->from('requirements'); 
    $status = $this->db->query("update requirements set working_status='1' where req_id ='$req_id'"); 
    $data=array('working_status'=>$status); 
    $this->db->where('req_id',$req_id); 
    //$this->db->update('candidates_details',$data); 
    //$query=$this->db->get(); 
    //echo $this->db->last_query(); 
    //return $query->result(); 

}

在您的 send_email 功能中

print_r($to); //-&gt; comment this line

如果您需要有关此问题的更多指南,我们将对其进行整理

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2016-09-09
  • 2019-09-15
  • 1970-01-01
  • 2017-12-06
  • 2020-01-26
  • 1970-01-01
相关资源
最近更新 更多