我正在尝试为我一直在研究的投票脚本构建一个主题构建器。我正在努力使它可以查看预览,因此不希望页面重新加载。我是AJAX的新手,如果我绕过它,一切似乎都有效。
问题是,当我调用php文件更新我的数据库时,我收到错误
HTML FILE :(所有输入实际上都在index.php中,但是为了空间而没有在这里包含它)
<div id="theme" name="theme" class="theme">
<form class="css_form">
<table>
<tr>
<td align="left" valign="center"><label>Map Navigation Color</label></td>
<td align="left" valign="center"><input type="text" id="map_navigation" name="map_navigation" class="colorwell" value="#ffffff" /></td>
</tr>
<tr>
<td align="left" valign="center"><label>Test It</label></td>
<td align="left" valign="center"><input type="button" value="preview" onClick="getTheme();"></td>
</tr>
</table>
</form>
</div>
AJAX FILE(theme_builder.js)
function getTheme(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("theme").innerHTML=xmlhttp.responseText;
}
}
var button_style = document.getElementById('button_style').value;
var div_grad_1 = document.getElementById('div_grad_1').value;
var div_grad_2 = document.getElementById('div_grad_2').value;
var gradient_direction = document.getElementById('gradient_direction').value;
var map_0 = document.getElementById('map_0').value;
var map_100 = document.getElementById('map_100').value;
var map_selected = document.getElementById('map_selected').value;
var map_outline = document.getElementById('map_outline').value;
var map_navigation= document.getElementById('map_navigation').value;
var queryString =
"button_style=" + button_style +
"&div_grad_1=" + div_grad_1 +
"&div_grad_2=" + div_grad_2 +
"&gradient_direction=" + gradient_direction +
"&map_0=" + map_0 +
"&map_100=" + map_100 +
"&map_selected=" + map_selected +
"&map_outline=" + map_outline +
"&map_navigation=" + map_navigation;
xmlhttp.open("GET", "theme_builder.php?"+queryString, true);
xmlhttp.send();
}
最后是php文件(theme_builder.php
<?php
include('../../connection.php');
$button_style = ($_GET['button_style']);
$div_grad_1 = ($_GET['div_grad_1']);
$div_grad_2 = ($_GET['div_grad_2']);
$gradient_direction = ($_GET['gradient_direction']);
$map_0 = ($_GET['map_0']);
$map_100 = ($_GET['map_100']);
$map_selected = ($_GET['map_selected']);
$map_outline = ($_GET['map_outline']);
$map_navigation = ($_GET['map_navigation']);
$id='2';
$update_settings = "UPDATE vs_vote_settings SET
button_style = ?,
div_grad_1 = ?,
div_grad_2 = ?,
gradient_direction = ?,
map_0 = ?,
map_100 = ?,
map_selected = ?,
map_outline = ?,
map_navigation = ?
WHERE id=?";
$query_update_settings = $conn->prepare($update_settings);
if(!$query_update_settings)
{
die ("error" .$conn->errorInfo());
}
$query_update_settings->execute(array($button_style, $div_grad_1, $div_grad_2, $gradient_direction, $map_0, $map_100, $map_selected, $map_outline, $map_navigation, $id));
$conn = null;
?>
我得到的错误是:
Notice: Undefined index: div_grad_2 in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 6
Notice: Undefined index: gradient_direction in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 7
Notice: Undefined index: map_0 in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 8
Notice: Undefined index: map_100 in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 9
Notice: Undefined index: map_selected in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 10
Notice: Undefined index: map_outline in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 11
Notice: Undefined index: map_navigation in C:\Program Files (x86)\EasyPHP-12.0\www\my portable files\status201\applications\201vote\theme_builder.php on line 12
请注意,button_style和div_grad_1不会抛出错误,但其余的都是。我找不到错误,但我认为它与我的ajax文件。我遵循了Tizag中的教程对于Ajax文件并尝试将其修改为我的需求......
任何帮助都会非常感激,我知道这里有很多代码,但我已经被困了一段时间了。
由于
答案 0 :(得分:0)
尝试在theme_builder.php中使用isset
$div_grad_1 = (isset($_GET['div_grad_1'])) ? $_GET['div_grad_1'] : "";