jQuery autocomplete aanvullen dmv database gegevens
GroundZero - 26/07/2012 13:16
Lid
Beste,
wanneer ik het volgende doe dan werkt het perfect:
<script language="javascript" type="text/javascript">
$(function() {
var availableTags = [
"test",
"abcde"
];
$( "#branche" ).autocomplete({
source: availableTags
});
});
</script>
<script language = "javascript" type= "text/javascript" >
$( function ( ) {
var availableTags = [
"test" ,
"abcde"
] ;
$( "#branche" ) . autocomplete( {
source: availableTags
} ) ;
} ) ;
</script>
Wanneer ik het volgende doe dan werkt het dus niet meer! de functie:
function autocompleteBranches(){
$query = mysql_query('SELECT DISTINCT branche FROM site_freelancers
ORDER BY branche ASC');
$rows = mysql_num_rows($query);
$counter = 1;
while($fetch = mysql_fetch_assoc($query))
{
// without comma or with comma
if($counter == $rows)
{
echo '"'.$fetch['branche'].'"
';
}
else
{
echo '"'.$fetch['branche'].'",
';
}
$counter++;
}
}
function autocompleteBranches( ) {
$query = mysql_query ( 'SELECT DISTINCT branche FROM site_freelancers ORDER BY branche ASC' ) ;
$counter = 1 ;
{
// without comma or with comma
if ( $counter == $rows )
{
echo '"' . $fetch [ 'branche' ] . '" ' ;
}
else
{
echo '"' . $fetch [ 'branche' ] . '", ' ;
}
$counter ++;
}
}
De autocomplete code:
<script language="javascript" type="text/javascript">
$(function() {
var availableTags = [
"test",
<?php
autocompleteBranches();
?>
];
$( "#branche" ).autocomplete({
source: availableTags
});
});
</script>
<script language="javascript" type="text/javascript">
$(function() {
var availableTags = [
"test",
<?php
autocompleteBranches( ) ;
?>
];
$( "#branche" ).autocomplete({
source: availableTags
});
});
</script>
Momenteel is er maar één hit, maar hij print wel netjes "jklbms" in de code. Maar toch gebeurd er niks wanneer ik één van die letters typ.
Weet iemand wat het probleem zou kunnen zijn misschien?
7 antwoorden
Gesponsorde links
Koen - 26/07/2012 13:54
PHP expert
hoe ziet de broncode eruit die gegenereerd werd?
Koen - 26/07/2012 15:46
PHP expert
Met de code die je liet zien is er geen probleem. Je zal meer code moeten laten zien: de pagina die je aanroept via ajax, en de javascript code die je daarvoor gebruikt.
GroundZero - 26/07/2012 16:09 (laatste wijziging 26/07/2012 16:10)
Lid
Oke, de volgende code is even snel gedaan, functies komen straks in een apart bestand.
De pagina die weergegeven word:
<?=isLogingValid($_COOKIE[COOKIE])?>
<?=displayText($_GET['page'])?>
<?php
$userinfo = getUserInformation($_COOKIE[COOKIE]);
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
mysql_query('UPDATE site_freelancers
SET
company = "'.mysql_real_escape_string($_POST['company']).'",
firstname = "'.mysql_real_escape_string($_POST['firstname']).'",
surname = "'.mysql_real_escape_string($_POST['surname']).'",
telephone = "'.mysql_real_escape_string($_POST['telephone']).'",
mobile = "'.mysql_real_escape_string($_POST['mobile']).'",
email = "'.mysql_real_escape_string($_POST['email']).'",
branche = "'.mysql_real_escape_string($_POST['branche']).'",
info = "'.mysql_real_escape_string($_POST['info']).'"
WHERE
id = "'.mysql_real_escape_string($userinfo[9]).'"') or die(mysql_error());
ob_clean();
header('location: '.ROOT.'account/edit-profile/');
}
?>
<div id="ajax-form">
</div>
<script language="javascript" type="text/javascript">
/* load on document load */
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'ajax/edit-profile.php',
success: function(html){
$('#ajax-form').html(html)
}
});
});
function verifyCompany(){
var company = document.getElementById('company').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyCompany.php',
data: {
company : company
},
success: function(html){
$('#companyIco').html(html);
}
});
}
function verifyFirstname(){
var firstname = document.getElementById('firstname').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyFirstname.php',
data: {
firstname : firstname
},
success: function(html){
$('#firstnameIco').html(html);
}
});
}
function verifySurname(){
var surname = document.getElementById('surname').value;
$.ajax({
type: 'POST',
url: 'ajax/verifySurname.php',
data: {
surname : surname
},
success: function(html){
$('#surnameIco').html(html);
}
});
}
function verifyTelephone(){
var telephone = document.getElementById('telephone').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyTelephone.php',
data: {
telephone : telephone
},
success: function(html){
$('#telephoneIco').html(html);
}
});
}
function verifyMobile(){
var mobile = document.getElementById('mobile').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyMobile.php',
data: {
mobile : mobile
},
success: function(html){
$('#mobileIco').html(html);
}
});
}
function verifyEmail(){
var email = document.getElementById('email').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyEmail.php',
data: {
email : email
},
success: function(html){
$('#emailIco').html(html);
}
});
}
</script>
<? = isLogingValid( $_COOKIE [ COOKIE] ) ?>
<? = displayText( $_GET [ 'page' ] ) ?>
<?php
$userinfo = getUserInformation( $_COOKIE [ COOKIE] ) ;
if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' )
{
SET
WHERE
header ( 'location: ' . ROOT
. 'account/edit-profile/' ) ; }
?>
<div id="ajax-form">
</div>
<script language="javascript" type="text/javascript">
/* load on document load */
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'ajax/edit-profile.php',
success: function(html){
$('#ajax-form').html(html)
}
});
});
function verifyCompany(){
var company = document.getElementById('company').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyCompany.php',
data: {
company : company
},
success: function(html){
$('#companyIco').html(html);
}
});
}
function verifyFirstname(){
var firstname = document.getElementById('firstname').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyFirstname.php',
data: {
firstname : firstname
},
success: function(html){
$('#firstnameIco').html(html);
}
});
}
function verifySurname(){
var surname = document.getElementById('surname').value;
$.ajax({
type: 'POST',
url: 'ajax/verifySurname.php',
data: {
surname : surname
},
success: function(html){
$('#surnameIco').html(html);
}
});
}
function verifyTelephone(){
var telephone = document.getElementById('telephone').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyTelephone.php',
data: {
telephone : telephone
},
success: function(html){
$('#telephoneIco').html(html);
}
});
}
function verifyMobile(){
var mobile = document.getElementById('mobile').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyMobile.php',
data: {
mobile : mobile
},
success: function(html){
$('#mobileIco').html(html);
}
});
}
function verifyEmail(){
var email = document.getElementById('email').value;
$.ajax({
type: 'POST',
url: 'ajax/verifyEmail.php',
data: {
email : email
},
success: function(html){
$('#emailIco').html(html);
}
});
}
</script>
het ajax formulier dat geladen word
<?php
require_once('..'.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'config.php');
require_once('..'.DIRECTORY_SEPARATOR.'language'.DIRECTORY_SEPARATOR.'NL_nl.php');
?>
<form name="edit-profiel" method="post" action="" enctype="multipart/form-data" class="space-above">
<fieldset>
<label><?=REGISTER_L2?></label>
<input type="text" name="company" id="company" value="<?=$userinfo[0]?>" class="input" onKeyUp="verifyCompany()" />
<span id="companyIco" class="aIco"></span>
<br />
<label><?=REGISTER_L3?></label>
<input type="text" name="firstname" id="firstname" value="<?=$userinfo[1]?>" class="input" onKeyUp="verifyFirstname()" />
<span id="firstnameIco" class="aIco"></span>
<br />
<label><?=REGISTER_L4?></label>
<input type="text" name="surname" id="surname" value="<?=$userinfo[2]?>" class="input" onKeyUp="verifySurname()" />
<span id="surnameIco" class="aIco"></span>
<br />
<label><?=REGISTER_L5?></label>
<input type="text" name="telephone" id="telephone" value="<?=$userinfo[3]?>" class="input" onKeyUp="verifyTelephone()" />
<span id="telephoneIco" class="aIco"></span>
<br />
<label><?=REGISTER_L6?></label>
<input type="text" name="mobile" id="mobile" value="<?=$userinfo[4]?>" class="input" onKeyUp="verifyMobile()" />
<span id="mobileIco" class="aIco"></span>
<br />
<label><?=REGISTER_L7?></label>
<input type="text" name="email" id="email" value="<?=$userinfo[5]?>" class="input" onKeyUp="verifyEmail()" />
<span id="emailIco" class="aIco"></span>
<br />
<label><?=REGISTER_L11?></label>
<input type="text" name="branche" id="branche" value="<?=$userinfo[6]?>" class="input" />
<br />
<label><?=REGISTER_L12?></label>
<textarea name="info" id="info" rows="7" class="input textarea"><?=$userinfo[10]?></textarea>
<br />
<label><?=FL_CP_FILE_1?></label>
<?php
if($userinfo[7] == ''){
echo 'geen bestand gevonden...';
} else {
echo $userinfo[7];
}
?>
<a href="<?=ROOT?>account<?=DIRECTORY_SEPARATOR?>edit-file<?=DIRECTORY_SEPARATOR?>1<?=DIRECTORY_SEPARATOR?>" title="<?=FL_CP_FILE_1?>">
<img src="<?=ROOT.'images'.DIRECTORY_SEPARATOR?>edit.png" alt="<?=FL_CP_EDIT_BUTTON?>" />
</a>
<br />
<label><?=FL_CP_FILE_2?></label>
<?php
if($userinfo[8] == ''){
echo 'geen bestand gevonden...';
} else {
echo $userinfo[8];
}
?>
<a href="<?=ROOT?>account<?=DIRECTORY_SEPARATOR?>edit-file<?=DIRECTORY_SEPARATOR?>2<?=DIRECTORY_SEPARATOR?>" title="<?=FL_CP_FILE_2?>">
<img src="<?=ROOT.'images'.DIRECTORY_SEPARATOR?>edit.png" alt="<?=FL_CP_EDIT_BUTTON?>" />
</a>
<br />
<input type="submit" name="submit" class="submit-button" value="<?=FL_CP_BUTTON?>" />
</fieldset>
</form>
<script language="javascript" type="text/javascript">
$(function() {
var availableTags = [
"test",
<?php
autocompleteBranches();
?>
];
$( "#branche" ).autocomplete({
source: availableTags
});
});
</script>
<?php
require_once ( '..' . DIRECTORY_SEPARATOR. 'includes' . DIRECTORY_SEPARATOR. 'config.php' ) ;
require_once ( '..' . DIRECTORY_SEPARATOR. 'language' . DIRECTORY_SEPARATOR. 'NL_nl.php' ) ;
?>
<form name="edit-profiel" method="post" action="" enctype="multipart/form-data" class="space-above">
<fieldset>
<label><? = REGISTER_L2?> </label>
<input type="text" name="company" id="company" value="<?= $userinfo [ 0 ] ?> " class="input" onKeyUp="verifyCompany()" />
<span id="companyIco" class="aIco"></span>
<br />
<label><? = REGISTER_L3?> </label>
<input type="text" name="firstname" id="firstname" value="<?= $userinfo [ 1 ] ?> " class="input" onKeyUp="verifyFirstname()" />
<span id="firstnameIco" class="aIco"></span>
<br />
<label><? = REGISTER_L4?> </label>
<input type="text" name="surname" id="surname" value="<?= $userinfo [ 2 ] ?> " class="input" onKeyUp="verifySurname()" />
<span id="surnameIco" class="aIco"></span>
<br />
<label><? = REGISTER_L5?> </label>
<input type="text" name="telephone" id="telephone" value="<?= $userinfo [ 3 ] ?> " class="input" onKeyUp="verifyTelephone()" />
<span id="telephoneIco" class="aIco"></span>
<br />
<label><? = REGISTER_L6?> </label>
<input type="text" name="mobile" id="mobile" value="<?= $userinfo [ 4 ] ?> " class="input" onKeyUp="verifyMobile()" />
<span id="mobileIco" class="aIco"></span>
<br />
<label><? = REGISTER_L7?> </label>
<input type="text" name="email" id="email" value="<?= $userinfo [ 5 ] ?> " class="input" onKeyUp="verifyEmail()" />
<span id="emailIco" class="aIco"></span>
<br />
<label><? = REGISTER_L11?> </label>
<input type="text" name="branche" id="branche" value="<?= $userinfo [ 6 ] ?> " class="input" />
<br />
<label><? = REGISTER_L12?> </label>
<textarea name="info" id="info" rows="7" class="input textarea"><?= $userinfo [ 10 ] ?> </textarea>
<br />
<label><? = FL_CP_FILE_1?> </label>
<?php
if ( $userinfo [ 7 ] == '' ) {
echo 'geen bestand gevonden...' ; } else {
}
?>
<a href="<? = ROOT?> account<? = DIRECTORY_SEPARATOR?> edit-file<? = DIRECTORY_SEPARATOR?> 1<? = DIRECTORY_SEPARATOR?> " title="<? = FL_CP_FILE_1?> ">
<img src="<? = ROOT. 'images' . DIRECTORY_SEPARATOR?> edit.png" alt="<? = FL_CP_EDIT_BUTTON?> " />
</a>
<br />
<label><? = FL_CP_FILE_2?> </label>
<?php
if ( $userinfo [ 8 ] == '' ) {
echo 'geen bestand gevonden...' ; } else {
}
?>
<a href="<? = ROOT?> account<? = DIRECTORY_SEPARATOR?> edit-file<? = DIRECTORY_SEPARATOR?> 2<? = DIRECTORY_SEPARATOR?> " title="<? = FL_CP_FILE_2?> ">
<img src="<? = ROOT. 'images' . DIRECTORY_SEPARATOR?> edit.png" alt="<? = FL_CP_EDIT_BUTTON?> " />
</a>
<br />
<input type="submit" name="submit" class="submit-button" value="<? = FL_CP_BUTTON?> " />
</fieldset>
</form>
<script language="javascript" type="text/javascript">
$(function() {
var availableTags = [
"test",
<?php
autocompleteBranches( ) ;
?>
];
$( "#branche" ).autocomplete({
source: availableTags
});
});
</script>
De functie welke alles in het javascript gedeelte plakt
function autocompleteBranches(){
$query = mysql_query('SELECT DISTINCT branche FROM site_freelancers
ORDER BY branche ASC');
$rows = mysql_num_rows($query);
$counter = 1;
while($fetch = mysql_fetch_assoc($query))
{
// without comma or with comma
if($counter == $rows)
{
echo '"'.$fetch['branche'].'"
';
}
else
{
echo '"'.$fetch['branche'].'",
';
}
$counter++;
}
}
function autocompleteBranches( ) {
$query = mysql_query ( 'SELECT DISTINCT branche FROM site_freelancers ORDER BY branche ASC' ) ;
$counter = 1 ;
{
// without comma or with comma
if ( $counter == $rows )
{
echo '"' . $fetch [ 'branche' ] . '" ' ;
}
else
{
echo '"' . $fetch [ 'branche' ] . '", ' ;
}
$counter ++;
}
}
Hopelijk heb je hier iets aan
Groetjes
Koen - 27/07/2012 10:45
PHP expert
Het probleem zit 'm erin dat alles tussen <script> en </script> standaard wordt genegeerd door jQuery.ajax(). De data wordt namelijk gewoon als string opgehaald.
Je moet dit tegengaan door een extra attribuut dataType: "html" mee te sturen met je AJAX request. Op deze manier zal deze string bij het ophalen reeds geinterpreteerd worden als zijnde HTML en wordt er een DOM (document object model) van opgebouwd. De bijgevoegde JavaScript wordt dan alsnog uitgevoerd op dit DOM.
Je code wordt dus zo:
$(document).ready(function(){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'ajax/edit-profile.php',
success: function(html){
$('#ajax-form').html(html)
}
});
});
$( document) .ready ( function ( ) {
$.ajax ( {
type: 'POST' ,
dataType: 'html' ,
url: 'ajax/edit-profile.php' ,
success: function ( html) {
$( '#ajax-form' ) .html ( html)
}
} ) ;
} ) ;
Hopelijk lost dit je probleem op. Anders zal je in de success-callback nog eens apart een request moeten uitvoeren die de autocomplete tags gaat ophalen.
Groeten,
Koen
Bedankt door: Martijn , GroundZero
GroundZero - 27/07/2012 12:20 (laatste wijziging 27/07/2012 14:29)
Lid
ahhhh kijk! goed om te weten haha... ik ga het vandaag proberen, ik laat je de uitkomst weten!
$(document).ready(function(){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'ajax/edit-profile.php',
success: function(html){
$('#ajax-form').html(html)
}
});
});
$( document) . ready( function ( ) {
$. ajax( {
type: 'POST' ,
dataType: 'html' ,
url: 'ajax/edit-profile.php' ,
success: function ( html) {
$( '#ajax-form' ) . html( html)
}
} ) ;
} ) ;
Zelfde resultaat, hij doet het nog steeds niks. Misschien haal ik de AJAX wel weg en gooi ik alles gewoon in het PHP document zelf. Zijn we meteen van dat probleem af ;)
Martijn - 27/07/2012 15:09
Crew PHP
Wat is me afvraag, waarom gooi je niet het zoekwoord naar php, laat je php met een beetje levensthein zoeken, en laat je php de autosuggest waardes terug geven?
Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.