0

Hi,

So basically I have a php file that fetches database records and displays it. Now i would like to add the ability to edit each record using lightbox.

here is the main file

<?php
$username="root";
$password="";
$database="lhs_anniversary_banquet";

mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

	<script type="text/javascript">
		$(document).ready(function() {
			$(".dialog_link").fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade'
			});	
		
			var variblevalue = $('.dialog_link').attr('href');
			var variblefixed = variblevalue.substr(1);

			$("#form_submit"+variblefixed).submit(function() {
			// we want to store the values from the form input box, then send via ajax below

			
			var id     = $('.id').attr('value');
			var email     = $('#email'+variblefixed).attr('value');
			alert(email);
			var cell     = $('#cell'+variblefixed).attr('value');
				$.ajax({
					type: "POST",
					cache	: false,
					url: "ajax.php",
					data: "email="+ email +"& cell="+ cell +"& id="+ id,
					success: function(){
						$('#form_submit'+variblefixed).hide(function(){$('div.success').fadeIn();});
					}
				});
			return false;
			
			
		});	
	});		

	</script>

<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<th>Name</th>
		<th>Surname</th>
		<th>Nickname</th>
		<th>Class</th>
		<th>Email</th>
		<th>Cell</th>
		<th></th>
	</tr>

	<?php
	$i=0;
	while ($i < $num) {
	
	$f1=mysql_result($result,$i,"id");
	$f2=mysql_result($result,$i,"Name");
	$f3=mysql_result($result,$i,"Surname");
	$f4=mysql_result($result,$i,"Nickname");
	$f5=mysql_result($result,$i,"Class");
	$f6=mysql_result($result,$i,"Email");
	$f7=mysql_result($result,$i,"Cell");
	?>

	<tr>
		<td><?php echo $f2; ?></td>
		<td><?php echo $f3; ?></td>
		<td><?php echo $f4; ?></td>
		<td><?php echo $f5; ?></td>
		<td><?php echo $f6; ?></td>
		<td><?php echo $f7; ?></td>
	<td>

		<a href="#<?php echo $f1; ?>" class="dialog_link">Edit</a>
		<div style="display: none;">
			<div id="<?php echo $f1; ?>" title="Update this record" style="width:400px;height:160px;overflow:auto; border: 4px solid #ff5501; padding:10px">
			<h2><?php echo $f2; ?> <?php echo $f3; ?></h2>
			<p>Fill in the information below and click save changes</p>
			<br/>
			<form id="form_submit<?php echo $f1; ?>" method="post">
					<fieldset>
						<input name="id" value="<?php echo $f1; ?>" class="id">
						<table cellpadding="0" cellspacing="0">
							<tr>
								<td><label for="email<?php echo $f1; ?>">Email Address:</label>
								</td>
								<td><input id="email<?php echo $f1; ?>" class="text" name="email" size="20" type="text" value="<?php echo $f6; ?>">
								</td>
							</tr>
							<tr>
								<td><label for="cell<?php echo $f1; ?>">Cell:</label>
								</td>
								<td><input id="cell<?php echo $f1; ?>" class="text" name="cell" size="20" type="text" value="<?php echo $f7; ?>">
								</td>
							</tr>						
						</table>
						<br/>
						<input type="submit" name="mysubmit" value="Save Changes" class="button"> 
					</fieldset>
			</form>		
			<div class="success" style="display: none;">This record has been updated.</div> 
			</div>
		</div>

	</td>


</tr>

<?php
$i++;
}
?>

Onsubmit i use the following;

<?php

	$username="root";
	$password="";
	$database="lhs_anniversary_banquet";
	
	mysql_connect("localhost",$username,$password);
	@mysql_select_db($database) or die( "Unable to select database");

	// CLIENT INFORMATION
	$id        = $_POST['id'];
	$email        = $_POST['email'];
	$cell        = $_POST['cell'];

    $updateuser  = "UPDATE students SET Email='$email', Cell='$cell' WHERE id='$id'";
    echo $updateuser;

    mysql_query($updateuser) or die(mysql_error());
?>

Now this works perfectly with one record. Think it might be an ID issue. Not too sure though. Any help would be great. Thanks

Edited by macneato: n/a

1
Contributor
1
Reply
4
Views
7 Years
Discussion Span
Last Post by macneato
0

Guessing the issue had something to do with the ID. Nevertheless, what i have did was. Change my lightbox to be launched from a ajax window (passing the variable via url) then ran the script from there. Worked like a charm.

Think i will get a Solved Threads point for solving my own? :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.