...

View Full Version : Help for ajax



utsav
05-20-2009, 06:38 AM
I am trying to make this.

1. Suggest name in select menu as typed in text box
2. Once the preferred name is clicked in the menu then show that users profile in other form (currently div would be ok).

Part 1 works good. But part 2 doesnot show any change.

Here is my file Please help urgent. Tried other forums but nothing happened. http://rathour.com.np/ajax.zip

Old Pedant
05-20-2009, 08:17 AM
That URL fails to load. Browser says it can't find the server (both MSIE and FF).

Look, instead of giving us a ZIP file, which is a royal pain anyway (we have to download it and then find some safe place to unzip it and and and...) how about just showing some code here? Be sure to put it between [ code ] and [ /code ] tags (without the spaces).

Don't show us code that works; just show us the part that doesn't.

For example, you say you are loading <option>s into a <select> via AJAX. Is that right? And then the user clicks on one of those <option>s and nothing happens. So what we really need to see is the <SELECT> tag itself and then whatever JS code it invokes, presumably via ONCHANGE?

Old Pedant
05-20-2009, 08:29 AM
Ahhh...just tried the URL for the 5th time and was able to get to the zip file.

But your code is *NOT* doing what you said!!!

You said:


1. Suggest name in select menu as typed in text box
...
Part 1 works good.


But all your code does is dump the search result into the innerHTML of the <div>!!! You don't even *TRY* to create <option>s in a <select>. You don't even have a <select> to put the options into!


var ajaxSearchResults = document.getElementById("ajaxDiv"); // the place holder of the result.
ajaxSearchResults.innerHTML = ajax.responseText;


So what are you talking about when you say "Suggest name in select menu"??? WHAT "select menu"???

utsav
05-20-2009, 01:18 PM
Sorry if my coding is all wrong.

But the select menu that shows values once any thing is typed in the textbox of index.php is in that div. and from that now what i want is to load the data into another form when i choose something that is being displayed in the select menu (div - as u say).

So what can i do to fix it?
Could u fix it for me?

Old Pedant
05-20-2009, 11:18 PM
Okay, I see. You are just having your PHP code return a <SELECT>. Not the best way to do this.

But your <SELECT> is all messed up.

You have


<select name="names" id ="names" onchange="getdata();">
<?php
while ($row = mysql_fetch_array ($result)) {
?>
<option id="names1" onchange="getdata()" value="<?php echo $row['name'];?>">
<?php
echo $row['name'];
?>
</option>
<?php
$str = "";
$i++;
}
?>
</select>
<?php } }?>

<option>s can *NOT* have IDs. <option>s can *NOT* have ONCHANGE.

And you have $str and $i++ in there for who knows what reason. They aren't doing anything useful, at all.

And you do all that work to set up a $resultDiv PHP variable...but then you never DO anything with that variable.

On top of that, you are including "search.js" in the HTML that is returned! So your HTML page will have *TWO COPIES* of that code. Huge mistake.

You "search.php" page should *PROBABLY* look ONLY like this:


<?php
$conn=mysql_connect('localhost','root','') or die("<h5 align=center>Could Not Connect to the Server</h5>");
$db=mysql_select_db("recess1",$conn) or die("<h5 align=center>Could Not Find the Databassse</h5>");

$searchQuery = $_GET['searchQuery'];

if ($searchQuery != "" || $searchQuery != null)
{

$result = mysql_query(
"SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
);

if ($result != false || $result || (mysql_num_rows ($result)> 0))
{
//if more than 0 results are found then display them in select menu.
echo '<select name="names" onchange="getdata();">';
while ($row = mysql_fetch_array ($result))
{
echo "<option>" . $row['name'];? . "</option>";
}
echo '</select>'
}
}
?>


Untested. I'm not a PHP person.

ShadowIce
05-20-2009, 11:50 PM
Fixed. The above code had quite a few errors. The new data is here:
<?php
$conn=mysql_connect('localhost','root','') or die("<h5 align=center>Could Not Connect to the Server because ".mysql_error()."</h5>");
$db=mysql_select_db("recess1",$conn) or die("<h5 align=center>Could Not Find the Database because ".mysql_error()."</h5>");

$searchQuery = $_GET['searchQuery'];

if ($searchQuery != "" || $searchQuery != null)
{

$result = mysql_query(
"SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
);

if ($result != false || $result || (mysql_num_rows ($result)> 0))
{
//if more than 0 results are found then display them in select menu.
echo "<select name=\"names\" onchange=\"getdata();\">\n";
while ($row = mysql_fetch_array ($result))
{
echo "<option>" . $row['name'] . "</option>\n";
}
echo "</select>\n";
}
}
?> Btw. if u need to use echo, use double quotations as single quotations dont allow for \n to be used. :) Also. if u need to use more than 1 set of ANY quote in a single echo, use backslashes ONLY on the quotes which will be inside the echo statement. example: <?echo "Hi, \"friend\"\n";?> it will say: Hi, "friend", instead of Hi, friend :) Same also happens with <? $friend = "friend"; echo "Hi, \"".$friend."\""; ?> it will return Hi, "friend" as well, as u use a dot, then php variable, or a dot then a set of quotes with text in the middle of quotes :) and w/e is put in the variable, is what it will say in quotes :)

Good luck :)

~SI~

Old Pedant
05-21-2009, 04:40 AM
Sorry, Shadow, I don't see anything you changed that matters. Except my typo where I had ";?" for who knows what reason.

There's no reason to put the \n's into the HTML, because it will never be seen by a human being (remember, this is an AJAX page) and browsers don't care about line breaks, at all.

Actually, I see other things wrong with the code:
(1) Why make a DB connection at all if there is no search term passed?
(2) Why check for the search term both != "" *OR* != null?? If it's not null, it *will* be a string--rules of what $_GET returns.

*PROBABLY* a more sensible page would look like this:


<?php
$searchQuery = $_GET['searchQuery'];
if ($searchQuery != null && || $searchQuery != "" )
{
$conn=mysql_connect('localhost','root','')
or die("<h5 align=center>Could Not Connect to the Server</h5>");
$db=mysql_select_db("recess1",$conn)
or die("<h5 align=center>Could Not Find the Databassse</h5>");
$result = mysql_query(
"SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
);
if ( $result == null || mysql_num_rows ($result) == 0 )
{
// optional message
echo "<h5 align=center>No matches</h5>";
} else
{
//if more than 0 results are found then display them in select menu.
echo '<select name="names" onchange="getdata();">';
while ($row = mysql_fetch_array ($result))
{
echo "<option>" . $row['name'] . "</option>";
}
echo '</select>'
}
}
?>

If you want the \n's in there for some odd reason--debugging, perhaps?--then change to Shadow's strings. I see no reason to use \" or \' when there is no need for them, especially not to just put in \n.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum