我一直在研究这段代码,显然每次加载页面时,它都会向数据库中插入数据。
<!DOCTYPE html>
<?php
include_once($_SERVER["DOCUMENT_ROOT"] .'/31783/functions.php');
$questions="";
$activityname="";
$myarray=array();
if($_SERVER["REQUEST_METHOD"] == "POST") {
print_r($myarray);
print_r($activityname);
$questions = $_POST['myquestions'];
$activityname = $_POST['activityname'];
$myarray = array_filter(explode("|||", $questions));
SqlRecord("INSERT INTO activity (name) VALUES ('$activityname')");
foreach ($myarray as $value) {
$connector = array();
$connector = array_filter(explode("!!!", $value));
$question = $connector[0];
$answer = $connector[1];
$activityid = SelectRecord("activity_id", "SELECT activity_id FROM activity WHERE name='$activityname'");
SqlRecord("INSERT INTO question(activity_id,question,answer) VALUES (" . $activityid . ",'$question','$answer')");
}
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Add / Remove Table Rows</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<style type="text/css">
tr:hover{
cursor: move;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var htm='<tr>\n' +
' <td><textarea name="name" required></textarea></td>\n' +
' <td><button class="remove"></button></td>\n' +
'<td><select id="myAnswer" name="myAnswer" class="form-control"><option value="1">True</option><option value="2">False</option></select></td>'+
' </tr>';
$(function () {
$('tbody').sortable();
$('#addRow').on( "click", function(){
$('tbody').append(htm);
});
$(document).on('click','.remove',function(){
$(this).parents('tr').remove();
});
$('#getValues').on( "click", function(){
var myValues="";
var values=[];
$('textarea[name="name"]').each(function(i,elem){
values.push({
question:$(elem).val(),
answer:""
});
});
$('select[name="myAnswer"').each(function(i,elem) {
values[i].answer=$(elem).val();
});
for (var j=0;j<values.length;j++) {
myValues=myValues+values[j].question+"!!!"+values[j].answer+"|||";
}
//console.log(myValues);
$("#myquestions").val(myValues);
});
});
</script>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="container">
<input type="text" id="activityname" name="activityname" required></br>
<table>
<tbody>
<tr>
<td><textarea name="name" required></textarea></td>
<td><button class="remove"></button></td>
<td><select name="myAnswer" class="form-control"><option value="1">True</option><option value="2">False</option></select></td>
</tr>
<tr>
<td><textarea name="name" required></textarea></td>
<td><button class="remove"></button></td>
<td><select name="myAnswer" class="form-control"><option value="1">True</option><option value="2">False</option></select></td>
</tr>
</tbody>
</table>
<input id="getValues" type="submit" value="Submit"/>
<input type="text" id="myquestions" name="myquestions">
<button id="addRow">Add</button>
</div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</body>
</html>
我无法在代码中发现错误。
每次我按添加时,代码都会添加<tr>
。它将<tr>
中的值传输到数组,然后将其作为字符串发送到html中的<input>
,以便能够在php中读取并将其插入数据库中。我没有使用ajax,因为代码简单明了。
谢谢
答案 0 :(得分:0)
如果您替换
XACT_STATE()
为
if($_SERVER["REQUEST_METHOD"] == "POST") {
应该可以,idk为什么您的$ _SERVER验证不起作用,对不起