0

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.

<script>

function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","testtwopart1.php?q="+str,true);
xmlhttp.send();
}
</script>


</head>
<body>

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

</select>


<div id="txtHint"><b>billing will be listed here.</b></div>
5
Contributors
21
Replies
23
Views
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

0

Sorry... heres the testwopart1.php

`<?php
$q=$_GET["q"];

$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>";




mysql_close($con);
?> `
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 any vulnerability issues and weaknesses..

 if($_GET){

 ## codes here

 }
 else{

 $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

0

Hello,

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

0

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">
0

@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 :)

0

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

0

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

query1

Edited by xbat

0

It's this line:
xmlhttp.open("GET","testtwopart1.php?q="+str,true);
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.

0

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

Edited by xbat

0

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

0

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

0

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

0

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:
xmlhttp.open("GET","testtwopart1.php?q="+q_str,true);
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:
showUser(this.options[this.selectedIndex].value)

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 :)

0

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.

0

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>
</select>

...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; 
    alert(selected);
}

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) :)

0

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>

</select>

0

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.