【发布时间】:2017-05-31 04:57:35
【问题描述】:
我是 PHP 新手,希望有人能帮助我。我有 4 个 PHP 文件,它们基本上是供用户填写的表单,然后系统将导航到验证页面,用户将单击提交保存。我在复选框部分有问题。
错误显示:
注意:C:\xampp\htdocs\LM\LMvalidate_reservation.php 中的数组到字符串的转换
为了便于理解,我已简化代码以仅显示复选框部分。我希望有人可以帮助我。
LMreservation.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>
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>
<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>
LMsave_reservation.php
<?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
<?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);
}
?>
【问题讨论】:
-
所以
checkbox输入的名称是module??.,如果在PHP /HTML中通常是这样,所有相关的复选框都应该具有相同的名称,并且当您在PHP中取复选框值时在 ``POST, it will bean Array. In your LMvalidate_reservation.php , you are attempting to print$module` 期间,该行中有一个数组<?php print $module; ?>,这就是您收到 Array to String 转换错误的原因。解决方案是您应该使用print_r来查看数组元素。 -
是的,checkbox输入的名字是module。那我该怎么办?
-
为什么在
LMsave_reservation.php和insert.php页面上使用PDO和mysqli_connect()?您应该建立一个连接来简化您的代码。你所拥有的不是很容易维护。 -
它可以工作,但只添加第一个检查值..
-
@Rasclat 我知道这有问题,但我不知道如何编辑它。
标签: javascript php jquery database checkbox