<script type="text/javascript">
var selected = new Array();
function inviteUser()
{
$(document.getElementById('invite')).click(function ()
{
getSelectedUsers();
var movieTitle = document.getElementById('titleKeyword').value;
var movieID = document.getElementById('movieMid').value;
var location = document.getElementById('location').value;
$.ajax({
url: 'processinvitationrequest.php',
data: {action: 'inviteUser', selectedUsers: selected, movieID: movieID, movieTitle: movieTitle, location: location},
type: 'post',
success: function(output)
{
alert(output);
}
});
});
}
function getSelectedUsers()
{
$("input:checkbox[name=type]:checked").each(function() {
selected.push($(this).val());
});
}
</script>
我正在尝试将复选框字段中的所有值发送到 php 页面以查询我的数据库。这是我的问题: 1)我不知道通过数据参数发送 js 数组“选择”的语法是否正确。2)我如何从我的php中的ajax检索我的数组,我的代码是否正确。
我得到一个未定义的索引错误。
if (isset($_POST['action']) && !empty($_POST['action']))
{
$action = $_POST['action'];
switch($action) {
case 'inviteUser' : inviteUser();break;
}
function inviteUser()
{
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING']))
{
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
foreach($_POST['selectedUsers'] as $selected)
{
$notificationLink = "http://localhost/Movies/movienightout.php?movieID=".$_POST['movieID']."&movieTitle=".$_POST['movieTitle']."&location=".$_POST['location']."";
$insertSQL = sprintf("INSERT INTO requestnightout (sender, recipient, notification) VALUES (%s, %s, %s)",
GetSQLValueString($_SESSION['MM_sessionEmail'], "text"),
GetSQLValueString($_POST['selectedUsers'], "text"),
GetSQLValueString($notificationLink, "text"));
mysql_select_db($database_movie, $movie);
$Result1 = mysql_query($insertSQL, $movie) or die(mysql_error());
$urlplus="notification=Invitation Sent";
header("Location: usersocialpage.php?".$urlplus."");
}
}
输出:
array (size=4)
'action' => string 'inviteUser' (length=10)
'movieID' => string '9' (length=1)
'movieTitle' => string 'Star Wars: Episode III - Revenge of the Sith 3D' (length=47)
'location' => string 'seee' (length=4)
( ! ) Notice: Undefined index: selectedUsers in C:\wamp\www\Movies\processinvitationrequest.php on line 54