Jump to content
View in the app

A better way to browse. Learn more.

The Uniform Server Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Undefined index:i on line 2

Featured Replies

I'm using ul element onclick javascript function to change div innerHTML with get mysql query but the innerHTML returns 'You have an error in your SQL syntax "undefined" at line 2'.

 

Onclick external.js Ajax

function getproduct(i){
if(i=' '){
document.getElementById('f1').innerHTML=' ';
}
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("f1").innerHTML=xmlhttp.responseText; //errors declared here
}
}
xmlhttp.open("GET","getproduct.php?q="+i,true);
xmlhttp.send();
}

var jewelry= document.getElementById('jewelry').childNodes;   
for (i = 0; i < jewelry.length; i++) {   
if (jewelry[i].className == 'item') {  // excludes class="item disabled" 
jewelry[i].onclick = function() {   
alert(this.id+': Is sending your request.');  // this actually gets the right id
var i=this.id; // declared variable?
getproduct(i);//used variable returned undefined index?
}   
}   
} 

window.onload=getproduct();

 

getproduct.php?

<?php
$q=$_GET["i"];//undefined index i at line 2
$link=mysql_connect("localhost","root","root")or die(mysql_error( ));
mysql_select_db("bwi",$link) or die(mysql_error( ));
$query=mysql_query("select FROM* [B]'".$i."'[/B]")or die(mysql_error( ));
//undefined variable i line 7 syntax error on quotes?

 

As I'm new to Ajax and PHP I have to know how to define i and the corect syntax in the select FROM * statement. Any thoughts greatly appreciated :) :rolleyes:

really look at what the error code says

You have an error in your SQL syntax

 

so it is not the ajax or the php it is the SQL statement you need to be looking at

 

in your sql you currently have "select FROM *" see anything wrong about that?

SQL is very logical - you are wanting to say select everything from the table or "select * FROM"

  • Author

megan Thanks, you're right. The 'i' still returns 'undefined index: i on line 2 and syntax error on line 7 ie

select * From '".i."'"

I'd guess that the ajax call is not getting the id of the li item clicked for the php $q=$_GET["i"]; as well as the wrong syntax for the quote double quotes shown above. Will have to get back and try again on the pc that has the server. Thank again for pointing out that simple syntax error.

  • Author

megan

The new errors produced after corecting the select*FROM statement are as follows

Notice: Undefined index: i in D:\use_ide_1\UniServer\www\getproduct.php on line 2

 

Notice: Undefined variable: i in D:\use_ide_1\UniServer\www\getproduct.php on line 7

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''..'' at line 1

 

So I'm guessing the 'i' is not being defined which could be the javascript ajax call function or the onclick itself is not passing var i=this.id to getproduct() corectly. I'll have to post in javascript forum. :rolleyes:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.