首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的addEventListener单击事件只触发一次?

为什么我的addEventListener单击事件只触发一次?
EN

Stack Overflow用户
提问于 2022-09-18 07:52:38
回答 1查看 54关注 0票数 0

我使用一个addEventListener单击事件来触发一个新的GPT3请求,该请求将最新的数据库帖子作为文本完成的提示。

数据通过隐藏的html表单发送到数据库,表单的操作触发了php脚本。

然后将新生成的文本发送到数据库,数据库将成为下一个GPT3请求等的提示。

问题是事件只触发一次,我希望能够在加载页面中继续生成新的文本,而页面中的其他数据重文件则加载。

代码语言:javascript
运行
复制
<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:

代码语言:javascript
运行
复制
<?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();?>

知道为什么会发生这种事吗?

EN

回答 1

Stack Overflow用户

发布于 2022-09-18 09:03:06

回答你的问题

你的addEventListener正在像预期的那样发射!

https://jsfiddle.net/Ldnv1oxs/1/

您可以在代码中通过注释掉generateText()函数中的代码来测试这一点:

代码语言:javascript
运行
复制
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:

  1. echovar_dump()你从你的PHP代码中得到了什么,检查结果是什么,你对你的PHP代码的期望是什么。
  2. 如果你的PHP正常工作,然后用你的PHP的结果准备一个代码片段,硬编码成你的JavaScript代码。
  3. 用这个代码片段写一个JavaScript问题。

PHP:

  1. --如果您的PHP代码不能像预期的那样工作,那么准备一个代码片段,为我们提供我们所需要的一切,以了解正在发生的事情。示例:$stack

的值是多少?

为了澄清何时、何地执行代码,代码的哪一部分被执行:

  1. 在.php文件中运行此代码段。一旦server.
  2. Then上的 on request time上的 PHP 部件在上运行--一些HTML被发送到带有嵌入式浏览器的浏览器中加载页面,将执行 JavaScript E 235并再次触发事件E 136,并且again.
  3. BUT JavaScript将使用来自PHP的相同值执行,因为PHP不再在浏览器中执行,这是不可能的。H 240G 241

示例:<?php echo json_encode($response, JSON_HEX_TAG); ?>;将产生一个字符串,在服务器上写入HTML,而不是在浏览器中更改。

我认为,在我们看不到的代码中,您尝试用一些用户输入将表单发送到PHP。相反,您应该捕获JavaScirpt提交的表单,通过Ajax将用户输入发送到第二个PHP端点,这样PHP就可以在每次单击时给出新的结果。

在浏览器中加载带有嵌入式JS triggers.

  • generateText()用户类型的
  1. mypage.php并将其发送到浏览器,事件在每个 generateText()上执行,您可以向端点myapi.php发出Ajax请求,发送用户输入并获得结果
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73760982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档