Jump to content

help!


Toy

Recommended Posts

hey, i'm nearly done with my new website project, getting massive help from here :)

 

now I this is kind of what I want to do:

 

There's a "drop down" (select) html bar who loads all the "names" in a table in mysql database.

When selected it'll load the "text" column and display the content in it (with an ajax function or so). When you enter the website and haven't selected one in the drop down bar yet, it'll just use the first best one for the time being.

 

I'd appreciate help, currently trying to do this on my own! :P

Link to comment
Share on other sites

I'm sorry but your request ain't clear at all. Could you please specify better what you need?

 

1. a html dropdown containing a list of possibilities loaded from a database?

2. not clear at all, something about a test column: please specify

3. make the dropdown a clickable link?

4. grab the default if nothing was passed on?

Link to comment
Share on other sites

ok simple

 

if we have a table:

table

names

text

 

A dropdown list should load all the "names" from each record in the table called: "table".

 

When you click on a certain entry in the drop down list

the "text" row from the same record as the name you just selected will be loaded (below or somewhere on the page)

 

And if you just enter the page without actually having selected anything from the drop down list it

 

it will just load the first "TEXT" entry it can find. And you can select another one later on.

 

do you understand? ;S

Link to comment
Share on other sites

I think I got it now. It's possible using php, mysql and ajax.

 

This should put you on your way.

 

The dropdown code:

<select name="names" id="names"  onchange="writeText(this.options[this.selectedIndex].value)">
    <option value="">Select a name</option>
<?php
    $sql = "SELECT Distinct(names) FROM table";
if (!$selecttabel = mysql_query($sql, $conn)) { echo mysql_error(); exit(); }

$nrlines =  mysql_num_rows($selecttabel);
for ($i = 0; $i < $nrlines; $i++)
{
	$stats = mysql_fetch_array($selecttabel);
	$names= $stats['names'];
	$text= $stats['text'];

	echo "<option value=\"$names\">$names</option>\n";
}
?>
    </select>

 

Where you want the text to come in your document make a div or a span with id 'textValue'.

 

Then make a javascript with this content and include it on your page:

 

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
	return new XMLHttpRequest();
} else if(window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP");
} else {
	alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function writeText(nameValue) {
if (searchReq.readyState == 4 || searchReq.readyState == 0) {
	searchReq.open("GET", 'getText.php?name=' + nameValue, true);
	searchReq.onreadystatechange = handleResponse; 
	searchReq.send(null);
}		
}

//Called when the AJAX response is returned.
function handleResponse() {
if (searchReq.readyState == 4) {
	var str = searchReq.responseText;
	document.getElementById('textValue').innerHTML = str;
}
}

 

Then make a php file and name it getText.php. In that file you run a _GET to get the value called 'name' and run a query to return text. Output that to the browser without anything else. If you're running it on a host with ads, you'll have to output it in xml and xpath the response in javascript first.

Link to comment
Share on other sites

Allright! Got the most of it working, but I wan't it so when you visit the page (the first time) and yet haven't selected anything out of the drop down list it'll just show the first best result and load it in the div! :)

 

Care to help me anyone? :S

Link to comment
Share on other sites

Just do it like this:

<div id='textValue'><?php echo $text; ?></div>

 

That should work if your div is stated before your dropdown box since it will just store the last text result it got from your dropdown. I see you need to fix a small bug in the first dropdown code I gave you though:

 

$sql = "SELECT Distinct(names) FROM table";

 

should be:

 

$sql = "SELECT Distinct(names), text FROM table";

 

Otherwise run a small query at the start:

 

$sql = "SELECT text FROM table limit 1";

 

Get the result and set it as a variable. Then put that variable in the div by default.

Link to comment
Share on other sites

Just do it like this:

<div id='textValue'><?php echo $text; ?></div>

 

That should work if your div is stated before your dropdown box since it will just store the last text result it got from your dropdown. I see you need to fix a small bug in the first dropdown code I gave you though:

 

$sql = "SELECT Distinct(names) FROM table";

 

should be:

 

$sql = "SELECT Distinct(names), text FROM table";

 

Otherwise run a small query at the start:

 

$sql = "SELECT text FROM table limit 1";

 

Get the result and set it as a variable. Then put that variable in the div by default.

 

I didn't use your code to fully, so i'm not able to do such.

 

I'm so tired right now but couldn't you just do like.. I mean when i've selected something in the drop down it works perfect! Couldn't you just query whatever the ajax is doing to load first best before selecting

 

I don't really know what i'm talking about right now. I'll go to sleep now.. if someone get's it i'll be so thankful. Really. :) If not i'll explain more tomorrow and supply code if needed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.