我目前正在编写一些民意测验软件,尽管它可以正常工作,但是我很难使我的一些JavaScript正常工作。我有一个标签为“添加新选项”的按钮,单击该按钮将调用以下javascript函数:
function newoption()
{
var option = "";
while((option.length < 1)||(option.length > 150))
{
var option = prompt("Please enter the option value... ").trim();
}
var add = confirm("You entered " + option + ", are you sure?");
if(add==1)
{
var code = window.location.href.length;
var poll = prompt("Which poll are you adding this to?", window.location.href.substring(code - 5, code));
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{this.responsetext = option;}};
xhttp.open("POST", "../special/new.php", true);
xhttp.send("poll=" + poll + "&opt=" + option);
}
else
{
alert("OK... try again");
}
}
它发布到的页面仅具有向用户提供代码的投票中添加选项的功能(它会自动从URL末尾获取代码),但是问题是,当我刷新页面时,选项列表没有更新,这使我觉得它没有被添加到数据库中,但是添加新选项的功能在创建民意调查时起作用。我在做错什么吗?
new.php的代码是:
<?php require("internal/index.php");
$option = string_format($conection, $_POST["opt"], 1)
$poll =(int) $_POST["poll"];
if($poll&&$option)
{
new_poll_option($connection, $poll, $option);
}
?>
答案 0 :(得分:0)
根据您所写的内容,我了解该代码在刷新页面之前一直有效。这意味着您无需检查Ajax响应,而只需插入一些HTML即可,直到刷新页面为止。
如果已创建项目,则需要查看数据库。如果已创建,则可能需要删除浏览器缓存(可以从Chrome的DevTools中的“网络”标签中删除)。
如果没有将项目插入数据库,则您需要调试或仅echo
使用的插入函数中的消息。
如果您稍后会刷新页面,则也可以使用form
而不使用 Ajax 。