Jump to content

Help! Something is wrong...


faces3

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by faces3
Link to comment
Share on other sites

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 */;
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.