faces3 Posted May 2, 2013 Share Posted May 2, 2013 I try get work that script, but something is wrong... Cant save new values and cant delete values. <html> <head> <title>Silmaring</title> <script src="js/jquery.js"></script> <script src="js/script.js"></script> <script src="js/jquery-ui-1.8.17.custom.min.js"></script> <link rel="stylesheet" href="css/style.css"> </head> <body> <br> <div style="margin-left: 20%;margin-top: 5%;"> <input type="button" value="Lisa" id="add_new"><p> <table width="70%" border="0" cellpadding="0" cellspacing="0" class="table-list"> <tr> <th width="20%">Kooli nimi</th> <th width="40%">Kustuta</th> </tr> </table> </div> <div class="entry-form"> <form name="userinfo" id="userinfo"> <table width="100%" border="0" cellpadding="4" cellspacing="0"> <tr> <td colspan="2" align="right"><a href="#" id="close">Sulge</a></td> </tr> <tr> <td>Kooli nimi</td> <td><input type="text" name="kool"></td> </tr> <tr> <td align="right"></td> <td><input type="button" value="Salvesta" id="save"><input type="button" value="Tühista" id="cancel"></td> </tr> </table> </form> </div> </body> </html> And here is ajax.php file. <? ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); $conn = mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("silmaring",$conn) or die(mysql_error()); if(isset($_POST) && count($_POST)){ $kool = $_POST['kool']; $item_id = $_POST['$item_id']; if($action == "save"){ $res = mysql_query("insert into kool values('','".$kool."')"); if($res){ echo json_encode( array( "success" => "1", "row_id" => time(), "kool" => htmlentities($kool), ) ); } } else if($action == "delete"){ $res = mysql_query("delete from info where id = $"); if($res){ echo json_encode( array( "success" => "1", "item_id" => $item_id ) ); } } }else{ echo "No data set"; } ?> I turn error messages on, but dont see any errors. Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2013 Share Posted May 2, 2013 1) $action doesn't appear to be defined anywhere. 2) Your DELETE query is foobar 3) You have eror reporting disabled. 4) Your code isn't indented making it hard to debug. Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 2, 2013 Author Share Posted May 2, 2013 I add that is it right? I dont see errors... error_reporting(E_ALL); ini_set('display_errors', '1'); And i add that line: $action = $_POST['action']; But seems it dont work. What i must put there ['action'] Or is it right? Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 2, 2013 Author Share Posted May 2, 2013 (edited) Looks ['action'] is right. File: script.js $(document).ready(function(){ $("#save").click(function(){ ajax("save"); }); $("#add_new").click(function(){ $(".entry-form").fadeIn("fast"); }); $("#close").click(function(){ $(".entry-form").fadeOut("fast"); }); $("#cancel").click(function(){ $(".entry-form").fadeOut("fast"); }); $(".del").live("click",function(){ ajax("delete",$(this).attr("id")); }); function ajax(action,id){ if(action =="save") data = $("#userinfo").serialize()+"&action="+action; else if(action == "delete"){ data = "action="+action+"&item_id="+id; } $.ajax({ type: "POST", url: "ajax.php", data : data, dataType: "json", success: function(response){ if(response.success == "1"){ if(action == "save"){ $(".entry-form").fadeOut("fast",function(){ $(".table-list").append("<tr><td>"+response.kool+"</td><td><a href='#' id='"+response.row_id+"' class='del'>Delete</a></a></td></tr>"); $(".table-list tr:last").effect("highlight", { color: '#4BADF5' }, 1000); }); }else if(action == "delete"){ var row_id = response.item_id; $("a[id='"+row_id+"']").closest("tr").effect("highlight", { color: '#4BADF5' }, 1000); $("a[id='"+row_id+"']").closest("tr").fadeOut(); } }else{ alert(response.msg); } }, error: function(res){ alert("Unexpected error! Try again."); } }); } }); Website give me same error what is here... "Unexpected error! Try again." Edited May 2, 2013 by faces3 Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 2, 2013 Author Share Posted May 2, 2013 Here is my database. -- phpMyAdmin SQL Dump -- version 3.5.4 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: May 01, 2013 at 10:11 PM -- Server version: 5.1.67 -- PHP Version: 5.3.3 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: `silmaring` -- -- -------------------------------------------------------- -- -- Table structure for table `kool` -- CREATE TABLE IF NOT EXISTS `kool` ( `nr` time NOT NULL, `kool` varchar(50) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!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 */; Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2013 Share Posted May 2, 2013 Post your current code and a decent description of what is happening and exactly what you have doen to debug the issue. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 2, 2013 Share Posted May 2, 2013 unless your ajax code displays everything that is sent back from the php code or you are directly examining the response sent back to the client, you won't see the php produced errors and there may not even be any php errors if the problem is in your php logic. Ajax adds a extra layer to your code that makes debugging harder. before you can use Ajax to accomplish a task, you must be able to perform that task, error free, without using Ajax and be able to debug what your code is doing. get a normal html form working with your php code, then add Ajax. Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 2, 2013 Author Share Posted May 2, 2013 That script i get that site. http://www.amitpatil.me/ajax-table-adding-removing-rows-dynamically-using-javascript-animation/ Seems there is more people who cant get work it. Quote Link to comment Share on other sites More sharing options...
jcbones Posted May 2, 2013 Share Posted May 2, 2013 In your script.js file, replace error: function(res){ alert("Unexpected error! Try again."); } with: error: function(jqXHR, textStatus, errorThrown){ alert("ERROR: "+errorThrown); } Then you should see something that points you in the right direction. Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) Error invalid JSON: and give ajax.php file Edited May 3, 2013 by faces3 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2013 Share Posted May 3, 2013 that's because you are using short opening php tag <? and your .php code is not running. use a full opening php tag <?php Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 3, 2013 Author Share Posted May 3, 2013 Undefined index: $item_id ajax.php on line 8 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2013 Share Posted May 3, 2013 $item_id = $_POST['$item_id']; should be $item_id = $_POST['item_id']; are you even trying to debug these errors? Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 3, 2013 Author Share Posted May 3, 2013 Yes I try it but give same error. http://workhour.tk/silmaring/regkool/ Quote Link to comment Share on other sites More sharing options...
ignace Posted May 3, 2013 Share Posted May 3, 2013 Ajax adds a extra layer to your code that makes debugging harder. Most browsers give you the option to replay Ajax requests. There are even browser extensions that allow you to create ajax calls with for example POST and JSON as body. @faces3 if you have Chrome installed (if not, install it) go to your page where you would call the ajax. Options (the three horizontal lines) > Extra > Developer Tools. This opens a new pane at the bottom. Click Network and click on XHR at the bottom. Now fire your ajax and you'll see that a new line appears. Click it and select Response, fix your code, come back and right-click on the line and select Replay XHR, another line appears, click it and select Response. Keep repeating this process until you have the output you expect. Quote Link to comment Share on other sites More sharing options...
faces3 Posted May 4, 2013 Author Share Posted May 4, 2013 Thank you. I try it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.