我是PHP的新手,我希望有人可以帮助我。我有4个PHP文件,它们基本上是用户填写的表单,然后系统会将其导航到验证页面,用户将单击“提交”以保存它。我在复选框部分有问题。
错误显示:
注意:C:\ xampp \ htdocs \ LM \ LMvalidate_reservation.php中的数组到字符串转换
我已将代码简化为仅显示复选框部分以便于理解。我希望有人可以帮助我。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="LMvalidate_reservation.php" method="post">
<div class="col-md-4"><b>Please check (√ ) the module(s) that you want to attend:</b><br></div>
<div class="col-md-8">
<input type="checkbox" class="get_value" value="WEBOPAC Usage">WEBOPAC Usage<br>
<input type="checkbox" class="get_value" value="Accessing Online Database Skill">Accessing Online Database Skill<br>
<input type="checkbox" class="get_value" value="E-Books and E-Journals Accession">E-Books and E-Journals Accession<br>
<input type="checkbox" class="get_value" value="Digital Collection Accession">Digital Collection Accession<br>
<input type="checkbox" class="get_value" value="EQPS Exam Papers">EQPS Exam Papers<br>
<input type="checkbox" class="get_value" value="Information Searching Strategy">Information Searching Strategy<br>
<input type="checkbox" class="get_value" value="SCOPUS & Web Of Science Usage Skill">SCOPUS & Web Of Science Usage Skill<br>
<input type="checkbox" class="get_value" value="Reference Management Software (EndNote & Mendeley)">Reference Management Software (EndNote & Mendeley)<br>
<input type="checkbox" class="get_value" value="UiTM Institutional Repository (Thesis & Dissertation)">UiTM Institutional Repository (Thesis & Dissertation)<br>
<input type="checkbox" class="get_value" value="Digital Map">Digital Map<br>
<input type="checkbox" class="get_value" value="E-Newspaper (BLIS (Bernama Library & Infolink Service)">E-Newspaper (BLIS (Bernama Library & Infolink Service))<br>
<input type="checkbox" class="get_value" value="Facility">Facility<br><br>
</div>
<script>
$(document).ready(function(){
$('#submit').click(function(){
var insert = [];
$('.get_value').each(function(){
if($(this).is(":checked"))
{
insert.push($(this).val());
}
});
insert = insert.toString();
$.ajax({
url: "insert.php",
method: "POST",
data:{insert:insert},
success:function(data){
$('#result').html(data);
}
});
});
});
</script>
<input type="submit" name="LMreservation_form" value="Submit">
</form>
<?php
if (isset($_POST['LMreservation_form'])) {
$module = "";
$count_error = 0;
$msg = "";
// validate if submitted variables empty show error msg else put in local variables
}
if (isset($_POST['module']) && ($_POST['module'] != ""))
$module = $_POST['module'];
else
{
$msg .= "Error: Please select your module.<br>";
$count_error++;
}
if ($count_error > 0) {
echo $msg;
echo "$count_error error(s) detected.";
die();
// display error(s) here and stop
}
}
else {
echo "Error: You have execute a wrong PHP. Please contact the web administrator.";
die();
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="LMsave_reservation.php" method="post">
<table>
<tr>
<td class="col-md-4 col-xs-4">Module:</td>
<td class="col-md-8 col-xs-8"><?php print $module; ?><input type="hidden" name="module" value="<?php echo $module; ?>"></td>
</tr>
</table><br>
<input type="submit" name="LMreservation_validate" value="Save My Reservation">
</form>
</body>
</html>
<?php
if (isset($_POST['LMreservation_validate'])) {
// Connection variables
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lm_reservation";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare the SQL statement
$stmt = $conn->prepare("INSERT INTO lmreservation(name, studentstaffid, faculty, contactno, email, program, participant, attandance, module, date, starttime, endtime) VALUES (:name, :studentstaffid, :faculty, :contactno, :email, :program, :participant, :attandance, :module, :date, :starttime, :endtime)");
// Bind the parameters
$stmt->bindParam(':module', $module, PDO::PARAM_STR);
// insert a row
$module = $_POST['module'];
$stmt->execute();
echo "Your application is successful. Have a nice day! :)";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
<?php
if(isset($_POST["insert"]))
{
$conn = mysqli_connect("localhost", "root", "", "lm_reservation");
$query = "INSERT INTO lmreservation(modules) VALUES ('".$_POST["insert"]."')";
$result = mysqli_query($conn, $query);
}
?>
答案 0 :(得分:3)
首先,为了获得$module
的数组,需要使用数组语法(有关更多信息,请参阅this section of the PHP documentation)。为此,每个复选框输入都需要添加值为{em> module [] 的name属性,如下面的标记所示:
<input type="checkbox" name="module[]" class="get_value" value="WEBOPAC Usage">WEBOPAC Usage<br>
<input type="checkbox" name="module[]" class="get_value" value="Accessing Online Database Skill">Accessing Online Database Skill<br>
<!-- repeated for all other checkboxes -->
然后在 LMvalidate_reservation.php 中,$module
将是一个数组(有关更多信息,请参阅this answer)。为了正确打印出数组中的每个元素(即在表单中检查的每个值),使用类似print_r()的函数或使用 foreach
<table>
<?php
foreach($module as $moduleItem) {
echo '<tr>
<td class="col-md-4 col-xs-4">Module:</td>
<td class="col-md-8 col-xs-8">'.$moduleItem.'<input type="hidden" name="module" value="'.$moduleItem.'"></td>
</tr>';
}?>
</table>
请参阅this phpfiddle中的演示。请注意,因为只允许一个PHP文件,所以LMreservation.php和LMvalidate_reservation.php中的代码已合并。
这是因为表单(即<form action="LMvalidate_reservation.php" method="post">
)以标准方式提交(即不是异步,就像jQuery AJAX代码尝试做的那样)。
此外,如果您希望jQuery AJAX代码有效,则提交按钮需要设置id attribute。所以这行在reservation.php中
<input type="submit" name="LMreservation_form" value="Submit">
需要像这样更新:
<input id="submit" type="submit" name="LMreservation_form" value="Submit">
这样点击处理程序(即$('#submit').click(function(){...}
)将绑定到DOM中存在的元素的点击。
答案 1 :(得分:0)
在该文件中使用For循环LMvalidate_reservation.php
<?php
if (isset($_POST['LMreservation_form'])) {
$module = "";
$count_error = 0;
$msg = "";
// validate if submitted variables empty show error msg else put in local variables
}
if (isset($_POST['module']) && ($_POST['module'] != ""))
$module = $_POST['module'];
else
{
$msg .= "Error: Please select your module.<br>";
$count_error++;
}
if ($count_error > 0) {
echo $msg;
echo "$count_error error(s) detected.";
die();
// display error(s) here and stop
}
}
else {
echo "Error: You have execute a wrong PHP. Please contact the web administrator.";
die();
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="LMsave_reservation.php" method="post">
<table>
<?php foreach($module as $element) { ?>
<tr>
<td class="col-md-4 col-xs-4">Module:</td>
<td class="col-md-8 col-xs-8"><?php print $element; ?><input type="hidden" name="module" value="<?php echo $element; ?>"></td>
</tr>
<?php } ?>
</table><br>
<input type="submit" name="LMreservation_validate" value="Save My Reservation">
</form>
</body>
</html>