php - I do not get a response after a post JavaScript CodeIgniter -
i have dropdown courses names(like 'php', 'c', 'c#' ... ). have onchange event put on can check if option drop-down valid(is found in database) - it may seem stupid, myself puting values in dropdown database, doing protecting against sql injection.
the problem not receive response verifycourse.php ... there wrong.
function.js
function complete() { $.post("verifycourse/domain", { domain: $('#domain').val().tolowercase() }).done(function (returnvalue) { alert('after post'); //it not alert var result = jquery.parsejson(returnvalue); if (result['returnvalue'] == 'ok') { alert('good domain'); } else if (result['returnvalue'] == 'wrong') { alert('wrong domain'); } }); }
verifycourse.php
function index($param = 1) { parent::__construct(); $this->load->model('user','',true); $this->load->library( 'mysmarty' ); $smarty = new mysmarty ; if (isset($param) && $param == 'domain') { $this->isvalidcourse($this->input->post('domain')); } } function isvalidcourse($coursename) { $query = $this->db->get('admins'); $arr = $this->user->getdomains($coursename); if(in_array($coursename, $arr)) return json_encode(array('returnvalue' => 'ok')); return json_encode(array('returnvalue' => 'wrong')); }
user.php
function getdomains() { $query = $this->db->get('admins'); $arr = array(); foreach ($query->result() $row) { if ($row->subject && !(in_array($row->subject, $arr))) $arr[] = $row->subject; } return $arr; }
home_view.tpl
<div class="modal-body"> <form class="contact" name="contact"> <font color="red"><div id="errors"></div></font> <input type="text" class="input-block-level" id="coursename" placeholder="course name" name="coursename"/> <br/> <select class="input-block-level" id="domain" placeholder="domain name" name="domain" onchange="complete();"> {foreach from=$myarray item=foo} <option value="foo">{$foo}</option> {/foreach} </select> </form> </div>
change $.post
$.ajax
in order catch other errors, this:
$.ajax({ type: "post", url: "verifycourse/domain", data: {domain: $('#domain').val().tolowercase()}, success: function(msg){ alert( msg ); }, error: function(xmlhttprequest, textstatus, error) { alert("some error"); console.log(xmlhttprequest); console.log(textstatus); console.log(error); } });
Comments
Post a Comment