Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax textbox and value

Status
Not open for further replies.

Marine1969

IS-IT--Management
Mar 5, 2015
60
0
0
US
I finally got my ajax working but not quite getting the desired result. Below is the search page. There are 3 questions here...
1. The ajax results make the textbox wider that the screen. So rather than creating 1 result per line it keeps going until it runs out of room and then then returns. So I can have 10+ results per line, although it does highlight each result when hovering. How do I get them to return 1 per line?

2. Is there a way to get the textbox to have a different value like a select box where you can display a name but the option value is an id?

3. Is there a way to adjust the execute line to also include the producer? IE. "if producer Like %$_GET['term']% or name Like %$_GET['term']% "

PHP:
$str = $conn->prepare("Select clientvendors.producer, products.name "
                    . "From products Inner Join clientvendors On clientvendors.idcv = products.idcv "
                    . "Where products.idclient=2 and name Like :name Order By clientvendors.producer, products.name");
$str->execute([':name' => "%{$_GET['term']}%"]);
$select = $str->fetchAll();

foreach ($select as $row) {
   $data[] = $row['producer'] . ": " . $row['name'];
   //$data[] = $row['name'];
}

//return json data
header('Content-Type: application/json');
echo json_encode($data);
 
1. Probably yes, but without knowing what you are doing in the JS portion of your code to display the results its hard to say what can be done.
You would need to have JS process the returned json and output the data however you need.

2. No. The Textbox has only one value. However, depending on what you plan on doing with said value, you can potentially use a hidden input tied to the text box and give it the actual value you want.

3. Yes. You can pass as many parameters to the query as you need. and your query can be whatever you want. You need to adjust your query as you point out.. This is more a PHP question that Javascript however.
[ocde]
. "Where products.idclient=2 and (producer like :name OR name Like :name) Order By clientvendors.producer, products.name");
Code:
----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

[url=http://behindtheweb.blogspot.com/] Web & Tech [/url]
 
Hi Vacunita,

The problem I had was somewhere in my dbconnection.php page. I just created a connection in my script page and it worked. Thanks for all the help.

1. Can you direct me where to look to figure out how to process the returns so that each one is on its own line?
2. I was able to strip out the data I needed when the selection is made.
3. Moving this to the php thread.
 
Just the third question is more PHP than JS. The other 2 do belong here. Do you have a further question about it? Of os, yes post in the PHP forum.

As to how to process the JSON, there's several ways to do this. You can have your PHP code return HTML and just have Javascript insert it into an element in your page, have Javascript actually build the HTML and insert it. Or use Js or jquery to build the elements directly.

Easiest thing to do, is to have PHP create the HTML, and just have the JS output it into an element.

Instead of returning a JSON array return a pre built HTML string.

Code:
foreach ($select as $row) {
   $data[] = [b]"<p>"[/b] . $row['producer'] . ": " . $row['name'] . [b]"</p>"[/b];
   //$data[] = $row['name'];
}

You'll need to modify the JS that receives the JSON to act on HTML rather than on JSON however. Otherwise see here for how to build an HTML table from JSON data for starters:
If you show us the Js side we may be able to provide a more specific answer.











----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
So I'm finally able to get back to this. How do I need to modify the js to get the results on seperate lines? Below is the code that calls the script that gets the data.

JavaScript:
<script>
	$(function() {
	    $( "#product" ).autocomplete({
	        source: 'search.php'
	    });
	});
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top