Hi guys,

I have a table that outputs all my contacts via a while loop from my database.

my syntax is like this:

SELECT * FROM contacts WHERE id = $_SESSION['user_id'] ORDER BY name ASC LIMIT 5

that pulls out all my data and only gives me 5 results.

Now my goal is to have a little button that opens up a model box with jquery (this I can manage on my own) with a form asking the user to input a number then that number will be sent via post or get to $PHP_SELF and update a local variable with the number the user inputed, then that variable will be used to update the database to increase or decrease the LIMIT value.

I have looked all over the web (with google) to look for submitting a form using AJAX but all the examples i've found don't work for me.

When the user submits the number and the sql query is executed and updated for the outputed table to dynamically update according to the new LIMIT value all without ever refreshing the page to the user.

my jquery code is:

$(document).ready(function(){
				$("form#form").submit(function() {
					// we want to store the values from the form input box, then send via ajax below
					var val = $('input[name=new_value]').attr('value');
					
					$.ajax({
						type: "post",
						url: "process.php",
						data: "val="+ val,
						cache: false,
						success: function(){
							$('form#form').hide(function(){$('.success').fadeIn();});
						}
					});
				return false;
				});
			});

then my php code is:

$new_val = $_POST['new_val'];

$_val  = "UPDATE `settings` SET `display_limit` = {$new_val} WHERE `user_id` = {$_SESSION['user_id']}";
mysql_query($_val) or die(mysql_error());
}

and my form is simple:

<form id="form">
							<input type="text" id="new_val" name="new_val" class="text" />
							<input type="submit" name="limit" id="submit" />
						</form>

any suggestions? I haven't come to how to have my outputed table dynamically update yet so if anyone can point me in the right direction or provide some help that would be awesome.

thanks

For one, your PHP code does not return anything. You should construct an array of all your results and echo them with json_encode() or just output HTML.

Second. Your success function does not contain a data parameter. You can use this to access the returned json or html and output them.

Hi, ok i fixed alot in the mean time

this is my PHP code:

$json = array('rows' => array());

	while ($row = mysql_fetch_assoc($last_five_listings)) {
		$json['rows'][] = $row;
	}

	echo json_encode($json);

and this is my new jquery:

$(function(){
				$("#form").submit(function(){
					var val = $("input[name=recent_val-seller]").val();
					$.getJSON("process.php?val=" + val, function(data){
						$("#last_five_sellers").empty();
						$("#last_five_sellers").append('<table>');
						$(data.rows).each(function(){
							$("#last_five_sellers").append('<tr><td>' + this.column_a + '</td><td>' + this.column_b + '</td></tr>');
						});
						$("#last_five_sellers").append('</table>');
					});
					return false;
				});
			})
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.