  | 
              
	              
	                
  
    
	 
        กดปุ่ม alert ของจาวาสคิปแล้วโค้ด HTML ไม่รันต่อครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                เว็บเว็บทำรายงานครับ 
หน้านี้ไม่มีปัญหาอะไร 
  
ใส่ Do ซ้ำจะขึ้น Alert แบบรูปข้างล่าง 
Code (PHP) 
<?php 
                            
                            $sqlcheck = "SELECT DO_name FROM do 
                            WHERE DO_Name='".$Show1."'; " or die("Error:" . mysqli_error());
                            $result = mysqli_query($conn, $sqlcheck); 
                            if ($result->num_rows > 0) {
                                 echo '<script type="text/javascript">alert("เลข Do นี้ซ้ำ");';
                                
                                 echo "</script>";
                                
                            }
 
เข้าเงื่อนไขนี้ 
  
พอกด Ok หน้าเว็บจะโหลดไม่ครบ 
  
 
 
อันนี้โค้ดแบบเต็มๆครับ 
Code (PHP) 
<?php
           $Show1 = $_POST["DO_name"];
            $Show2 = $_POST["Date"];   
            $Show3 = $_POST["customer_id"];       
            $Show4 = $_POST["Product_id"]; 
            $Show5 = $_POST["Container"];
            $Show6 = $_POST["PlateofTruck"];
            $user_id = $_REQUEST["user_id"];
?>
<form id="contact" name="frmMain" method="post" action="insert1.php">
        <table width="40%" border="1" >
            <tr>
                <th width="48%" height="27">กรอกหมายเลข DO.</th>
                <td>
                    <?php 
                            
                            $sqlcheck = "SELECT DO_name FROM do 
                            WHERE DO_Name='".$Show1."'; " or die("Error:" . mysqli_error());
                            $result = mysqli_query($conn, $sqlcheck); 
                            if ($result->num_rows > 0) {
                                 echo '<script type="text/javascript">alert("เลข Do นี้ซ้ำ");';
                                
                                 echo "</script>";
                                
                            }
                            else{
                                $do=$_POST["DO_name"];
                        //     }
                        // }
                         
                    ?>
                    <input name="DO_name" type="text" id="DO_name"  maxlength ="10" value="<?php echo $do; ?>"/>
                    <!--<label><?php echo $text;?></label>-->
                </td>
            </tr>
          
            <tr>
            <th>วัน/เดือน/ปี ที่ขนส่ง</th>
            <td><input name="Date" type="date" id="Date" require style="width:75%;" placeholder="YYYY-MM-DD" ></td>
          </tr>
          <tr>
            <th>ลูกค้า</th>
            <td>
                <input list="customer_id" name="customer_id" value="<?php echo $customer; ?>">
                        <datalist id="customer_id" >
                            <?php
                            include("connect2.php");
                            $result = $conn->query("select customer_id, customer_name from customer");
                            while ($row = $result->fetch_assoc()) {
                                unset($id, $name);
                                $id = $row['customer_id'];
                                $name = $row['customer_name']; 
                                echo '<option value="'.$id.'" value="'.$id.'">'.$name.'</option>';
                            }
                            ?>
                        </datalist>
            </td>
          </tr>
            
          <tr>
            <th>สินค้า</th>
            <td>
                <input list="Product_id" name="Product_id" value="<?php echo $product; ?>">
                    <datalist id="Product_id" >
                        <?php
                        include("connect2.php");
                        $result = $conn->query("select Product_id, Product_name from product");
                        while ($row = $result->fetch_assoc()) {
                            unset($id, $name);
                            $id = $row['Product_id'];
                            $name = $row['Product_name']; 
                            echo '<option value="'.$id.'" type="hidden">'.$name.'</option>';
                        }
                        ?>
                    </datalist>
              </td>
          </tr>
        </table>
        <br>
              <table width="40%" border="1" >
              <tr>
                <th width="48%" height="27">ทะเบียนรถ</th>
                <td ><input  name="PlateofTruck" type="text" id="PlateofTruck" style="width:75%;"></td>
              </tr>
              <tr>
                <th width="48%" height="27">หมายเลขตู้คอนเทนเนอร์</th>
                <td ><input  name="Container" type="text" id="Container" style="width:75%;"></td>
              </tr>
              <tr>
              <td></td>
                <td> <input type = "hidden" name = "user_id" value="<?php echo $user_id; ?>">
                <input type="submit"  name="Submit" value="บันทึก"  id="add" ></td>
              </tr>
            </table>
            <?php 
            
                if($Show1&&$Show2 !=null){
                    
                    mysqli_set_charset($conn, "utf8");
                    ob_start();  ////จุดเริ่มต้น
                              
                    $sql = "INSERT INTO do (DO_name,Date,customer_id,Product_id,Container,PlateofTruck,user) 
                    VALUES ('".$Show1."', '".$Show2."', 
                    '".$Show3."','".$Show4."',
                    '".$Show5."','".$Show6."','".$user_id."')";
                    
                    if ($conn->query($sql) === TRUE){
                        echo '<script type="text/javascript">alert ("บันทึกสำเร็จ");';
                        echo "</script>";
                    }
                    else{
                        echo "บันทึกไม่สำเร็จ";
                    }
                }
                
            }
        
            ?>
    </form>
 
