在下面的代码中,当我在php工作时,下拉列表运行良好,当选择考试名称相应course_code来了,当我选择相应的subject_code来了。但是现在我创建了控制器并在我选择exam_name时调用codeigniter中的视图没有填充值,当我点击上传它显示course_code值而不填充subject_coide时,当我点击上传它填充subject_code。所以任何人请帮助我。
Upload1_site (controller)
<?php
class Upload1_site extends ci_controller
{
function index()
{
$this->load->view('new_view');
}
function upload()
{
$this->load->view('new1_view');
}
}//end of class ?>
new_view (View 1)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".hai").change(function()
{
var id=$(this).val();
// Please find the course_code, course_code was not found
var dataString = 'course_code='+ id;
$.ajax
({
type: "POST",
url: "upload1_site/upload",
data: dataString,
cache: false,
success: function(html)
{
$(".hai2").html(html);
}
});
});
$(".hai2").change(function()
{
var id2=$("#hai2").val();
alert(id2);
var dataString = 'subject_code='+ id2;
$.ajax
({
type: "POST",
url: "upload1_site/upload",
data: dataString,
cache: false,
success: function(html)
{
$(".hai3").html(html);
}
});
});
});
</script>
</head>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
File to import:
<input size='30' type='file' name='filename'>
<br>
Select Exam name:
<select name="hai" class="hai" id="hai">
<?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("slseatapp") or die(mysql_error());
$query="select distinct exam_name from examcourse";
$result = mysql_query($query);
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['exam_name']."'>".$nt['exam_name']."</option>";
}
?>
</select>
<br>
<span class="hai2">
Course code:
<select name="hai2" id="hai2">
<?php if($_REQUEST['hai']){?>
<option>Select</option>
<?php
$query="SELECT distinct course_code FROM examcourse where exam_name = '".$_REQUEST['hai']."' ";
$result = mysql_query($query);
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
}
?>
<?php }else{?>
<option>Select</option>
<?php }?>
</select>
</span>
<br>
<span class="hai3">
Subject code:
<select name="hai3" id="hai3">
<?php if($_REQUEST['hai2']){?>
<option>Select</option>
<?php
$query="SELECT distinct subject_code FROM coursesubject where course_code = '".$_REQUEST['hai2']."'";
$result = mysql_query($query);
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['subject_code']."'>".$nt['subject_code']."</option>";
}
?>
<?php }else{?>
<option>Select</option>
<?php }?>
</select>
</span>
<br>
<input type="submit" name="submit" value="Upload"></form>
<?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("slseatapp") or die(mysql_error());
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//Import uploaded file to Database
$row = 1;
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$var = $_POST['hai'];
$var2 = $_POST['hai2'];
$var3 = $_POST['hai3'];
//$res=mysql_fetch_array(mysql_query("SELECT subject_code FROM coursesubject where course_code = '".$var1."'"));
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//Update Database Values
$import="insert into student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var2','".$var3."','$var')";
//$import="replace into student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var','$var1','$var2')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
echo"<script>alert('Uploaded Successfully');</script>";
}else{
echo"<script>alert('Failed');</script>";
}
}
?>
new2_view (view 2)
<script language="javascript">
function changeSelection(value){
var length = document.getElementById("hai3").options.length;
if(value == 0){
for(var i = 1;i<length;i++)
document.getElementById("hai3").options[i].selected = "selected";
document.getElementById("hai3").options[0].selected = "";
}
}
</script>
<?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("slseatapp") or die(mysql_error());
if($_POST['course_code']){
@$exam_name=$_POST['course_code'];
?>
Course code: <select name="hai2" id="hai2">
<option >Select</option>
<?php
$query="SELECT distinct course_code FROM examcourse where exam_name = '$exam_name' ";
$result = mysql_query($query);
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
}
?>
</select>
<?php }
if($_POST['subject_code']){
@$subject_code=$_POST['subject_code'];
?>
Subject code:
<select name="hai3" multiple onChange="changeSelection(this.value)" id="hai3">
<option value="0">Select</option>
<?php
$query="SELECT subject_code FROM coursesubject where course_code = '".$subject_code."'";
$result = mysql_query($query);
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['subject_code']."'>".$nt['subject_code']."</option>";
}
?>
</select>
<?php }?>
答案 0 :(得分:1)
尝试这个
把这一个
var base_url="<?=base_url()?>"
脚本标签内的某处可能在
之前$(document).ready(function(){
线
并更改行
url: "upload1_site/upload",
与
url: base_url+"upload1_site/upload",
如果有效,请告诉我。