www.webdeveloper.com
Results 1 to 9 of 9

Thread: Php ajax with mysql if else error

  1. #1
    Join Date
    Aug 2010
    Posts
    58

    Cool Php ajax with mysql if else error

    I recently made an ajax/php script to query a database.
    I had the ajax page and the request page seperate unlil I got it working but when I did merge then I came about a problem.

    The problem arises at the merge in the if else statement. I have
    PHP Code:

    <?php
    if ($_SERVER['QUERY_STRING']){.....etc.....};
    else { 
    ?>

    //start of html
    <html>
    <head>
    ....etc....
    I canneot get this to work,
    any help would be appreciated,
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    We may need more info on what you mean by it not working: what does it do that you don't want it to do or not do that you want it to do?

    One suggestion: use the empty() function to test the $_SERVER value, just in case it's not set at all, e.g.:
    PHP Code:
    <?php
    if(!empty($_SERVER['QUERY_STRING'])) {
       
    // do something with query string
    }
    else {
    ?>
    <!-- some HTML -->
    <?php
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2010
    Posts
    58
    it returns an, unexpected else, error... I have tried putting in a ; in places but no luck

  4. #4
    Join Date
    Oct 2010
    Posts
    25
    I notice that you have a ";"-character at the end of your
    if ($_SERVER['QUERY_STRING']){.....etc.....}
    it shouldn't be there

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    For a syntax error such as that we'll probably need to see the actual code, not a "brief synopsis". But "andx85" has a good point if your actual code is written that way witha ";" following the "}".
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Nov 2006
    Posts
    14
    Yeah, the "unexpected else" is definitely coming from that semi-colon at following the closing curly bracket at the end of the if statement.

    Also, not sure what the rest of your page looks like, but it's good practice (IMO) to keep the business logic separate from the presentation. So you could do something like this...
    PHP Code:
    <?
    if ($_SERVER['QUERY_STRING']) {
      
    // something
    } else include_once "./file1.html"?>
    Just my two cents!

  7. #7
    Join Date
    Aug 2010
    Posts
    58
    This is the section of my code at the join:
    PHP Code:
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell){
            
            
    $toreturn.= "<td>$cell</td>";}

        
    $toreturn.= "</tr>\n";
    }
    $toreturn.= "</table></div>";
    return 
    $toreturn;
    }
    else
    {
        

     
    ?>


    <html>
    <style type="text/css">
    h1{text-transform:capitalize;text-align:center}
    th{width:100px} 

  8. #8
    Join Date
    Aug 2010
    Posts
    58
    It works fine if I replace the 'else' with 'if(empty($_SERVER["QUERY_STRING"]))' ......
    But it still return an "unexpected else" error

  9. #9
    Join Date
    Aug 2010
    Posts
    58
    [SOLVED]
    I had a function in between the 'if' and the 'else' ....
    Sorry for wasting everyones time(& my noobishness)
    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles