gpt4 book ai didi

php - 数据在页面加载时插入数据 - php jquery

转载 作者:行者123 更新时间:2023-11-28 02:27:24 25 4
gpt4 key购买 nike

<分区>

我一直在研究这段代码,显然每次我加载页面时它都会将数据插入数据库。

<!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> 中的值到一个数组然后它将它作为一个字符串发送到一个<input>在 html 中,以便能够在 php 中读取它并将其插入数据库。我没有使用 ajax,因为代码简单明了。

谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com