Jump to content

ajax/php passing variables and limiting queries


imperialized

Recommended Posts

Ok, so I am working on trying to make a working AJAX script. It works in the context that it executes the query, however the LIMIT is wrong.

 

main page:

 

<?php

/**
* @author David Lallone
* @content AJAX/MYSQL submit/retrieve test
* @copyright 2008
*/
?>

<html>
<head>

</head>
<body>
This is a Form: <br>
<form name="ajax" action="ajax_form.php?action=Submit&page=<?php echo $pg; ?>" METHOD="post">
<input name="name" type=text><br>
<input name="message" type=text><br>
<input type="submit" value="submit">
<script src="sortfilter.js"></script>
<div id="content">
<br>
<!-- This is where the JAVASCRIPT will display the data --!>
<?php
include("db_con.php");

$pg = $_GET['page'];
  if ($pg == "") {
  	$pg = 1; 
$start=0; 
$end=16;
 }
  if ($pg == 1){
  		$start=0; 
    $end=16;
} else {
  	$start = $pg * 16 - 15;
  	$end = $pg * 16;
}

$next_pg = $pg+1;
$prev_pg = $pg-1;

//Submitting info to DB
/*
if($_GET['action'] == "Submit"){

$name = $_POST['name'];
$message = $_POST['message'];

$insert = "INSERT INTO ajax (name, message) VALUES ('$name', '$message')";
$insert_query = mysql_query($insert) OR DIE("Error: ". mysql_error());

include("db_con.php");
$query = "SELECT * FROM ajax LIMIT $start, $end";
$go = mysql_query($query);
if(mysql_num_rows($go) != 0){
while($x = mysql_fetch_array($go)){
$name = $x['name'];
$message = $x['message'];

print "<b>$name</b>:  $message <br>";

   }
} else {
print "No Messages Have Been Left";
}




} else {


*/ 

//Not Submitting info	
Print "Page: $pg   Start: $start   End: $end <br><br>";
$query = "SELECT * FROM ajax LIMIT $start, $end";
$go = mysql_query($query);
$count = mysql_num_rows($go);
if(mysql_num_rows($go) != 0){
while($x = mysql_fetch_array($go)){
$name = $x['name'];
$message = $x['message'];

print "<b>$name</b>:  $message <br>";
   }
} else {
print "No Messages Have Been Left";
  }
  print "Total Results Returned: $count <br>";
  Print " Next Button: <input type=button name='next' value='Next' onclick='Next($next_pg)'> ";
// }	
?>
</div>

 

 

 

:: page.php (exceuted using ajax)

 

<?php

/**
* @author 
* @copyright 2008
*/
include("db_con.php");

$pg = $_GET['page'];

print "Page = $pg ";

  if ($pg == "") {
  	$pg = 1; 
$start=0; 
$end=16;
 }
  if ($pg == 1){
  		$start=0; 
    $end=16;
} else {
  	$start = $pg * 16 - 15;
  	$end = $pg * 16;
}

$next_pg = $pg+1;
$prev_pg = $pg-1;

Print "Start: $start   End: $end <br><br>";
$query = "SELECT * FROM ajax LIMIT $start, $end";
$go = mysql_query($query);
$count = mysql_num_rows($go);
if(mysql_num_rows($go) != 0){
while($x = mysql_fetch_array($go)){
$name = $x['name'];
$message = $x['message'];

print "<b>$name</b>:  $message <br>";
   }
} else {
print "No Messages Have Been Left";
  }
print "Total Results Returned: $count <br>";
Print " Next Button: <input type=button name='next' value='Next' onclick='Next($next_pg)'> ";    
?>

 

 

an example of this page can be found at: http://www.imperialized.net/ajax/ajax_form.php

 

I have it set to display the $start and $end variables, and they are correct. But as you see, the page results vary for some reason.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.