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!

Help needed with an Ajax using JQuery to return to values to a form

Status
Not open for further replies.

aalmeida

MIS
Aug 31, 2000
468
0
0
US
I'm having an issue with Ajax using a JQuery to populate 2text fields as you can see in the cold below I have two PHP pages.
The main page that have a form with 2 text fields that need to be field with the selected result from the second page Ajax JQuery query.
I'm not being able to make the result selectable much less to populate the 2 fields.
I would like to populate the hidden field "id" with the value of "m.pessoas_id" and the field "find" with the value "m.nome" from the DB query.
Below the two pages code Any help will be appreciated.

Main Page
Code:
<?php
require_once'DbConnection.php';
?>
<html>
<head>
<meta http-equiv="Content-Language" content="pt-br" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>jQuery</title>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$('document').ready(function(){
        $('#loading').hide();
        $('#find').click(function(){
                $('#find').val('');
        });
        $('#find').keyup(function(){
                $('#loading').ajaxStart(function(){
                        $('#target').hide();
                        $('#loading').show();   
                });
                $('#loading').ajaxStop(function(){
                        $('#loading').hide();   
                });
                $.post('buscaAjaxJQueryTest.php',
                {busca: $('#find').val()},
                function(data){
                        if ($('#find').val()!=''){
                                $('#target').show();
                                $('#target').empty().html(data);
                        }
                        else{
                                $('#target').empty();
                        }
                }
                );
        });
});
</script>
</head>
<body>
<div id="main">
	<div id="busca">
      <form>
          <fieldset>
          <legend>Busca usando JQuery</legend>
            <input name="id" id="id" type="hidden" value=""/>
            <input name="find" id="find" type="text" value="Digite o Nome" size="50" />
            <div id="loading"><img src="BarraCarregando.gif"> </div>
            <div id="target"></div>
          </fieldset>
      </form>
	</div> <!-- busca -->
</div> <!-- main -->
</body>
</html>

Now the page that run the JQuery query

Code:
<?php
header("Content-Type: text/html; charset=ISO-8859-1",true);
require_once'DBConnection.php';
if (isset($_POST['busca'])){
	$queryString = $_POST['busca'];	
    $sql = new contomssql();
	$s=$sql->query("SELECT TOP 10 m.pessoas_id, m.nome FROM (SELECT DISTINCT  p.pessoas_id , p.nome FROM tbl_Pessoas p where p.nome LIKE '$queryString%') m","");
	$s1 = $sql->objects("",$s);
	do {
		echo '<ul>';
        echo '<li>'.$s1->pessoas_id.' - '.$s1->nome.'</li>';
        echo '</ul>'; 
    } while($s1 = $sql->objects('',$s));
	} else{
    	echo 'Error try again';
	}
	?>

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
i tend to return data to jquery using json and then reference the object in the receiving code

your query will/may produce multiple results. so which of these results will you use to fill the id attribute of #id?
 
Jpadie,

For the id attribute I would like to use pessoas_id from the combination m.pessoas_id, m.nome selected from the result set, which at this time it's limited to 10 rows.
I may have forgotten to mention that the database is MS SQL Server 2008 so the query is written using T-SQL.

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
Kind of related, but kind of not... I've been playing around with the Yii PHP framework for the past few weeks. It does for PHP what Groovy & Grails did for Java, and wow - it rocks!

The related bit: Its ORM support makes writing SQL queries such as that redundant, and allows such structured code that I've begun to find writing PHP a joy rather than a chore.

Also with CRUD support, massive amounts of security support, I could go on and on, but this post would sound as if I were on commission (which I'm not :)).

If either of you fancy trying it, I can highly recommend reading through the entire "The definitive guide to Yii" online (if you can stop yourself wanting to keep going to try bts of code out).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
have you tried the jquery forum? this is usually the best place to start when working with framework

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top