我有3个选择。当用户选择第三个选择并将变量发布到另一个PHP文件时,将发生jQuery onchange,因此我可以根据选择的ID检查数据库。问题是,如何获取第一个和第二个选择变量并将其与第三个选择一起发布?
这些是我使用的表:
fromlist table :
| id | fromname |
| 1 | A |
| 2 | B |
| 3 | C |
tolist table:
| id | toname |
| 1 | Z |
productlist table:
| id | name |
| 1 | CPO |
| 2 | PK |
transport table:
| id | product_id | product | to_id | from_id | price |
| 1 | 100 | 1 | 1 | 1 | 100.00 |
| 2 | 101 | 2 | 1 | 1 | 200.00 |
| 3 | 102 | 1 | 1 | 2 | 300.00 |
| 4 | 103 | 2 | 1 | 2 | 400.00 |
| 5 | 104 | 2 | 1 | 3 | 500.00 |
代码如下:
<form name = "transport" method = "GET" action = "http://interlink2u.com/transport/transportfiles/verify.php">
<select name = "phive_product_id" id = "phive_product_id" hidden></select>
<label>From:</label>
<select name = "select_from" id = "select_from" required>
<option id = "" value = "">None</option>
<?php
$fromlist = mysqli_query($connection, "SELECT * FROM fromlist ORDER BY fromname ASC");
while ($row = mysqli_fetch_assoc($fromlist))
{
$getID = $row['id'];
$getName = $row['fromname'];
?><option id = "<?php echo $getID; ?>" value = "<?php echo $getName; ?>"><?php echo $getName;?></option><?php
}
?>
</select><br><br>
<label>To:</label>
<select name = "select_to" id = "select_to" required>
<option id = "" value = "">None</option>
<?php
$tolist = mysqli_query($connection, "SELECT * FROM tolist ORDER BY toname ASC");
while ($row = mysqli_fetch_assoc($tolist))
{
$getID = $row['id'];
$getName = $row['toname'];
?><option id = "<?php echo $getID; ?>" value = "<?php echo $getName; ?>"><?php echo $getName;?></option><?php
}
?>
</select>
<br><br>
<label>Product:</label>
<select name = "select_product" id = "select_product" required>
<option id = "" value = "">None</option>
<?php
$products = mysqli_query($connection, "SELECT * FROM products ORDER BY name ASC");
while ($row = mysqli_fetch_assoc($products))
{
$getID = $row['id'];
$getName = $row['name'];
?><option id = "<?php echo $getID; ?>" value = "<?php echo $getName; ?>"><?php echo $getName;?></option><?php
}
?>
</select>
<select name = "phive_booked_price" id = "phive_booked_price" hidden></select>
<br><br>
<input type = "submit" value = "Submit" id="disablebutton">
</form><br><br><br>
这是脚本:
jQuery(document).on("click", 'select#select_product', function(e)
{
var productID = jQuery(this).children(":selected").attr("id");
var fromID = jQuery('#select_from').children(":selected").attr("id");
var toID = jQuery('#select_to').children(":selected").attr("id");
$.ajax
({
type: "POST",
data: {productID: productID, fromID: fromID, toID: toID},
url: 'http://interlink2u.com/transport/transportfiles/getprice.php',
dataType: 'json',
success: function(json)
{
var $el4 = jQuery("select#phive_product_id");
$el4.empty();
$.each(json, function(k, v)
{
$el4.append("<option id = '" + v.product_id + "'value = '" + v.product_id + "'>" + v.product_id + "</option>");
});
var $el5 = jQuery("select#phive_booked_price");
$el5.empty();
$.each(json, function(k, v)
{
$el5.append("<option id = '" + v.product_id + "'value = '" + v.price + "'>" + v.price + "</option>");
});
}
});
})
getprice.php代码:
<?php
include_once 'config/config.php';
$productID= $_POST['productID'];
$result = mysqli_query($connection, "SELECT * FROM transport
INNER JOIN tolist ON transport.to_name = tolist.id
INNER JOIN products ON transport.product = products.id
WHERE product = '" .$productID. "'");
$results = [];
while ($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
header('Content-Type: application/json');
echo json_encode($results);
?>
我想获取第一个和第二个选定的值,然后将其发布到getprice.php,以便我可以像这样查询结果:
<?php
include_once 'config/config.php';
$fromID = $_POST['fromID'];
$toID = $_POST['toID'];
$productID = $_POST['productID'];
$result = mysqli_query($connection, "SELECT * FROM transport
INNER JOIN tolist ON transport.to_name = tolist.id
INNER JOIN products ON transport.product = products.id
WHERE from_id = '" .$fromID. "'
AND to_name = '".$toID."'
AND product = '".$productID."'");
$results = [];
while ($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
header('Content-Type: application/json');
echo json_encode($results);
?>
答案 0 :(得分:0)
您仅在ajax中发送productID:
data: {productID: productID},
如果将行更改为:
data: $('form[name="transport"]').serialize(),
Read more on serialize.您的jquery调用现在看起来像:
jQuery(document).on("click", 'select#select_product', function(e)
{
$.ajax
({
type: "POST",
data: $('form[name="transport"]').serialize(),
url: 'http://interlink2u.com/transport/transportfiles/getprice.php',
dataType: 'json',
success: function(json)
{
var $el4 = jQuery("select#phive_product_id");
$el4.empty();
$.each(json, function(k, v)
{
$el4.append("<option id = '" + v.product_id + "'value = '" + v.product_id + "'>" + v.product_id + "</option>");
});
var $el5 = jQuery("select#phive_booked_price");
$el5.empty();
$.each(json, function(k, v)
{
$el5.append("<option id = '" + v.product_id + "'value = '" + v.price + "'>" + v.price + "</option>");
});
}
});
})
然后在您的getprice.php代码中需要更新:
$productID = $_POST['select_product']; // now select_product as per your selectbox name
但是您现在也可以使用:
$_POST['select_from']
$_POST['select_to']
$_POST['phive_booked_price']
$_POST['phive_product_id']
编辑:
要按照您在评论中的要求将其包括在内,您需要进行更新:
var productID = $('#select_product').val();
var fromID = $('#select_from').val();
var toID= $('#select_to').val();
答案 1 :(得分:0)
在脚本中,您正在发送如下数据:data: {productID: productID, fromID: fromID, toID: toID},
因此,在getprice.php中,您应该收到类似以下的内容:
$fromID = $_POST['fromID'];
$toID = $_POST['toID'];
$productID = $_POST['productID'];