我正在尝试将一个变量从javascript函数传递给我的php文件。我所做的就是从javascript中的cookie中提取user_name然后使用AJAX将$ _POST变量添加到我的php文件中进行处理。我没有使用表单来提交我过去做过的POST变量。
问题是我的$ _POST数组没有传递给php文件。我看了看这里,看了几个其他的建议,但都没有用。你必须通过html表单提交$ _POST变量吗?我不这么认为,但也许我错了。这是代码:
Javascript -
function clearDoneItems() {
var user_name = getNameFromCookie();
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
alert(user_name);
xmlhttp.open("POST","todolist/clear_done_items.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(user_name);
displayHomeInformation(user_name);
}
PHP -
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Clear Done Items</title>
</head>
<body>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
print_r($_POST);
$xml_name=$_POST['user_name'];
$xml_file = $xml_name . ".xml";
echo $xml_file;
/* Here we will use the The DOMDocument class functions to delete
the text nodes.
Format XML to save indented tree rather than one line */
$domxml = new DOMDocument('1.0');
if ($domxml->load('$xml_file') === TRUE) {
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
// Start Delete process of node
echo "<br>";
$xpath = new DOMXPath($domxml);
// Here we use the The DOMXPath class function evaluate to do the heavy lifting.
foreach ($xpath->evaluate('//doneitems/item') as $node) {
$node->parentNode->removeChild($node);
}
//Save XML to file - remove this and following line if save not desired
$domxml->save('alan.xml');
echo "<strong>XML Document updated.</strong>";
}
else {
echo " <strong>Error in loading XML file:</strong>";
echo "<br>";
print_r(error_get_last());
}
?>
</body>
</html>
php页面上的错误: 注意:未定义的索引:第16行的/var/www/bb/todolist/clear_done_items.php中的user_name 0 警告:DOMDocument :: load():I / O警告:无法加载外部实体&#34; / var / www / bb / todolist / $ xml_file&#34;在第24行的/var/www/bb/todolist/clear_done_items.php
答案 0 :(得分:0)
很抱歉延迟回复。我只是想发布我如何解决问题。我将不得不重新使用带有AJAX的$ _POST数组,因为我永远无法通过它传递变量。
使用Javascript:
// Clear Done Items Function
function clear_done_items()
{
var xmlhttp;
var temp = document.getElementById("list_section");
var temp_list_name = temp.innerHTML;
var str_len = temp_list_name.length;
var list_name = temp_list_name.slice(17,str_len);
var user_name = getNameFromCookie();
if (user_name == list_name) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","todolist/clear_done_items.php",true);
xmlhttp.send();
} else {
alert("Warning: You Must be Logged in as this User, " + list_name + ", to Delete the Completed Items List");
}
}
和php:
<?php
$xml_cookie = $_COOKIE['user'];
$xml_file_name = $xml_cookie . ".xml";
/* Here we will use the The DOMDocument class functions to delete
the text nodes.*/
//Format XML to save indented tree rather than one line
$domxml = new DOMDocument('1.0');
if ($domxml->load($xml_file_name) === TRUE) {
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
// Start Delete process of node
echo "<br>";
$xpath = new DOMXPath($domxml);
// Here we use the The DOMXPath class function evaluate to do the heavy lifting.
foreach ($xpath->evaluate('//doneitems/item') as $node) {
$node->parentNode->removeChild($node);
}
//Save XML to file - remove this and following line if save not desired
$domxml->save($xml_file_name);
echo "<strong>XML Document updated.</strong>";
}
else {
echo " <strong>Error in loading XML file:</strong>";
echo "<br>";
print_r(error_get_last());
}
?>
所以基本上我使用cookie来获取php的user_name变量来从主页面进行处理。