コンテンツをデータベースに保存する際にいくつか問題があります。テーブル/行はjqueryを使用して動的に生成されます! しかし、送信ボタンをクリックすると、テキスト値を保存してもデータベーステーブルに数字の行だけが保存されます。これが私のjqueryコードです:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = "<input type='text' name='Obstacle'>";
var cell3 = row.insertCell(2);
cell3.innerHTML = "<select name='Likelihood'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
var cell4 = row.insertCell(3);
cell4.innerHTML = "<select name='Severity'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
var cell5 = row.insertCell(4);
cell5.innerHTML = "<select name='Priority'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
}
これが私のhtmlです:
<div class="compTable">
<form action="" method="post">
<TABLE width="425" border="1">
<thead>
<tr>
<th width="4"></th>
<th width="100">Obstacle</th>
<th width="50">Likelihood</th>
<th width="50">Severity</th>
<th width="50">Priotity</th>
</tr>
</thead>
<tbody id="dataTable">
</tbody>
</TABLE>
<INPUT type="submit" value="Save" name="submit" />
<INPUT type="button" value="Add Obstacle" onClick="addRow('dataTable')" />
<INPUT type="button" value="Delete Obstacle" onClick="deleteRow('dataTable')" />
</form>
</div>
そして最後に私のphpコード:
if(isset($_POST['submit'])){
$user = $_SESSION['user']['FullName'];
$obstacleDec = isset($_POST["Obstacle"][$key]);
$likelihoodScale = isset($_POST["Likelihood"][$key]);
$severityScale = isset($_POST["Severity"][$key]);
$priorityScale = isset($_POST["Priority"]);
foreach ($_POST as $key => $value){
$sql = "INSERT INTO obstacles (ObstacleID, ObstacleDescription, Priority, Uncertainty, Severity, UserName, ComplianceID, Accepted)
VALUES (:ID, :obstacleDec, :priorityScale, :likelihoodScale, :severityScale, :aUser, :compID, :accepted)";
try{
$stmt = $db->prepare($sql);
$result= $stmt->execute(array(':ID'=>'', ':obstacleDec'=>$obstacleDec,':likelihoodScale'=>$likelihoodScale, ':severityScale'=>$severityScale,':priorityScale'=>$priorityScale, ':aUser'=>$user, ':compID'=>'', ':accepted'=>'' ));
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
header("Location: obstacles.php");
die("Redirecting to obstacles.php");
if(!$sql){
die("error inserting values");
}
else{
die("successfully inserted the values");
}
}
}
助けてくれてありがとう