我使用一个addEventListener单击事件来触发一个新的GPT3请求,该请求将最新的数据库帖子作为文本完成的提示。
数据通过隐藏的html表单发送到数据库,表单的操作触发了php脚本。
然后将新生成的文本发送到数据库,数据库将成为下一个GPT3请求等的提示。
问题是事件只触发一次,我希望能够在加载页面中继续生成新的文本,而页面中的其他数据重文件则加载。
<form name="GPT3commentForm" id="GPT3commentForm" display="none" action="/insert_GPT3.php" method="post" autocomplete="off">
<input type="hidden" name="GPT3_field1" id="GPT3Text"/></form>
<?php
require_once './includes/Openai.php';
$openai = New Openai();
//Engine, prompt and max tokens - takes the last entry of the database array as the prompt
$openai->request("davinci", end($stack), 100);
$latestEntry = end($stack);
$latestEntry = JSON_encode($latestEntry);
?>
<script>
var data = <?php echo json_encode($response, JSON_HEX_TAG); ?>; // Don't forget the extra semicolon!
//turn the string into an object
const dataObj = JSON.parse(data);
console.log(typeof dataObj);
//access only the text completion section...
console.log(dataObj.choices[0].text);
GPT3Array = [];
newData = [];
newDataObj = [];
currentGPT3Text = 0;
GPT3Array[currentGPT3Text] = dataObj.choices[0].text;
document.getElementById('GPT3Text').value = GPT3Array[currentGPT3Text];
const GPT3symbolSpan = document.createElement("span");
const GPT3link = document.createElement("a");
GPT3link.textContent = GPT3Array[currentGPT3Text];
GPT3symbolSpan.appendChild(GPT3link);
GPT3LoadingText.appendChild(GPT3symbolSpan);
document.getElementById('GPT3commentForm').submit();
var generateText = function (event) {
currentGPT3Text += 1;
<?php
require_once './includes/Openai.php';
$new_openai = New Openai();
//Engine, prompt and max tokens - takes the last entry of the database array as the prompt
$new_openai->request("davinci", end($stack), 100);
$latestEntry = end($stack);
$latestEntry = JSON_encode($latestEntry);
//unset($new_openai);
//unset($latestEntry);
?>
//make newData and newDataObj an array to make this work...
newData[currentGPT3Text] = <?php echo json_encode($response, JSON_HEX_TAG); ?>;
newDataObj[currentGPT3Text] = JSON.parse(newData[currentGPT3Text]);
GPT3Array[currentGPT3Text] = newDataObj[currentGPT3Text].choices[0].text;
document.getElementById('GPT3Text').value = GPT3Array[currentGPT3Text];
document.getElementById('GPT3commentForm').submit();
GPT3link.textContent = GPT3Array[currentGPT3Text];
console.log(currentGPT3Text);
console.log(GPT3Array[currentGPT3Text]);
}
// GPT3LoadingText.onclick = generateText;
document.getElementById('loading-screen').addEventListener('click', generateText, {once: false});
</script>
这里有一个指向显示问题的站点的链接:https://surfacecollider.net/ (忽略开发控制台中的其他错误,它们与我还没有解决加载的GLTF加载程序的单独问题有关)。
下面是与表单操作链接的php:
<?php
$username = "************";
$password = "************";
$database = "************";
$mysqli = new mysqli("************", $username, $password, $database);
// Don't forget to properly escape your values before you send them to DB
// to prevent SQL injection attacks.
$GPT3_field2 = $mysqli->real_escape_string($_POST['GPT3_field1']);
$query = "INSERT INTO comments (comment)
VALUES ('{$GPT3_field2}')";
$mysqli->query($query);
?>
<?php
$query = $mysqli->query("SELECT * FROM comments");
$query = "SELECT * FROM comments";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$field1name = $row["comments"];
}
/* free result set */
$result->free();
}?>
<?php
$username = "************";
$password = "************";
$database = "************";
$mysqli = new mysqli("***********", $username, $password, $database);
$query = "SELECT * FROM comments";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Username</font> </td>
<td> <font face="Arial">GPT3_field1</font> </td>
</tr>';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["GPT3_field1"];
echo '<tr>
<td>'.$field1name.'</td>
</tr>';
}
$result->free();
}
$mysqli->close();?>
知道为什么会发生这种事吗?
发布于 2022-09-18 09:03:06
回答你的问题
你的addEventListener正在像预期的那样发射!
https://jsfiddle.net/Ldnv1oxs/1/
您可以在代码中通过注释掉generateText()
函数中的代码来测试这一点:
var generateText = function (event) {
console.log('click');
// currentGPT3Text += 1;
// <?php
// require_once './includes/Openai.php';
// $new_openai = New Openai();
// //Engine, prompt and max tokens - takes the last entry of the database array as the prompt
// $new_openai->request("davinci", end($stack), 100);
// $latestEntry = end($stack);
// $latestEntry = JSON_encode($latestEntry);
// //unset($new_openai);
// //unset($latestEntry);
// ?>
//
// //make newData and newDataObj an array to make this work...
// newData[currentGPT3Text] = <?php echo json_encode($response, JSON_HEX_TAG); ?>;
// newDataObj[currentGPT3Text] = JSON.parse(newData[currentGPT3Text]);
// GPT3Array[currentGPT3Text] = newDataObj[currentGPT3Text].choices[0].text;
// document.getElementById('GPT3Text').value = GPT3Array[currentGPT3Text];
// document.getElementById('GPT3commentForm').submit();
// GPT3link.textContent = GPT3Array[currentGPT3Text];
// console.log(currentGPT3Text);
// console.log(GPT3Array[currentGPT3Text]);
}
// GPT3LoadingText.onclick = generateText;
document.getElementById('loading-screen').addEventListener('click', generateText, {once: false});
一个提示,如何找到你剩下的问题
JavaScript:
echo
或var_dump()
你从你的PHP代码中得到了什么,检查结果是什么,你对你的PHP代码的期望是什么。。
PHP:
$stack
的值是多少?
为了澄清何时、何地执行代码,代码的哪一部分被执行:
E 235
并再次触发事件E 136
,并且again.H 240G 241
示例:<?php echo json_encode($response, JSON_HEX_TAG); ?>;
将产生一个字符串,在服务器上写入HTML,而不是在浏览器中更改。
我认为,在我们看不到的代码中,您尝试用一些用户输入将表单发送到PHP。相反,您应该捕获JavaScirpt提交的表单,通过Ajax将用户输入发送到第二个PHP端点,这样PHP就可以在每次单击时给出新的结果。
在浏览器中加载带有嵌入式JS triggers.
https://stackoverflow.com/questions/73760982
复制相似问题