i am trying to add and delete records(games) from my database using jquery and ajax.
i am still a junior software engineer and learning the basics of web development but i have this task to accomplish and unsure what i am missing for it to work.
i have 5 columns from my database and the ID is hidden with auto increment.
my question is what is broken in the code that is not letting me add or delete a game and reflect it on the database???
this is my main file with the calls to delete and add games.
<script type = "text/javaScript" src ="http://code.jquery.com/jquery- latest.js""></script>
<script type = "text/javascript">
$(document).ready(function(){
$("add games").Submit(function()
$("#add").load("add.php"); {
var id = $("#ID").val();
var name = $("#name").val();
var type = $("#type").val();
var rating = $("#score").val();
var release = $("Yreleased").val();
var dataString = 'ID=' + id 'name='+ name + '&username=' + type + '&password=' + rating + '&gender=' + release;
if (id =='' || name == '' || type == '' || rating == '' || release == '')
{
alert("please enter some valid data for your game entry");
}
else
$.ajax({
type: "POST",
url: "add.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
<body>
<?php
include("dbconfig.php");
$sql = "SELECT * FROM games";
$result = mysql_query($sql);
while ($record = mysql_fetch_array($result)){
echo "</br> Game ID: " .$record['ID']. "</br> Game Name: " .$record['Name'].
"<br /> Game Type: ".$record['Type']. "<br /> Rating: ".$record['Rating']."<br /> Year Released: ".$record['Release Year']."<br /> <br />" ?>
<a href="#" id="<?php echo $record["ID"]; ?>" class="deletebutton"><img src="trash.png" alt="delete"/></a>
<?php
}
?>
<form name="add" id ="add" action="add.php" method="post">
<input class ="gameID" type="hidden" id="ID" value = " ' .$record['ID'] . ' " />
<b>Game Name: </b> <input type="text" id="name" size=70>
<b>Game Type:</b> <input type="text" id="type" size=40>
<b>Rating: </b> <input type="number" id="score" min="1.0" max="10.0" step ="0.1"/>
<b>Year Released: </b> <input type="number" min="1900" max="2011" id="Yreleased" value="1985" size=4>
<p><input type="submit" name="Submit" id = "Submit" value="Add Game" class = "add games"></p>
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"
$(document).ready(function() {
$("a.deletebutton").click(function(){
var del_id = element.attr("id");
var info = 'id=' + del_id;
var parent = $(this).parent();
if(confirm("Sure you want to delete this game? !"))
{
$.ajax({
type: "get",
url: "delete.php",
data: parent.attr('id').replace('record-',"),
success: function(){
}
});
$(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
</script>
</body>
and i have very simple php files for add, for delete and for the dbconfig
for delete.php
<?php
// This is a sample code in case you wish to check the username from a mysql db table
include("dbconfig.php");
if(($_GET['id'])
{
$id=$_GET['id'];
$sql = "DELETE from games where ID='.(int)$_GET['id'];
$result= mysql_query( $sql);
}
?>
and this is my add.php and my dbconfig.php in the end
<?php
include("dbconfig.php");
if(isset($_POST['Submit'])
$name=$_POST['name'];
$type=$_POST['type'];
$rating=$_POST['score'];
$release=$_POST['Yreleased'];
$sql = ("INSERT INTO games VALUES ($name, $type, $rating, $release)");
echo " your game has been added to the list of games. ";
?>
and last my dbconfig
<?php
$user_name = "root";
$password = "";
$database = "gamebook";
$server = "localhost";
$bd = mysql_connect($server, $user_name, $password) or die ('I cannot connect to the database because:' . mysql_error());
mysql_select_db($database);
?>
this is the first time every i post a question and ask, so if i did something unacceptable or that should have been done otherwise, i would appreciate the tips, help, links, or if someone can help me on how to fix my issue.
i can read from the database, but not add or delete from it dynamically.
delete.php
add.php
index.php take from here: http://pastebin.com/NVbheAJZ