我在一个页面上有多个表单,并且在另一个名为editmenusprocess.php
的页面中处理这些表单。
当我尝试提交个人表单时,信息被放入数据库中。
但是,所有其他表单也是以空值提交的。
我试过使用
if ($_POST['action'] == 'starter')
作为变通办法。
我认为我没有正确地实现它。
我在一个页面中有几个这样的表单。
<?php
if ($_POST['action'] == 'starter') {
}
else if($_POST['action'] == 'main' ){
}
else if($_POST['action'] == 'desert' ){
}
else if($_POST['action'] == 'menus' ){
}
?>
<div class='container-fluid bg-color' style='margin-bottom:20px'>
<div class='row justify-content-center '>
<div class='col-md-4'>
<form action='editmenusprocess.php?action=starter' method='POST'>
<legend>
<h3 style='margin-top: 20px;'>Add Starter</h3>
</legend>
<div class='form-group '>
<label for='exampleInputEmail1'>Food:</label>
<input type='text' class='form-control form-control-lg' name='msfood' placeholder='Title'>
</div>
<div class='form-group '>
<label for='exampleInputEmail1'>Description:</label>
<input type='text' class='form-control form-control-lg' name='msdescript' placeholder='Title'>
</div>
<div class='form-group'>
<label for='exampleInputEmail1'>Price:</label>
<input type='text' class='form-control form-control-lg' name='msprice' placeholder='Info'>
</div>
<button type='submit' class='btn btn-primary bt'>Submit</button>
</form>
</div>
此代码位于流程页面上。
$msid = mysqli_real_escape_string($link, $_POST["msid"]);
$msfood = mysqli_real_escape_string($link, $_POST["msfood"]);
$msdescript = mysqli_real_escape_string($link, $_POST["msdescript"]);
$msprice = mysqli_real_escape_string($link, $_POST["msprice"]);
$msinsertquery = "INSERT INTO 2043menustarter (id, food, descript,price) VALUES (null, '$msfood','$msdescript','$msprice')";
$msresult = mysqli_query($link, $msinsertquery) or die (mysqli_error($link));
我已经使用($_POST['action'] == 'starter')
方法解决了这个问题,表单现在将数据插入到相应的表中,但在提交时仍会弹出一些警告。
这是现在编辑/表单页面的代码:
<form action ='editmenusprocess.php' method = 'POST'>
<legend><h3 style = 'margin-top: 20px;'>Add Starter</h3></legend>
<div class='form-group '>
<label for='fd1'>Food:</label>
<input type='text' class='form-control form-control-lg' name='msfood' id="fd1">
<input type="hidden" name="action" value="starter">
</div>
<div class='form-group '>
<label for='fd2'>Description:</label>
<input type='text' class='form-control form-control-lg' name='msdescript' id="fd2">
<input type="hidden" name="action" value="starter">
</div>
<div class='form-group'>
<label for='fd3'>Price:</label>
<input type='text' class='form-control form-control-lg' name = 'msprice' id=fd3>
<input type="hidden" name="action" value="starter">
</div>
<button type='submit' class='btn btn-primary bt'>Submit</button>
</form>
</div>
<div class= 'col-md-4'>
<form action ='editmenusprocess.php' method = 'POST'>
<legend><h3 style = 'margin-top: 20px;'>Add Main</h3></legend>
<div class='form-group '>
<label for='st1'>Food:</label>
<input type='text' class='form-control form-control-lg' name='mmfood' id="st1">
<input type="hidden" name="action" value="main">
</div>
<div class='form-group '>
<label for='st2'>Description:</label>
<input type='text' class='form-control form-control-lg' name='mmdescript' id='st2'>
<input type="hidden" name="action" value="main">
</div>
<div class='form-group'>
<label for='st3'>Price:</label>
<input type='text' class='form-control form-control-lg' name = 'mmprice' id='st3'>
<input type="hidden" name="action" value="main">
</div>
<button type='submit' class='btn btn-primary bt'>Submit</button>
</form>
下面是process页面上的代码:
$action = (!empty($_POST["action"]))?$_POST["action"]:null;
$msfood = mysqli_real_escape_string($link, $_POST["msfood"]);
$msdescript = mysqli_real_escape_string($link, $_POST["msdescript"]);
$msprice = mysqli_real_escape_string($link, $_POST["msprice"]);
$mmfood = mysqli_real_escape_string($link, $_POST["mmfood"]);
$mmdescript = mysqli_real_escape_string($link, $_POST["mmdescript"]);
$mmprice = mysqli_real_escape_string($link, $_POST["mmprice"]);
if($action == 'starter'){
$stmt = $link->prepare("INSERT INTO 2043menustarter (id,food, descript, price) VALUES (null, '$msfood','$msdescript','$msprice')");
$stmt->bind_param('sss',$_POST["msfood"], $_POST["msdescript"], $_POST["msprice"]);
$stmt->execute();
$stmt->close();
}else if($action == 'main'){
$stmt = $link->prepare("INSERT INTO 2043menumain (id,food, descript, price) VALUES (null, '$mmfood','$mmdescript','$mmprice')");
$stmt->bind_param('sss',$_POST["mmfood"], $_POST["mmdescript"], $_POST["mmprice"]);
$stmt->execute();
$stmt->close();
}
发布于 2017-12-13 01:43:28
如果您希望将action
作为POST读取,请尝试将其放入隐藏输入中。其中一个表单将如下所示:
<form action="editmenusprocess.php" method="POST">
<legend><h3 style="margin-top: 20px;">Add Starter</h3></legend>
<div class="form-group">
<label for="exampleInputEmail1">Food:</label>
<input type="text" class="form-control form-control-lg" name="msfood" placeholder="Title">
<input type="hidden" name="action" value="starter">
</div>
<!-- THE REST OF THE INPUT FIELDS BELOW -->
对你的其他表单做剩下的事情。
然后,在您的editmenusprocess.php中:
$action = (!empty($_POST["action"]))?$_POST["action"]:null;
if($action == 'starter'){
$stmt = $link->prepare("INSERT INTO 2043menustarter (food, descript, price) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $_POST["msfood"], $_POST["msdescript"], $_POST["msprice"]);
$stmt->execute();
$stmt->close();
}
/*** THEN THE REST OF YOUR OTHER CONDITIONS BELOW ***/
在你的帖子的评论部分,人们一直建议你至少使用预准备语句。你可以阅读更多关于它的here。
发布于 2017-12-12 22:17:17
操作值编码在查询字符串中。
PHP将查询字符串解析为$_GET
超级全局。只有请求体中的数据才会被解析到$_POST
超级全局中。
使用$_GET['action']
或将数据从查询字符串移动到输入。
发布于 2017-12-13 02:36:50
如果您希望将字段单独发送到其他脚本,则有两个选择。
1.将您的输入字段放入他们自己的表单中(使用他们自己的提交按钮)
<form method="post" action="somescript.php">
<input type="text" name="input1"><button type="submit">save</button>
</form>
<form method="post" action="somescript.php">
<input type="text" name="input1"><button type="submit">save</button>
</form>
2.使用javascript或jQuery只发送非空的数据。
$('form').submit(function(e){
e.preventDefault();
// clean your data
var yourCleanedData = [];
$('form input').each(functionn(idx, elm){
if (elm.value){
yourCleanedData[elm.name] = elm.value;
}
});
$.ajax({
url: 'somescript.php',
type: 'POST',
data: yourCleanedData,
})
});
https://stackoverflow.com/questions/47782256
复制相似问题