probleem met ajax

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ralph van der Tang

ralph van der Tang

15/04/2013 14:45:56
Quote Anchor link
hoi iedereen,

ik heb een probleem met mijn ajax.

het probleem is het volgende.

ik heb een zoek functie geschreven die dmv een ajax call op elke key up gegevens opmoet halen.
nu is het probleem dat de success niet wordt bereikt
alles wat ik in de error: van mijn call zet wordt uitgevoerd

javascript
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
    // live search
    $("input#text_search").live("keyup", function(e){
        // Set Timeout
        clearTimeout($.data(this, 'timer'));

        // searchstring
        var search_string = $(this).val();
        
        // Do nothing
        if (search_string == '')
        {

            $("div#text_results").fadeOut();
        }
else{
            // Do Search
            $("div#text_results").fadeIn();
            $(this).html('timer', setTimeout(search, 100));
        };

        
        // zoeken
        if(search_string !== '' )
        {

            $.ajax({
                type:"POST",
                url:"<?php echo Settings::$url;?>/"+ReadCookie('lang')+"/adminajax/content/textsearch",
                data:    {
                            query: search_string
                        },
                cache: false,
                success: function(data)
                {

                        alert('xxxxxx');
                    
                    //$("div.admin_text_table").css();
                },
                error:function(data)
                {

                alert('fail');
                }
            });
        }
return false;
    });


?>


controller
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
            $html
="<tr><th>Content</th><th>Tag</th>><th>Language</th><th class='small'></th><th class='small'></th>";
            
                        
            // ophalen search termen
            $search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
            
            
            // controleren op inhoud
            if(strlen($search_string)>= 1 && $search_string !== ' ')
            {

                // ophalen tekstgegevens
                $query="SELECT * FROM `tagtexts` WHERE `tag_name` LIKE '%".$search_string."%'  OR `content` LIKE '%".$search_string."%' ";
                $result = Database::execute($query);
                // ophalen language
                //$lang="SELECT `language` FROM `languages` WHERE language_id=".$result['language_id']."";
                //$language=Database::execute($lang);

                
                foreach($result as $res)
                {

                    $html.="<tr id='".$res['id']."'><td>".$res['content']."</td><td>".$res['tag_name']."</td>";    
                }

                
                /*foreach($language as $lang)
                {
                    $html.="<td>".$lang['language']."</div>";
                    $html.="</td><td class='td_small'><img src='<?php echo Settings::$url;?>/images/pencil.png' alt='aanpassen' onclick='makeChange($(this).parent().parent().attr(\'id\'))' /></td><td class='td_small'><img src='<?php echo Settings::$url;?>/images/delete.png' alt='verwijderen' onclick='deleteText($(this).parent().parent().attr(\'id\'))' /></td></tr>";
                }
                */

                echo $html;
                echo json_encode(array("success"=> true));
            }


?>


html
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
$arr
['html'] = '<div class="admin_text_search">Zoeken:<input type="text" id="text_search" autocomplete="off"></div><div class="admin_text_table"></div><div id="text_results"></div>'
?>


iemand suggesties
 
PHP hulp

PHP hulp

22/12/2024 19:33:22
 
Eddy E

Eddy E

15/04/2013 15:04:47
Quote Anchor link
Ik ben ben geen Javascript-deskundige.
Maar dit lijkt mij niet logisch:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
                echo $html;
                echo json_encode(array("success"=> true));
?>


Regel 31.
 
Robert Wazzaa

Robert Wazzaa

15/04/2013 15:38:04
Quote Anchor link
Die eens een alert op data.responseText in je error functie, dan kun je zien of/wat er mis gaat in je PHP script.

Toevoeging op 15/04/2013 15:38:46:

Dit:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
               error:function(data)
                {
                alert('fail');
                }

Word dan:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
               error:function(data)
                {
                alert(data.responseText);
                }


Toevoeging op 15/04/2013 15:40:28:

En als je data terug wilt sturen als json en zo wil gebruiken met ajax, moet je in je ajax call even de dataType: 'json' toevoegen, en in je php dit boven je echo zetten:
header('Content-type: application/json');
 
Ralph van der Tang

ralph van der Tang