ผมติดปัญหาข้อเดียวคือ Alert พอกด OK โค้นมันไม่รันช่วยผมหน่อยนะครับ ขอบคุณครับ
 
 
  Tag : PHP, MySQL, CSS, HTML5, JavaScript               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2020-05-02 16:12:00 | 
                      By :
                          kittinun | 
                      View :
                          1224 | 
                      Reply :
                          7 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 โหลดก่อนค่อยแจ้ง 
 
Code (JavaScript) 
window.onload = function() {
    alert("เลข Do นี้ซ้ำ");
}
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2020-05-02 16:52:49 | 
                        By :
                            PhrayaDev | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ผมลองแล้วยังทำไม่ได้เลยครับ T_T 
 
Code (JavaScript) 
<script>
            function load() {
                alert("เลข Do นี้ซ้ำ");
            }
        </script>
 
 
Code (PHP) 
           echo "<script type='text/javascript'> window.onload=load; </script>";
  
 
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2020-05-02 17:30:06              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2020-05-02 17:29:06 | 
                        By :
                            kittitnun | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองแบบนี้ครับ 
 
Code (PHP) 
echo '<script type="text/javascript">window.onload = function(){ alert("เลข Do นี้ซ้ำ");}</script>'; 
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2020-05-02 17:55:55 | 
                        By :
                            PhrayaDev | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองแล้วยังไม่ได้เหมือนเดิมเลยครับ T_T  
  
 
 
Code (PHP) 
echo '<script type="text/javascript">window.onload = function(){ alert("เลข Do นี้ซ้ำ");}</script>'; 
 
 
 
ขอบคุณมากนะครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2020-05-02 18:27:35 | 
                        By :
                            kittitnun | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 จัด flow control ใหม่  
แยก php กับ html/javascript ออกจากกัน 
จัดการ php ก่อน  
 
Code (PHP) 
<?php
 $msg=''; // กำหนดตัวแสดง Error
// เปิด db connection ตรวจสอบ DB
$db=new mysqli(.......); 
if ($db->errno){
   die('db error');
}
if( isset($_POST['submit']){
   // จะทำอะไรกับสิ่งที่ request มาก็ทำมันตรงนี้
   // จะ ตรวจสอบ จะ update insert delete อะไรก็ว่าไปตรงนี้
   if($update!='complete'){
     $msg='error';
   }else $msg = 'OK';
}
// เร่ิมต้นคิวรี่ อ่านค่าสิ่งที่ต้องแสดง
$result = $db->query();
//.....
?><!doctype html>
<html lang='th'>
<header>
<meta ...... >
<link .....>
<script src=.....></script>
<script>
function fnc_onload(){
   ........
}
</script>
</header>
<body onload='fnc_onload'>
<?=$msg?> <!--  มีเออเร่อร์ จะแสดงมันตรงนี้ก่อนก็ได้ จะทำเป็น pop up จาก onload ก็ว่าไป  -->
<!--  เร่ิมแสดง html ..... -->
</body>
</html>
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2020-05-03 06:49:39 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |