Jump to content

Php And Ajax Mysql Database Example


Recommended Posts

I tried adding that to the top of my code and the screen went blank again so took it out.
I don't think you added it correctly, there's not an error in that code.
echo "<td>"<a href="java script:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a>"</td>";
You didn't copy that line correctly, go back to post 20 and check it again. Look at all the double quotes you have there.
Link to post
Share on other sites

Thanks for the reply. That was it, such a silly mistake!!Now instead of having ID's in my ID field I've got a delete button. It doesn't do anything though? I've added the function code in and get that blank screen again. I'm looking through the code and seeing if I've messed it up again but I may be placing it in the wrong place?

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script><link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" /><script type="text/javascript" src="tablecloth/tablecloth.js"></script></head><body background="backgrounda.PNG"><h2 align="center"><b><u><font size="8" face="COMIC SANS MS" font color="#38ACEC">Malik's Gym</font></b></u></h2><form><font color="#38ACEC"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Tahir Malik</option><option value="2">Khalid Mahmood</option><option value="3">Amanda Murphy</option><option value="4">Pep Messoud</option><option value="5">Nabeela Din</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);[b]function delete_record(id){  xmlHttp=GetXmlHttpObject();  if (xmlHttp==null)  {	alert ("Your browser does not support AJAX!");	return;  }  var url="delete.php?id=" + id;  xmlHttp.onreadystatechange=function ()  {	// check for response, delete row from html table, etc  };  xmlHttp.open("GET",url,true);  xmlHttp.send(null);}[/b]$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo "<table border='1'><tr><th>ID</th><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo '<td><a href="java script:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a></td>';echo "<td>" . $row['Name'] . "</td>";echo "<td>" . $row['######'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Address'] . "</td>";echo "<td>" . $row['Post_Code'] . "</td>";echo "<td>" . $row['Contact_Number'] . "</td>";echo "<td>" . $row['Member_Type'] . "</td>";echo "</tr>";}echo "</table>";?></div></center></p><center><FORM METHOD="LINK" ACTION="http://samp.inf.brad.ac.uk:59237/add.php"><INPUT TYPE="submit" VALUE="Add Member"></FORM></center></body></html>

As always thanks for your help!

Edited by diego
Link to post
Share on other sites

First, make sure there's not a space between "java" and "script". This forum likes to add a space there because it apparently thinks it's protecting something by doing that, but it's not correct to have a space there.echo '<td><a href="javascript:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a></td>';If you want to still show the ID, add a blank cell to the table header for the delete link to go in.It looks like you added the Javascript function to the PHP code. That's not going to work, Javascript isn't valid PHP. Javascript code either needs to go inside a script tag in the HTML or inside an external file that you include with a script tag. Check the examples here:http://w3schools.com/js/js_howto.asp

Link to post
Share on other sites

Thanks for the reply.I've added it in as a script, just like i did when I used the tablecloth.js file which works perfectly and I'm still getting the same problem. I can click on the delete button but nothing happens?

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script><link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" /><script type="text/javascript" src="tablecloth/tablecloth.js"></script><script type="text/javascript" src="delete_record.js"></script></head><body background="backgrounda.PNG"><h2 align="center"><b><u><font size="8" face="COMIC SANS MS" font color="#38ACEC">Malik's Gym</font></b></u></h2><form><font color="#38ACEC"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Tahir Malik</option><option value="2">Khalid Mahmood</option><option value="3">Amanda Murphy</option><option value="4">Pep Messoud</option><option value="5">Nabeela Din</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo "<table border='1'><tr><th>ID</th><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo '<td><a href="java script:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a></td>';echo "<td>" . $row['Name'] . "</td>";echo "<td>" . $row['######'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Address'] . "</td>";echo "<td>" . $row['Post_Code'] . "</td>";echo "<td>" . $row['Contact_Number'] . "</td>";echo "<td>" . $row['Member_Type'] . "</td>";echo "</tr>";}echo "</table>";?></div></center></p><center><FORM METHOD="LINK" ACTION="http://samp.inf.brad.ac.uk:59237/add.php"><INPUT TYPE="submit" VALUE="Add Member"></FORM></center></body></html>

delete_record.js

function delete_record(id){  xmlHttp=GetXmlHttpObject();  if (xmlHttp==null)  {	alert ("Your browser does not support AJAX!");	return;  }  var url="delete.php?id=" + id;  xmlHttp.onreadystatechange=function ()  {	// check for response, delete row from html table, etc  };  xmlHttp.open("GET",url,true);  xmlHttp.send(null);}

Not sure what's wrong because that should work really.

Link to post
Share on other sites

All that function does is send a request to delete.php. You can add some code to delete.php to write some debug info out, or add some alerts to the Javascript function to pop up some boxes telling you what's going on. An alert would probably be a good idea just to make sure the function's running.

Link to post
Share on other sites

I've created the delete.php file below but still have the same problem of nothing happening when I press the delete button? I checked the database and all the entries are still there?

<html><title>Malik's Gym</title><body background="backgrounda.PNG"><?php$con = mysql_connect("localhost","tmalik2","England");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("tmalik2", $con);$sql="DELETE FROM Malik (ID)VALUES('$_POST[id]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "";mysql_close($con)?><center><FORM METHOD="LINK" ACTION="http://samp.inf.brad.ac.uk:59237/MG.php"><INPUT TYPE="submit" VALUE="Return to database"></FORM></center></body></html><font color="#38ACEC"><b>New Member has been added to the database</b></font>

Thanks.

Edited by diego
Link to post
Share on other sites

That request is going to go through $_GET, not $_POST. SQL delete syntax looks like this:DELETE FROM table WHERE field=valueIf the value's a string it needs to go in quotes, otherwise if it's a number it doesn't need the quotes.Also, that page is just being requested through AJAX, it doesn't need all of the HTML markup, a user will never see it.

Link to post
Share on other sites

Thanks for your help. I've changed everything around and now using a more robust AJAX system. The theory behind everything you've posted me helped me so much! I've not got an add/remove/edit feature installed. I wanna move on to sorting the data by clicking the header and most improtantly carrying out the basic excel formulas on it so addition/substraction/etc.How would that be possible?Thanks.

Link to post
Share on other sites

Sorting is going to be a lot of work to do yourself. The sort function will need to know or be able to determine which column you're clicking on, and it has to run through each row in the table and compare the value in that row and column with the previous or next row. There are several sorting algorithms around, some of them will only require one pass through the table and others will require multiple passes. The bubble sort and quicksort algorithms are popular.http://www.google.com/search?client=opera&...-8&oe=utf-8http://www.google.com/search?client=opera&...-8&oe=utf-8Math operations involve some of the same things, the calculation functions will need to loop through each row in your table, find the data to calculate, convert it to a number, and do whatever calculation you want.

Link to post
Share on other sites

I'm working on the sort functions, gonna be difficult but I think I can get that done. Thanks.I'm having real problems getting my head around the formulas though. Is there any other spreadsheet like feature that I can add instead of formulas. Hoping for something more advanced then what I currently have?Thanks.

Link to post
Share on other sites

It's sort of up to you to decide on which features you want. I had suggested using something like Ext for that because it will automatically handle things like sorting, reordering rows, summing or averaging. It will also filter the data, but you need to tell it how to do that, it's not automatic.

Edited by justsomeguy
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...