Reputation: 73
Using firebug I keep getting an 'undefined index' notice for 'item', 'rating', and 'classes'. Below is my code. In the php code if I comment out the if/die statement that's where I get 'undefined index' from, if I leave the if/die statement in I get 'There was a problem' as the response with firebug. My question is how do you properly define an index? What I'm wanting to do is transfer some values using the POST method to a different PHP file. If I alter the Javascript code to make sure up to that point is good, everything looks good, but when it gets to the next stage, which is the php code below, that's where my problem occurs. BTW, I'm not a programmer and pretty new with this stuff, this is just a side project. Thank you for any help in advance.
<?php
require_once("classes/include.all.php");
// Check that the data was sent
if (sizeof($_POST) == 0
|| $_POST['item'] == null
|| strlen(trim($_POST['item'])) == 0
|| $_POST['rating'] == null
|| strlen(trim($_POST['rating'])) == 0
|| is_numeric($_POST['rating'])
|| $_POST['classes'] == null
|| strlen(trim($_POST['classes'])) == 0)
{
die("There was a problem");
}
echo Rating::RateItem($_POST['item'], $_POST['rating'], $_POST['classes']);
?>
Javascript code
function RateItem(varItemId, varRate)
{
var varorgclass = document.getElementById(varItemId).className;
var request;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
}
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST","ajax.rate.item.php",true);
//request.onload= ReloadRating;
request.setRequestHeader('item',varItemId);
request.setRequestHeader('rating',varRate);
request.setRequestHeader('classes',varorgclass);
request.send();
Upvotes: 0
Views: 377
Reputation: 1888
If those values aren't set, there is a problem in your javascript, which may be because of a browser incompatibility. Since you are new, I would strongly recommend the use of JQuery - it's easy to use, lightweight, and portable. You can use Google's hosted copy of JQuery for quick access that will already be cached by most people, too.
First, include this in the head of your HTML/PHP Document:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Then, use the following javascript in place of your current "request" setup:
$.ajax({
url:"ajax.rate.item.php",
type:"POST",
data:{item:varItemId,rating:varRate,classes:varorgclass},
success:function(result){
alert(result);
}
});
Upvotes: 0
Reputation: 123
Your variable are not set, try this :
<?php
require_once("classes/include.all.php");
// Check that the data was sent
if (isset($_POST['item'], $_POST['rating'], $_POST['classes'])) {
if (sizeof($_POST) == 0
|| $_POST['item'] == null
|| strlen(trim($_POST['item'])) == 0
|| $_POST['rating'] == null
|| strlen(trim($_POST['rating'])) == 0
|| is_numeric($_POST['rating'])
|| $_POST['classes'] == null
|| strlen(trim($_POST['classes'])) == 0)
{
die("There was a problem");
}
echo Rating::RateItem($_POST['item'], $_POST['rating'], $_POST['classes']);
}
?>
Upvotes: 0
Reputation: 32350
On $_POST['item'] == null
(in the first if mentioned in the question) the warning appears because $_POST['item']
does not exist.
You can prevent this by either:
isset($_POST['item'])
first, or
array_key_exists('item', $_POST)
Or.... you disable error_reporint()
for E_WARNING
/ E_NOTICE
.
First solution recommended.
Upvotes: 4