Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > งงว่าทำไมแทรก code ตรงนี้แล้ว error เลยทันทีครับ JSON



 

งงว่าทำไมแทรก code ตรงนี้แล้ว error เลยทันทีครับ JSON

 



Topic : 123954



โพสกระทู้ ( 19 )
บทความ ( 0 )



สถานะออฟไลน์




ผมทำตาม tutorial ในyoutube เกี่ยวกับระบบแสดงความเห็น https://www.youtube.com/watch?v=p_GIBLNmtco&index=14&list=PLyKBLKYqadGlmL6QLKCicUp9tQsybUII5
ทีนี้เห็นเขา include หน้าที่เป็น class มาใส่อะครับโดยใช้คำสั่งrequire_once ซึ่งเขาทำสำเร็จ แต่ของผมทำแล้วerrorอะครับ

error ขึ้นว่า Uncaught SyntaxError: Unexpected token < in JSON at position 0
ซึ่ง errorนี้ จะว่าไปแค่ผมเอา console.log("Hello") ไปเขียนแทรกแทนบรรทัดที่เขียนว่า require_once แล้วลบrequire_onceออก ก็ยัง error อยู่ดี เลยคิดว่าสรุปคือ มันแทรกcodeอะไรในบรรทัดนี้ไม่ได้เลยซะงั้น (แทรกได้แค่$x = 10; กำหนดตัวแปรไรประมาณนี้)-*- แต่ในคลิปเขาแทรกrequire_onceแล้วได้

Code (PHP)
<?php
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert')
{
	$userId = (int)$_POST['userId'];
	$comment = addslashes(str_replace( "\n" , "<br>", $_POST['comment'])); 

	$std = new stdClass(); 
	$std->comment_id = 24;
	$std->userId = $userId;
	$std->comment = $comment;
	$std->userName = "Supanat";
	$std->profile_img = "http://s3.amazonaws.com/37assets/svn/765-default-avatar.png";

	require_once '../mysql/models/comments.php'; 
	if(class_exists('Comments'))
	{
		$commentInfo = Comments::insert( $comment, $userId); //$comment จากข้างบนตรง addslash
		if($commentInfo != null)
		{

		}
	}

	echo json_encode($std);

}
else
{
	header('location: /');  //go to main page use slash
}
?>




หน้า class ที่ถูกrequire_once
Code (PHP)
<?php

class Comments
{
	public static function getComments()
	{

	}

	public static function insert($commentText, $userId)  //although insert to db but also return a stdClass Object from the database
	{


		$std = new stdClass(); //stdClass is a empty class that is already in PHP system
		$std->comment_id = null;  //name from variable in db
		$std->comment = $comment_txt;
		$std->userId = (int)$userId;

		return $std;
	}

	public static function update($data)
	{

	}

	public static function delete($commentId)
	{
		//delete the comment from the comment database using the id of comment_id
	}
}

?>



อีกหน้าครับเอามาเผื่ออาจจะเกี่ยวข้อง

Code (JavaScript)
$(document).ready(function(){
	$('#post-btn').click(function(){
		comment_post_btn_click(); //เรียกใช้ function
	});	
});



function comment_post_btn_click()
	{
		var text = $('#posttext').val(); //get value from text
		var userid = $('#user-id').val();
		var user_name = $('#user-name').val();

		if(text.length > 0 && userid != null)
		{
			
			$('.comment-insert-container').css('border', '1px solid #e1e1e1');  //after border red from emptypost so then if notempty post oldcolor will be back

			$.ajax({  
				type: "POST",
				url: "ajax/comment_insert.php",
				data: {
					task : "comment_insert",
					userId : "userid",
					comment : text 
				},
				success : function(data)
				{
					comment_insert( jQuery.parseJSON(data));
					console.log("ResponseText " + data);
				},
				//dataType: 'html' //not neccessary
			});

			

			console.log(text + " Username: " + user_name + " User ID: " + userid );
		}
		else
		{
			//text empty and then let's put a border of red on it
			$('.comment-insert-container').css('border', '1px solid red');
			console.log("The text was empty");
		}

		
		$('#posttext').val(""); 
	}

	function comment_insert(data)
	{
		var t = '';
		t += '<li class="comment-holder" id="_'+data.comment_id+'">';   //copy from foler include/commentbox.php
		t += '<div class="user-img">';
		t += '<img src="'+data.profile_img+'" class="user-img-pic" />';
		t += '</div> ';
		t += '<div class="comment-body">';
		t += '<h3 class="username-field">'+data.userName+'</h3>';
		t += '<div class="comment-text">'+data.comment+'</div>';
		t += '</div>';
		t += '<div class="comment-button-holder">';
		t += '<ul>';
		t += '<li class="delete-btn">X</li>';
		t += '</ul>';
		t += '</div>';
		t += '</li>';
							
		$('.comment-holder-ul').prepend( t );
	}




Tag : PHP, JavaScript, Ajax, jQuery









ประวัติการแก้ไข
2016-07-21 17:11:58
2016-07-21 17:19:24
2016-07-21 17:19:34
2016-07-21 17:23:27
2016-07-21 17:25:33
2016-07-21 17:32:38
2016-07-21 17:34:33
2016-07-21 17:38:01
2016-07-21 17:38:21
2016-07-21 17:39:39
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-07-21 16:25:13 By : BHunter View : 2002 Reply : 3
 

 

No. 1



โพสกระทู้ ( 19 )
บทความ ( 0 )



สถานะออฟไลน์


ลบ








ประวัติการแก้ไข
2016-07-21 17:22:18
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-07-21 16:32:37 By : BHunter
 


 

No. 2



โพสกระทู้ ( 19 )
บทความ ( 0 )



สถานะออฟไลน์


ลบ


ประวัติการแก้ไข
2016-07-21 17:22:05
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-07-21 17:04:53 By : BHunter
 

 

No. 3



โพสกระทู้ ( 19 )
บทความ ( 0 )



สถานะออฟไลน์


ลบ


ประวัติการแก้ไข
2016-07-21 17:13:28
2016-07-21 17:21:36
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-07-21 17:08:00 By : BHunter
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : งงว่าทำไมแทรก code ตรงนี้แล้ว error เลยทันทีครับ JSON
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่