skip to Main Content

Have this as a part in a ajax search page

index.php

    $(document).ready(function(){  
        $('#user').keyup(function(){  
            var query = $(this).val();  
            if(query != '')  
            {  
                $.ajax({  
                    url:"search.php",  
                    method:"POST",  
                    data:{query:query},  
                    success:function(data)  
                    {  
                        $('#postList').fadeIn();  
                        $('#postList').html(data);  
                    }  
                });  
            }  
        });  
  
    }); 

search.php

$output .= '<div class="bild"><img src="'.$row["URL"].'" width="200" height="300" alt="'.$row["Titel"].'" onError="this.onerror=null;this.src="loading.jpg";"/></div>';
$output .= '</div>'; 

The problem is that i get a space in index.php when i activate it

this.src=" loading.jpg"

Anyone have any good explanation for why this is and also im happy if someone can get me in right path 🙂

Thanks

enter image description here

Need to get away the space ..

this.src="loading.jpg"

2

Answers


  1. Here you have double quotes inside double quotes, change one of the to single quotes or escape the ones inside:

    onError="this.onerror=null;this.src="loading.jpg";"
    

    Try to do this:

    onError="this.onerror=null;this.src="loading.jpg";"
    

    or this:

    onError="this.onerror=null;this.src='loading.jpg';"
    

    or this:

    onError="this.onerror=null;this.src='loading.jpg';"
    

    One of them should fix it, just try with your code which one is best.

    Login or Signup to reply.
  2. You quotations marks for loading.jpg are not escaped, thereby it is treating the loading.jpg as an attribute of the div tag and not as a src parameter of this. Replace the quotes around loading.jpg with ' and it should work.

    I quickly typed the example in an interactive PHP shell to show you the effect.

    First what your result was (you see that loading.jpg is in a different color highlighted):

    php > $output = '<div class="bild"><img src="'.$row["URL"].'" width="200" height="300" alt="'.$row["Titel"].'" onError="this.onerror=null;this.src="loading.jpg";"/></div>';
    php > echo($output);
    <div class="bild"><img src="http:demo.com" width="200" height="300" alt="Demo" onError="this.onerror=null;this.src="loading.jpg";"/></div>
    

    And now my fix:

    php > $output = '<div class="bild"><img src="'.$row["URL"].'" width="200" height="300" alt="'.$row["Titel"].'" onError="this.onerror=null;this.src='loading.jpg';"/></
    div>';
    php > echo($output);
    <div class="bild"><img src="http:demo.com" width="200" height="300" alt="Demo" onError="this.onerror=null;this.src='loading.jpg';"/></div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search