基于单选按钮选择运行mysql查询的PHP代码,无需提交
我有三个单选按钮。我想选择一个单选按钮值并将该选择传递给mysql查询。我不想使用提交按钮。 例如:如果我选择卡车或汽车,我希望将值传递到where条件基于单选按钮选择运行mysql查询的PHP代码,无需提交,php,html,mysql,radio-button,Php,Html,Mysql,Radio Button,我有三个单选按钮。我想选择一个单选按钮值并将该选择传递给mysql查询。我不想使用提交按钮。 例如:如果我选择卡车或汽车,我希望将值传递到where条件 <form action="example.php" method="post"> <input type="radio" name="vehicle" value="All Vehicles" checked> All Vehicles <input type="radio" name="vehi
<form action="example.php" method="post">
<input type="radio" name="vehicle" value="All Vehicles" checked> All Vehicles
<input type="radio" name="vehicle" value="Truck"> Truck
<input type="radio" name="vehicle" value="Cars"> Cars
</form>
<?php
$type = $_POST['Vehicle'];
if ($type == "All Vehicle") {
$sql ='select distinct reservation_id,Vehicle,rental_price from vehicle_reservation';
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
}
else ($answer == "Truck") {
$sql ='select distinct reservation_id,Vehicle,rental_price from vehicle_reservation where v.type="Truck"';
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
}
?php
所有车辆
卡车
汽车
首先,让我们澄清一下,您的php代码在服务器上运行,html和任何可以使用的javascript都在web浏览器上运行
话虽如此,很明显,在浏览器中进行的选择必须以某种方式到达服务器,才能包含在查询中
有两种方法可以做到这一点:
1) 使用按钮或其他javascript事件以表单形式提交数据
2) 使用ajax调用,与javascipt事件关联,该事件也在后台提交数据。在这里,您可以选择将其作为表单或json等其他内容发送。使用jquery ajax编辑的代码尝试此操作
<?php
//replace connection variables with yours.
//hope you store Vehicle with names in vehicle_reservation table
$conn = mysqli_connect('hostname','username','password','database');
if(!$conn){
$response = array('success'=>0,'data'=>'unable to connect with db');
}else{
$type = $_POST['Vehicle'];
$where = ($type != "All Vehicle")?" WHERE Vehicle='".$type."'":"";
$sql ='select distinct reservation_id,Vehicle,rental_price from vehicle_reservation'.$where;
$query = mysqli_query($conn, $sql);
if (!$query) {
$response = array('success'=>0,'data'=>mysqli_error($conn));
}else{
if(mysqli_num_rows($query)>0){
$data = mysqli_fetch_array($query);
$response = array('success'=>1,'data'=>$data);
}else{
$response = array('success'=>0,'data'=>'No records found');
}
}
}
echo json_encode($response);
?>
在html文件中
<script>
$(document).on('click','input[name="Tool"]:radio',function() {
var val = $(this).val();
console.log(val);
$.ajax({
type:"POST",
url:"C:\example.php",
data:'Vehicle='+val,
success:function(response) {
console.log(response);
}
});
})
</script>
$(文档)。在('单击','输入[name=“Tool”]:收音机',函数()上{
var val=$(this.val();
控制台日志(val);
$.ajax({
类型:“POST”,
url:“C:\example.php”,
数据:'Vehicle='+val,
成功:功能(响应){
控制台日志(响应);
}
});
})
您不能这样做,您必须提交页面。但是,您可以将PHP移植到单独的脚本中,并在单击单选按钮时使用AJAX执行该脚本。if($type==“All Vehicle”)
将永远不会计算。在添加jquery代码段时,我是否需要对代码的PHP部分进行任何更改?用php代码创建新的php文件并回显所需内容。使用表单从页面中删除代码。将新php文件的路径添加到ajax函数HTML文件AllVehicles Truck$(document).on('click','input[name=“Tool”]:radio',function(){var val=$(this.val();console.log(val);$.ajax({type:'POST',url:'C:\example.php),数据:'selected_radio='+val,success:function(response){console.log(response);}}}}PHP文件我对PHP不熟悉。当我运行html文件时,我只看到单选按钮,当我选择all vehicle radio时,它不会从数据库返回任何值。你能告诉我我错过了什么吗