Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery + ajax. what is wrong in my javascript function? delete record, add record without refreshing
    primarykey
    data
    text
    <p>What is wrong with my JavaScript? It is breaking somewhere to add a record without refreshing the page.</p> <p>i finished deleting completely now with the aid of some experienced users. but now adding a record to the database is not working yet</p> <p>i want to add a record dynamically now without refreshing.. deleting is functioning well and it is being reflected on the database. </p> <p>I will include 4 files and my database:</p> <ul> <li>the files are <code>index.php</code> for my main</li> <li><code>add.php</code> for adding a record</li> <li><code>delete.php</code> for deleting a record</li> <li>dbconfig for the database connection and configuration</li> </ul> <h2><code>index.php</code>:</h2> <pre><code> &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;title&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php include("dbconfig.php"); $sql = "SELECT * FROM games"; $result = mysql_query($sql); while ($record = mysql_fetch_array($result)){ echo "&lt;p class=\"p" .$record['ID']. "\"&gt;&lt;/br&gt; Game ID: " .$record['ID']. "&lt;/br&gt; Game Name: " .$record['Name']. "&lt;br /&gt; Game Type: ".$record['Type']. "&lt;br /&gt; Rating: ".$record['Rating']."&lt;br /&gt; Year Released: ".$record['Release Year']."&lt;br /&gt; &lt;br /&gt;" ?&gt; &lt;a href="#" id="&lt;?php echo $record["ID"]; ?&gt;" class="deletebutton"&gt;&lt;img src="trash.png" alt="delete"/&gt; &lt;/a&gt;&lt;/p&gt; &lt;?php } ?&gt; &lt;form name="add" id ="add" action="" method="post"&gt; &lt;input class ="gameID" type="hidden" id="ID" name="ID" value = " ' .$record['ID'] . ' " /&gt; &lt;b&gt;Game Name: &lt;/b&gt; &lt;input type="text" id="name" name="name" size=70&gt; &lt;b&gt;Game Type:&lt;/b&gt; &lt;input type="text" id="type" name="type" size=40&gt; &lt;b&gt;Rating: &lt;/b&gt; &lt;input type="number" id="score" name="score" min="1.0" max="10.0" step ="0.1"/&gt; &lt;b&gt;Year Released: &lt;/b&gt; &lt;input type="number" min="1900" max="2011" id="Yreleased" name="Yreleased" value="1985" size=4&gt; &lt;p&gt;&lt;input type="submit" name="Submit" id = "Submit" value="Add Game" class = "add games"&gt;&lt;/p&gt; &lt;/form&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;script type = "text/javascript"&gt; $(document).ready(function(){ $("#add").submit(function(){ var name = this['name'].value; var type = this['type'].value; var rating = this['score'].value; var release = this['Yreleased'].value; var dataString = 'name='+ name + '&amp;type=' + type + '&amp;rating=' + rating + '&amp;release=' + release; if (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(){ window.location.reload(true); $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } }); return false; } )}); &lt;/script&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;script type = "text/javascript"&gt; $(document).ready(function(){ $("a.deletebutton").click(function(){ var del_id = $(this).attr("id"); var info = 'id=' + del_id; var parent = $(this).parent(); if(confirm("Sure you want to delete this game? !..There is no Undo")){ $.ajax({ type: "get", url: "delete.php?" + info, context: document.body, success: function(){ $('.p'+del_id).html('deleted'); $('.success').fadeIn(200).show(); } }); } return false; }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2><code>add.php</code>:</h2> <pre><code>&lt;?php require('dbconfig.php'); //we cannot continue without this file, thats why using require instead of include if(isset($_POST['name'])) { $name=addslashes($_POST['name']); $type=addslashes(($_POST['type'])); $rating=addslashes($_POST['rating']); $release=addslashes($_POST['release']); $sql = 'INSERT INTO `games` (`Name`,`Type`,`Rating`,`Release Year`) VALUES ("'.$name.'", "'.$type.'", "'.$rating.'", "'.$release.'")'; mysql_query( $sql); if(!mysql_errno()) echo " your game has been added to the list of games. "; } ?&gt; </code></pre> <h2><code>delete.php</code>:</h2> <pre><code> &lt;?php require('dbconfig.php'); //we cannot continue without this file, thats why using require instead of include if(isset($_GET['id'])) { $id=(int)$_GET['id']; $sql = 'DELETE FROM `games` WHERE `ID`="'.$id.'" LIMIT 1'; mysql_query( $sql); echo 'deleted'; } ?&gt; </code></pre> <h2>dbconfig:</h2> <pre><code> &lt;?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); ?&gt; </code></pre> <p>If needed, here's the schema for my database as well:</p> <pre><code> SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `gamebook` -- CREATE DATABASE `gamebook` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE `gamebook`; -- -------------------------------------------------------- -- -- Table structure for table `games` -- CREATE TABLE IF NOT EXISTS `games` ( `ID` int(4) NOT NULL AUTO_INCREMENT, `Name` varchar(70) COLLATE utf8_unicode_ci NOT NULL, `Type` varchar(30) COLLATE utf8_unicode_ci NOT NULL, `Rating` float NOT NULL, `Release Year` int(4) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ; -- -- Dumping data for table `games` -- INSERT INTO `games` (`ID`, `Name`, `Type`, `Rating`, `Release Year`) VALUES (1, 'Battlefield 3', 'First Person Shooter', 8.5, 2011), (2, 'Fifa 12', 'Sports', 9.2, 2004), (3, 'Red Alert 3', 'Strategy ', 8.8, 2007), (4, 'Fight Night Round 4', 'Fighting', 9, 2010), (5, '', '', 0, 0), (6, '', '', 0, 0); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; </code></pre> <p>What is wrong in my javascript and what is the best way to figure out??</p> <p>Is there a good text editor you guys recommend for beginners like myself?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload