使用javascript创建表单并将输入带回php

Using javascript to create form and bring input back to php

本文关键字:输入带 php javascript 创建 表单 使用      更新时间:2024-03-30

我已经尝试了一段时间来让它发挥作用。我想要的是,当用户单击create Card时,应该弹出一个包含多个字段的表单,这样他们就可以看到$key[2]并编写问题和答案。

如果有人能给我指明正确的方向,用javascript创建这个表单,并解释如何将该表单中的变量返回到php,那将是非常棒的。我试着使用xmlhttprequest,我不知道这是否是最好的。

<?php
if (isset($myNotes)) {
    foreach ($myNotes as $key) {
        Print "<li>$key[2] <a href=''><dif onclick='createCard()'>(Create Card)</dif></a></li>";
    }
}
?>
function createCard()
{
    xmlhttp=new XMLHttpRequest();
    var question;
    var answer;
    question=prompt("Enter Q:","");
    answer=prompt("Enter Answer:","");
    xmlhttp.open("GET","control.php?q="+question, true);
    xmlhttp.send();
}

您可以有一个隐藏的div,其中包含当用户单击createCard时显示的表单。

<div id="popupdiv" style="display:none">
<p>Question: <input type="text" id="question"></p>
<p>Answer: <input type="text" id="answerswer"></p>
<input type="button" onclick="processCard()">
</div>

function createCard(){
    // This displays the popup with your form.
    document.getElementById("popupdiv").style.display = "block";
}
function processCard(){
    // This grabs the data and sends it server and then hides the div again.
    var question = document.getElementById("question").value;
    var answer = document.getElementById("answerswer").value;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","control.php?q="+question+"&a="+answer, true); 
    xmlhttp.send();
    document.getElementById("popupdiv").style.display = "none";
}   


 <?php
 $question = $_GET['question'];
 $answer = $_GET['answer'];
 ?>