我正在使用PHP,mysql和ajax。我有一个名为Billing.php
的php文件,其中有2个下拉列表,其中一个名为Company
,另一个名为Model Name
。
我可以使用sql查询在下拉列表中显示所有公司名称和模型名称。
但现在它获取了下拉列表中的所有可用数据。我担心的是,我想选择一个公司名称,第二个下拉列表应该根据公司过滤模型名称。
在网上寻找一些解决方案之后我才知道我们可以使用ajax,但我不知道我的代码中缺少什么。它也给了我错误
Notice: Undefined index: ajaxcompanyname in C:\Users\Shesharm\MyWebsite\Billing.php
其中ajaxcompanyname
是从ajax函数传递到php文件的数据,我用它来获取模型名称下拉列表的查询结果。
Billing.PHP
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style>
table,th,td{
border: 1px solid black;
border-collapse: collapse;
text-align: left;
table-layout: auto;
background-color: #CCCCCC;
}
</style>
<script type="text/javascript" src="Billing_Selection.js"></script>
</head>
<body>
<?php
//Define variables
$company=$ModelName=$ModelNo=$productcolor=$cost=$sql1=$sql2=$IMEI=$accessories=$row=$quantity=$customername=$customerinfo=$prodstatus="";
$quantityerr=$q="";
?>
<form name="SearchMobile" method="POST" action="<?php $_SERVER['PHP_SELF'];?>" style="background-color:#FF6600;">
<fieldset>
<legend><b>Search the Required mobile</b> </legend>
<?php
// Connect to database and get the no of rows in $num
$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
$sql1 = "select DISTINCT CompanyName from mobile_data";
//check connection
if(!$con){
echo "Failed to connect to MySQL: " . mysql_error();
}
$retval1 = mysqli_query($con,$sql1);
echo "<select name='companyname' onchange='companysortlist(this.value)'>";
echo "<option value=''>Select a Mobile:</option>";
while($row = mysqli_fetch_array($retval1)){
echo "<option value='".$row['CompanyName']."'>".$row['CompanyName']."</option>";
}
echo "</select>"." ";
//******************* Select the model name..
$sql2 = "select DISTINCT ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
//check connection
if(!$con){
echo "Failed to connect to MySQL: " . mysql_error();
}
echo "<select name='modelname' class='companymodelname'>";
echo "<option value=''>Select a Model Name:</option>";
$retval1 = mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($retval1)){
echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
}
echo "</select>"." ";
mysqli_close($con);
?>
</fieldset>
</form>
<br>
</body>
</html>
Billing_Selection.js
function companysortlist(data){
$.ajax({
type:'POST',
url:'Billing.php',
data:"ajaxcompanyname="+data,
success:function(data){
$('.companymodelname').html(data);
}
}
)
}
请让我知道我做错了什么。我是初学者。
答案 0 :(得分:0)
Ajax允许您在 页面加载后向服务器 发出更多请求,因此名称为ajax:
A 同步 J avascript A nd X ml
您正在获得Undefined index: ajaxcompanyname
因为POST
变量ajaxcompanyname
未在初始页面加载时设置,后来的ajax请求会附带。{/ p>
你可以向最初加载页面的同一网址发出ajax请求,但需要额外的逻辑来确定请求是否是ajax请求。我建议反对它并为你创建一个单独的文件ajax请求,例如:
<强> ajaxcompanyname.php 强>
看起来像:
$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
//******************* Select the model name..
$sql2 = "select DISTINCT ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
//check connection
if(!$con){
echo "Failed to connect to MySQL: " . mysql_error();
}
echo "<select name='modelname' class='companymodelname'>";
echo "<option value=''>Select a Model Name:</option>";
$retval1 = mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($retval1)){
echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
}
echo "</select>"." ";
mysqli_close($con);
然后你会改变
url:'Billing.php',
要
url:'ajaxcompanyname.php',
并从Billing.php
修改强>
我想我应该提一下,我没有尝试修复代码中的任何SQL注入漏洞。这应该是look into
答案 1 :(得分:0)
简化答案:
我省略了一些数据库检查等以保持简短。
<强> Billing.php 强>
<?php
$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
$sql = "select DISTINCT CompanyName from mobile_data";
$retval = mysqli_query($con,$sql);
?>
<form method="POST">
<select name="companyname" onchange="companysortlist(this.value)">
<option value=''>Select a Mobile:</option>
<?php
while($row = mysqli_fetch_array($retval)){
echo "<option value='".$row['CompanyName']."'>".$row['CompanyName']."</option>";
}
?>
</select>
<select name="modelname" class="companymodelname">
<!-- note the select is completely empty, it will be fill by ajax -->
</select>
</form>
<script>
function companysortlist(data){
$.ajax({
type: 'POST',
url: 'ajax.php',
data: "ajaxcompanyname=" + data,
success: function (data) {
$('.companymodelname').html(data);
}
});
}
</script>
<强> ajax.php 强>
<?php
$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
$sql = "select DISTINCT ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
$retval = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($retval)){
echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
}
?>
请注意
我仍然没有修复代码中的任何SQL注入漏洞