16/04/2013 11:43:15
Quote Anchor link
code is veranderd dit is de code nu
controler
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
        if($this->arguments[0] == "textsearch")
        {

            //$html="<tr><th>Content</th><th>Tag</th>><th>Language</th><th class='small'></th><th class='small'></th>";
                        
            // ophalen search termen

            $search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
            
            
            // controleren op inhoud
            if(strlen($search_string)>= 1 && $search_string !== ' ')
            {

                // ophalen tekstgegevens
                $query="SELECT `id`,`languages_id`,`content`,`tag_name` FROM `tagtexts` WHERE `tag_name` LIKE '%".$search_string."%'  OR `content` LIKE '%".$search_string."%' ";
                $result = Database::execute($query);
                
                foreach($result as $res)
                {

                    // ophalen language
                    $lang="SELECT `language` FROM `languages` WHERE id=".$result[0]["languages_id"]."";
                    $language=Database::execute($lang);
                    $result[]=str_replace($result[0]['languages_id'],$language[0]['language'],'');
                        
                }

                var_dump($result);
                //echo json_encode($result);
            }
?>


javascript :
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
    $("input#text_search").live("keyup", function(e){
        // Set Timeout
        clearTimeout($.data(this, 'timer'));

        // searchstring
        var search_string = $(this).val();
        
        // Do nothing
        if (search_string == '')
        {

            $("#text_results").fadeOut();
        }
else{
            // Do Search
            $("div#text_results").fadeIn();
        };

        
        // zoeken
        if(search_string !== '' )
        {

            $.ajax({
                type:"POST",
                url:"<?php echo Settings::$url;?>/"+ReadCookie('lang')+"/adminajax/content/textsearch",
                data:    {
                            query: search_string
                        },
                cache: false,
                success: function(data)
                {

                    $(".admin_text_table").hide();
                    $(".text_results").html(data);
                    $(".text_results").sortableTable({
                        pageItems: 10,
                        sortableColumns: Array('tag', 'languages_id','content'),
                        showAllColumns: Array('tag', 'language','content'),
                        colorEven: '#EFEFEF',
                        colorOdd: '#FFF',
                        customHtml: '<td class="td_small"><img src="<?php echo Settings::$url;?>/images/pencil.png" alt="aanpassen" onclick="makeChange($(this).parent().parent().attr(\'id\'))" /></td><td class="td_small"><img src="<?php echo Settings::$url;?>/images/delete.png" alt="verwijderen" onclick="deleteText($(this).parent().parent().attr(\'id\'))" /></td>',    
                    },
                    {

                        jsonData: data.data
                    })    
                }
            });
        }
return false;
    });


?>


Toevoeging op 16/04/2013 11:43:18:

code is veranderd dit is de code nu
controler
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
        if($this->arguments[0] == "textsearch")
        {

            //$html="<tr><th>Content</th><th>Tag</th>><th>Language</th><th class='small'></th><th class='small'></th>";
                        
            // ophalen search termen

            $search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
            
            
            // controleren op inhoud
            if(strlen($search_string)>= 1 && $search_string !== ' ')
            {

                // ophalen tekstgegevens
                $query="SELECT `id`,`languages_id`,`content`,`tag_name` FROM `tagtexts` WHERE `tag_name` LIKE '%".$search_string."%'  OR `content` LIKE '%".$search_string."%' ";
                $result = Database::execute($query);
                
                foreach($result as $res)
                {

                    // ophalen language
                    $lang="SELECT `language` FROM `languages` WHERE id=".$result[0]["languages_id"]."";
                    $language=Database::execute($lang);
                    $result[]=str_replace($result[0]['languages_id'],$language[0]['language'],'');
                        
                }

                var_dump($result);
                //echo json_encode($result);
            }
?>


javascript :
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
    $("input#text_search").live("keyup", function(e){
        // Set Timeout
        clearTimeout($.data(this, 'timer'));

        // searchstring
        var search_string = $(this).val();
        
        // Do nothing
        if (search_string == '')
        {

            $("#text_results").fadeOut();
        }
else{
            // Do Search
            $("div#text_results").fadeIn();
        };

        
        // zoeken
        if(search_string !== '' )
        {

            $.ajax({
                type:"POST",
                url:"<?php echo Settings::$url;?>/"+ReadCookie('lang')+"/adminajax/content/textsearch",
                data:    {
                            query: search_string
                        },
                cache: false,
                success: function(data)
                {

                    $(".admin_text_table").hide();
                    $(".text_results").html(data);
                    $(".text_results").sortableTable({
                        pageItems: 10,
                        sortableColumns: Array('tag', 'languages_id','content'),
                        showAllColumns: Array('tag', 'language','content'),
                        colorEven: '#EFEFEF',
                        colorOdd: '#FFF',
                        customHtml: '<td class="td_small"><img src="<?php echo Settings::$url;?>/images/pencil.png" alt="aanpassen" onclick="makeChange($(this).parent().parent().attr(\'id\'))" /></td><td class="td_small"><img src="<?php echo Settings::$url;?>/images/delete.png" alt="verwijderen" onclick="deleteText($(this).parent().parent().attr(\'id\'))" /></td>',    
                    },
                    {

                        jsonData: data.data
                    })    
                }
            });
        }
return false;
    });


?>


Toevoeging op 16/04/2013 11:44:31:

als ik data in de javascript alert krijg ik alleen objecten terug

Toevoeging op 16/04/2013 12:34:28:

heb nog een andere vraag ik stuur in mijn controler een array terug en wil deze stijlen mat java script enig idee hoe dat het makkelijkst te doen is?
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.