SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ajax suggest example don't work

    I have been trying an ajax suggest example I downloaded from the web, and get this error:
    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Server\Apache2\htdocs\apet\suggest\suggest.php on line 11

    This is line 11:
    Code:
    echo "<a href='javascript:;' onclick=\"q.value='".$row['petname']."'">".$row['petname'].\"</a><br />";
    I've tryed backslashing various double quotes, not backslasing any, and I even tryed q.value and document.q.value. Can someone help, I'd like to get it working.

    Code for html
    Code:
    <html>
    <head>
    <title>Update Record</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <LINK href="include/style.css" type=text/css rel=stylesheet>
    <script language="javascript"> 
    
    function createRequestObject() { 
    
    var req; 
    
    if(window.XMLHttpRequest){ 
    // Firefox, Safari, Opera... 
    req = new XMLHttpRequest(); 
    } else if(window.ActiveXObject) { 
    // Internet Explorer 5+ 
    req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } else { 
    // There is an error creating the object, 
    // just as an old browser is being used. 
    alert('Problem creating the XMLHttpRequest object'); 
    } 
    
    return req; 
    
    } 
    
    // Make the XMLHttpRequest object 
    var http = createRequestObject(); 
    
    function sendRequest(q) { 
    
    // Open PHP script for requests 
    http.open('get', 'suggest.php?q='+q); 
    http.onreadystatechange = handleResponse; 
    http.send(null); 
    
    } 
    
    function handleResponse() { 
    
    if(http.readyState == 4 && http.status == 200){ 
    
    // Text returned FROM the PHP script 
    var response = http.responseText; 
    
    if(response) { 
    // UPDATE ajaxTest content 
    document.getElementById("searchResults").innerHTML = response; 
    } 
    
    } 
    
    } 
    
    </script> 
    
    
    </head>
    <body>
    
    <form method="post" id="editform" name="editform">
    <input name="q" id="q" type="text" id="q" style="width:300px" onkeyup="sendRequest(this.value);" /> 
    <div id="searchResults" style="width:295px; padding:5px; border:1px solid #000000"></div>
    
    </form>
    
    </body>
    </html>
    Code for suggest.php
    Code:
    <?php 
    mysql_connect("localhost", "root", "sa"); 
    mysql_select_db("petback2"); if(isset($_GET['q'])) { 
    
    $searchString = $_GET['q']; 
    $sql = mysql_query("SELECT * FROM tablename WHERE field LIKE '".$_GET['q']."%' ORDER BY anotherfield ASC"); 
    
    if($searchString != NULL) { 
    
    while($row = mysql_fetch_assoc($sql)) { 
    echo "<a href='javascript:;' onclick=\"q.value='".$row['petname']."'">".$row['petname'].\"</a><br />"; 
    } 
    
    } 
    
    if(mysql_num_rows($sql) == 0) { 
    echo "<i>No results found</i>"; 
    } 
    
    } 
    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks to me like
    Code:
    echo "<a href='javascript:;' onclick=\"q.value='".$row['petname']."'">".$row['petname'].\"</a><br />";
    should be:

    Code:
    echo "<a href='javascript:;' onclick=\"q.value='".$row['petname']."'\">".$row['petname']."</a><br />";
    I think some of the " are improperly escaped, which is usually what that type of error means.

    Dave


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •