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,028

HOME > PHP > PHP Forum > php จะเช็ค username โดยใช้ jquery และ ajax แต่ทำไม่ได้ครับ ไม่รู้ว่าอะไรมันผิด



 

php จะเช็ค username โดยใช้ jquery และ ajax แต่ทำไม่ได้ครับ ไม่รู้ว่าอะไรมันผิด

 



Topic : 092160



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



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




โค้ดทั้งหมดนี้อยู่ในหน้าเดียวกันทีืชื่อ "Register_PP.php" ครับ
โค้ดตามนี้ อันนี้เป็น jquery ไว้เช็ค username ว่า available or not
คือมันได้คำตอบเป็น undefine is not available ตลอดเลยครับ พอลองตั้ง else เป็น else if result == 0 กับ 1 สองอัน มันก็ขึ้น checking ตลอดเลย

<body>

<script type="text/javascript">
    $(document).ready(function() {  
      
            //the min chars for username  
            var min_chars = 3;  
      
            //result texts  
            var characters_error = 'Minimum amount of chars is 3';  
            var checking_html = 'Checking...';  
      
            //when typing in txtUser [Username textbox]
            $('#txtUser').keyup(function(){  
                //run the character number check  
                if($('#txtUser').val().length < min_chars){  
                    //if it's bellow the minimum show characters_error text '  
                    $('#user_result').html(characters_error);  
                }else{  
                    //else show the cheking_text and run the function to check  
                    $('#user_result').html(checking_html);  
                    check_availability();  
                }  
            });  
      
      });  
      
    //function to check username availability  
    function check_availability(){  
      
            //get the username  
            var username = $('#username').val();  
      
            //use ajax to run the check  
            $.post("Register_PP.php", { username: username },  
                function(result){  
                    //if the result is 1  
                    if(result == 1){  
                        //show that the username is available  
                        $('#user_result').html(username + ' is Available');  
                    }else{  
                        //show that the username is NOT available  
                        $('#user_result').html(username + ' is not Available');  
                    }  
            });  
      
    }  

</script>


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
แล้วก็ โค้ด php กับ textbox

Code (PHP)
<td align="right">Username:</td>
            <td align="left"><input type="text" id="txtUser" name="txtUser" title="Symbol is allowed" autocomplete="off" 
            value="<?php echo $user?>" maxlength="20"/> <div id='user_result'></div>  </td>
            
			<?php
			$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
			$objDB = mysql_select_db("accommodation");
			  
			//get the username  
			$username = mysql_real_escape_string($_POST['txtUser']);  
			//mysql query to select field username if it's equal to the username that we check '  
			$result = mysql_fetch_array(mysql_query('SELECT * FROM member WHERE m_user = "'. $username .'"'));  
			  
			//if username found 
			if($username == $result['m_user']){  
				//and we send 0 to the ajax request  
				echo 0;  
			}else{  
				//else if username not found'  
				//and we send 1 to the ajax request  
				echo 1;  
			}  
			?>




Tag : PHP, MySQL, JavaScript, Ajax, jQuery









ประวัติการแก้ไข
2013-03-10 17:45:50
2013-03-10 17:46:51
2013-03-10 18:11:58
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-03-10 17:44:41 By : copsychus View : 1172 Reply : 2
 

 

No. 1



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



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


คุณเจ้าของกระทู้ครับ

1. var username = $('#username').val(); // ที่มัน undefine เพราะคุณไม่ได้กำหนดมัน เพราะใน id textbox คุณเป็น txtUser มันจึงไม่รู้จัก username

2. ผมแนะนำให้แยก code PHP

<?php
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("accommodation");

//get the username
$username = mysql_real_escape_string($_POST['txtUser']);
//mysql query to select field username if it's equal to the username that we check '
$result = mysql_fetch_array(mysql_query('SELECT * FROM member WHERE m_user = "'. $username .'"'));

//if username found
if($username == $result['m_user']){
//and we send 0 to the ajax request
echo 0;
}else{
//else if username not found'
//and we send 1 to the ajax request
echo 1;
}
?>
เพราะที่ผมลองเอา code คุณไป test ดูมันมีปัญหาหากไว้ไฟล์เดียวกัน หวังว่าคำแนะนำนี้คงเป็นประโยชน์นะครับ
ไปอีกหน้านึงเพราะ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-11 11:01:01 By : atthk208
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

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

error นี้ไล่ยากนิดหนึ่งครับ ลองใใช้การไล่ debug ด้วยการ alert() ทีล่ะ step ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-11 11:01:26 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : php จะเช็ค username โดยใช้ jquery และ ajax แต่ทำไม่ได้ครับ ไม่รู้ว่าอะไรมันผิด
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 05
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 อัตราราคา คลิกที่นี่