所以我想在我的数据库中插入一行。我正在调用一个类似 ajax 的函数来在我的表中插入一个新行。但它没有插入一行。
function showResult(first, last)
{
var First = first;
var Last = last;
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","http://www.website.ca/portal/MyChapter2/cgi-bin/DetermineUser.php?FirstName="+First+"&LastName="+Last,true);
xmlhttp.send();
}
这是它转到的文件,以便将行插入到表中。
<?php
require_once (dirname(__FILE__) . '/../../include/Initialization.php');
require_once (PORTAL_PATH . '/include/FormLibrary.php');
require_once (PORTAL_PATH . '/include/SingleRowQuery.php');
require_once (PORTAL_PATH . '/include/Functions.php');
require_once (PORTAL_PATH . '/include/VolunteerInterests.php');
require_once (PORTAL_PATH . '/TaskManager/cgi-bin/AutoTaskFunctions.php');
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$sql="INSERT INTO `Track_Notification`(`Track_ID`, `Track_UserID`) VALUES ('$FirstName','$LastName')";
echo ("success");
?>
最佳答案
您正在执行 POST,但未通过该 POST 发送任何数据。您在 URL 中发送数据,这实际上是一种 GET 技术:
xmlhttp.open([..snip...] /DetermineUser.php?FirstName="+First+"&LastName="+Last,true);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
不管你使用什么 HTTP 动词,如果 URL 中有查询参数,它们将在 $_GET 中,所以
$_GET['FirstName'];
$_GET['Lastname'];'
除此之外,您还容易受到 SQL 注入(inject)攻击,所以享受您的服务器 pwn3d。
关于php - Ajax 函数未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21992191/