Getting a q is undefined.. I took <option="1">1</option> etc.. and now I'm pulling with ajax from datasrc..

I can't quite figure out how to define my database value.. The one uses query which pulls from the auto start typing.. then it submits to the other page through q - Once you click the item box where the part number is taken from.. It does not send q through.. Any pointers or help would greatly be appericated.


function showUser(str)
if (str=="")
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)


<form name="invoice1">
<select name="users" onchange="showUser(this.form)" width="80" style="width:80px" class="autoconvert contain" datasrc="inventory.php">


<div id="txtHint"><b>billing will be listed here.</b></div>
4 Years
Discussion Span
Last Post by xbat
Featured Replies
  • 1

    Hi, Try adding... if(isset($_GET['q'])){ ## codes for the script response when GET is triggered $q=$_GET["q"]; } else{ $q = ''; } Alternatively, I keep on seeing this type of approach by other developers of which I have not tested or ever tried, but I think it is worth teting for … Read More


Sorry... heres the testwopart1.php


$con = mysql_connect('localhost', 'yaaaaaaaa', 'passomass');
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("something", $con);

$sql="SELECT * FROM inventoryjazz WHERE itemnumber = '".$q."'";

$result = mysql_query($sql);

echo "<table>

<input type='text' name='inp1' value='desci={}'> ";

while($row = mysql_fetch_array($result))
  echo "<tr>";
  echo "<td>" . $row['itemnumber'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "<td>" . $row['list'] . "</td>";
  echo "</tr>";
echo "</table>";

?> `


Try adding...


## codes for the script response when GET is triggered


$q = '';


Alternatively, I keep on seeing this type of approach by other developers of which I have not tested or ever tried, but I think it is worth teting for any vulnerability issues and weaknesses..


 ## codes here


 $q = '';


Again the alternative option is just too broad for my consideration. I am just assuming here that this method can fail.

Edited by veedeoo: more info. added



Sorry no I didn't get it to work... Its with firebug and i believe the java that is causing the error.

I believe the javascript still thinks its pulling from <option="value <<<< and im trying to get it to pull from q thats where its causing an error i believe

Edited by xbat


Pass this.value not this.form to showuser (line 35 in your code above in post 1)

<select name="users" onchange="showUser(this.value)" width="80" style="width:80px" class="autoconvert contain" datasrc="inventory.php">

@xbat, from what I can see, 'q' is not defined in your JS code, it is not even referenced, so it can't be JS that's giving the error. The only way I can understand this undefined error regarding 'q' is the processing page, testwopart1.php.
The beginning of the script (testwopart1.php), references $_GET['q'] and if this variable is not defined, you will get an error of type notice concerning an 'undefined key'. Veedeeo's suggestion to use a test: if(isset($_GET['q'])) is the way to avoid this error. If you want to test testwopart1.php, you can go directly to the page and add "?q=something" into the address bar (change the something to what you think should be sent) and see what the output is. If the output looks good, then I would put a line at the top of testwopart1.php: echo "some random words"; die(); and then alert the xmlhttp.responseText to the screen on return to the ajax function. From these two steps you can get a better idea of where the problem lies.
I find this type of problem standard issue whenever I have used Ajax. Good luck :)


I changed the this.value, as for if(isset($_GET['q'])) and veedeoo I tried that and am I still getting the same error


Heres the error I am getting with firebug.. It works fine on query but doesn't work with q Click Here


Edited by xbat


It's this line:
str is undefined.
utrivedi suggested changing line 35:
<select name="users" onchange="showUser(this.value)"
I agree with him.

You could alert the value of str before you send the ajax request, to make sure it is formed properly.


eve after changing this.form to this.value I still get the same error.. with q

Edited by xbat


Can you copy the structure of the select list from the source when the page has loaded and post a few lines here?

Edited by adam.adamski.96155


I'm not sure what your asking me.. Do I go into firebug and do this? What do I click?


There is no <option="1">1</option>... .. I took <option="1">1</option> etc.. and now I'm pulling with ajax from datasrc.. I'm trying to pull from the datasrc not option 1


If I could see how the options are formed, I could be certain how to proceed, but without seeing I can still make a guess.

change your select declaration to this:
<select name="users" onchange="showUser(this)"

...then add this line:
q_str = str.options[str.selectedIndex].value;
...above the line:
notice that 'str' was changed to 'q_str' in the line above.

There is a special way of finding out which option was selected and your script wasn't configured to discover it. showUser(this) passes the element itself to the function (this element) and str.options[str.selectedIndex].value gets the value of the selected option. Theoretically, you could send the selected option straight to the function but it would get messy in those parenthesis:

If it doesn't work now, I need to see the structure of the select element, specifically the options... yes I know they are pulled from the src file, but they must still exist in the page when it is loaded or there could be no 'onchange' taking place. Right-click and view source to see them.
Good luck :)


What code where you looking for or needing?

I'm getting this error - TypeError: str.selectedIndex is undefined

Thanks for all your help with all this.. I have forgotten alot with javascript.


I will show you what I mean:

A standard select menu...

<select id="select" onchange="alertSelected(this)">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>

...will have the select declaration:
<select id="select">
...and will also have some options:

<option value="1">One</option>
<option value="2">Two</option>

Without the options we can't 'select' one and send that selection in the ajax request.
As an example, we will only alert the selected value using a JS function:

function alertSelected(el){
    var selected = el.options[el.selectedIndex].value; 

I am running this exact code in my browser and it is alerting the selected value when I choose an option. We need to do the same with your code, which means seeing the structure of the '<option>'s. The way to acheive this is to let the page load, then view the source of the page (standard procedure), then look through the source for the section where the options are set.
You can copy that code and paste it here (hopefully) :)


I'm not using any otptions. I'm pulling directly for the database.

example of what im using -

`<select name="users" onchange="showUser(this)" width="80" style="width:80px" class="autoconvert contain" datasrc="inventory.php">

</select> `

I'm not using

<select name="whatever">

<option name=""></option>



I found a different way of doing this with jquery... I am going to mark this solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.