【发布时间】:2015-06-19 09:52:48
【问题描述】:
我有一个包含课程名称的下拉列表(例如“php”、“c”、“c#”...)。 我还有一个 onchange 事件,以便我可以检查下拉列表中的选项是否有效(在我的数据库中找到) - 它可能看起来很愚蠢,因为我自己将这些值放在从我的数据库中下拉,但我这样做是为了防止 SQL 注入。
问题是我没有收到 verifycourse.php 的回复...所以这是我做错了。
function.js
function complete() {
$.post("verifycourse/domain", {
domain: $('#domain').val().toLowerCase()
}).done(function (returnValue) {
alert('after post'); //it does 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() as $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>
【问题讨论】:
-
错误或拼写错误?
f(in_aray($courseName, $arr))应该是in_array -
不敢相信...错误,但仍然没有提醒
-
您没有收到任何警报吗?从来没有
alert("after post")? -
连那个都没有……
-
这个
$smarty = new Mysmarty;不需要是这个$smarty = new Mysmarty();吗?
标签: javascript php jquery json codeigniter