我正在创建一个应用程序,让您设计一顿饭,添加或删除成分。但是,我遇到的问题是$ _POST数据。我遇到的问题是我需要一个按钮来提交每个 $ _POST数据表单。
例如,我可以使用查看餐点按钮列出成分(如图所示),但是由于此操作会刷新页面,因此$ _POST [meal]数据不再存在。因此,当我尝试去除一种成分时,它可以给我所选择的成分,但不能给出膳食名称。
获取$ _POST数据的正确方法是什么?我必须为每次提交做一个新的表单和按钮。我是否需要使用某种AJAX,因此它不必刷新页面而丢失了这些数据?或者我需要使用$ _GET方法吗?
<?php
if (isset($_POST['view_meal'])){
$meal = (string)$_POST['meal_names'];
$meal_fk_q = "SELECT item
FROM meal_ingredients
WHERE meal_name='$meal'
ORDER BY item";
$meal_fk_c = $conn->query($meal_fk_q);
$option_string = "";
echo "<div class='view_meal_table_wrapper'>";
while ($row = $meal_fk_c->fetch_assoc()){
$view_ingredient = $row['item'];
echo "<table class='view_meal_table'>
<tr>
<td class='view_meal cell'>$view_ingredient</td>
</tr>
</table>";
$option_string .= "<option>" . $view_ingredient . "</option>";
}
echo "</div>";
echo "<form action='createmeal.php' method='post'>
<select name='remove_ingredients'>
<option disabled selected value> -- Remove Ingredient -- </option>";
echo $option_string;
echo "</select>
<input type='submit' name='remove_ingredient' value='Remove Ingredient'>";
}
if (isset($_POST['remove_ingredient'])){
$ingr = $_POST['remove_ingredients'];
$sql = "DELETE FROM meal_ingredients
WHERE item='$ingr'";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
}
?>
答案 0 :(得分:0)
Ajax是你在这方面的前进方式。你可以添加更多的东西来改进它,但基础知识 - 使用jQuery:
var variable-to-send = 42;
$.ajax ({
type: 'POST',
url: 'php_file_location.php',
data: {name-in-post: variable-to-send, more: more, andmore: andmore}
success: function (response) {
// handle the returned details here
console.log(response);
}
});
然后在php中你做$data = $_POST ['name-in-post'];
在移动设备上输入,如果有任何错误,请道歉