www.webdeveloper.com
Results 1 to 5 of 5

Thread: Ajax/mysql query

  1. #1
    Join Date
    May 2009
    Posts
    40

    Ajax/mysql query

    I have the following script, which is meant to create a query to a mysql database:

    Code:
    <script type="text/javascript">
    
    //Browser Support Code
    function ajaxFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	
    var queryString = "?id=" + id;
    	
    ajaxRequest.open("GET","getuser.php?q="+str,true);
    ajaxRequest.send();
    }
    </script>
    Then I have this php page:

    PHP Code:
    <?php
    $q
    =$_GET["q"];

    $con mysql_connect('localhost''peter''abc123');
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("ajax_demo"$con);

    $sql="SELECT * FROM user WHERE id = '".$q."'";

    $result mysql_query($sql);

    echo 
    "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['FirstName'] . "</td>";
      echo 
    "<td>" $row['LastName'] . "</td>";
      echo 
    "<td>" $row['Age'] . "</td>";
      echo 
    "<td>" $row['Hometown'] . "</td>";
      echo 
    "<td>" $row['Job'] . "</td>";
      echo 
    "</tr>";
      }
    echo 
    "</table>";

    mysql_close($con);
    ?>
    These are obviously snippets, but I don't know how to get them to work together on my page. The actual fields in my db are id, quote, and date. What I need to do is display whatever the highest id number first, and then navigate through each id increment.

  2. #2
    Join Date
    Jun 2010
    Posts
    4

    Thumbs down

    Hi!
    I don't see where our problem is... Are you saying that the data is not getting to PHP?? or that your query is not getting what you need??
    In the first case, I strongly recommend a "well-known" developer practice that is to "echo" each variable received from the AJAX script and also to create a JAVASCRIPT message with the string that you are sending to see where the data is lost.

    Best Regards!

  3. #3
    Join Date
    May 2009
    Posts
    40
    Ah, ok.

    Here's my page:
    http://2stepsdown.com

    It's supposed to mimic the effect of that js. So here are the things I don't know how to do. First, the ajax is calling a .php page, where what I need is that the php by on the same page as the js, as that's where the output needs to be displayed.

    Second, is how to calculate the id var, to mimic the same navigation I now have just using an array.

    Sorry, I should have asked the question better.

  4. #4
    Join Date
    May 2010
    Posts
    109
    rlinsurf:

    The best way to do this is to build and echo XML, then parse the XML.

    Example attached..
    Attached Files Attached Files

  5. #5
    Join Date
    Jun 2007
    Posts
    667
    You need a readystatechange handler to read the data returned from the request. https://developer.mozilla.org/en/ajax/getting_started
    Y_U U_G_A_E_U_ B_S_A_D_

Thread Information

Users Browsing this Thread

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

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