简体   繁体   中英

ajax alert data not return the value of php echo

how can i alert in javascript the value echo from php

NOTE I TRIED THE ALERT(DATA) BUT IT din't give any validation when success because of the lopping thing in php

my concern is when i try making validation it just go straight to else thing even thought which is Import Successfully even if the data or the echo in php is "Error1" , "Error2" or "Error3" please help..

here is my ajax

<script>  
      $(document).ready(function(){  
           $('#upload_csv').on("submit", function(e){  
                e.preventDefault(); //form will not submitted  
                $.ajax({  
                     url:"import.php",  
                     method:"POST",  
                     data:new FormData(this),  
                     contentType:false,          // The content type used when sending data to the server.  
                     cache:false,                // To unable request pages to be cached  
                     processData:false,          // To send DOMDocument or non processed data file it is set to false  
                     success: function(data){  
                        if(data == 'Error1')
                        {
                            alert("Error1");
                        }
                        else if(data == 'Error2')
                        {
                            alert("Error2");
                        }
                        else if(data == 'Error3')
                        {
                            alert('Error3');
                        }
                        else
                        {
                            alert('Import Successfull');
                            $('#employee_table').html(data);
                        }                       
                     }
                })  
           });  
      });  
 </script>  

and here is the php code

<?php  
 if(!empty($_FILES["employee_file"]["name"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "db");  
      $output = '';  
      $allowed_ext = array("csv");  
      $tmp = explode(".", $_FILES["employee_file"]["name"]);
      $extension = end($tmp); 
      if(in_array($extension, $allowed_ext))  
      {  
           $file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');  
           fgetcsv($file_data);
              while($row = fgetcsv($file_data))  
           {
               $try = count($row);
               if($try === 6){             
                $id = mysqli_real_escape_string($connect, $row[0]);
                $name = mysqli_real_escape_string($connect, $row[1]);  
                $address = mysqli_real_escape_string($connect, $row[2]);  
                $gender = mysqli_real_escape_string($connect, $row[3]);  
                $designation = mysqli_real_escape_string($connect, $row[4]);  
                $age = mysqli_real_escape_string($connect, $row[5]);                
                $query = "  
                INSERT INTO tbl_employee  
                     (id,name, address, gender, designation, age)  
                     VALUES ('$id','$name', '$address', '$gender', '$designation', '$age')  
                ";              
                mysqli_query($connect, $query);
               }else
               {

                   echo 'Error3';
                   break;
               }                   
            } 

           echo $output;  
      }  
      else  
      {  
           echo "Error1";  
      }  
 }  
 else  
 {  
      echo "Error2";  
 }  
 ?>  

alter() cannot output complex data. try to use console.log() .

data will appear in the browser console instead of popping up directly. You can press F12 and then click on the Console to view

try the return value using json, on js

<script>  
      $(document).ready(function(){  
           $('#upload_csv').on("submit", function(e){  
                e.preventDefault(); //form will not submitted  
                $.ajax({  
                     url:"import.php",  
                     method:"POST",
                     dataType: 'JSON',  
                     data:new FormData(this),  
                     contentType:false,          // The content type used when sending data to the server.  
                     cache:false,                // To unable request pages to be cached  
                     processData:false,          // To send DOMDocument or non processed data file it is set to false  
                     success: function(data){  
                        // look into detail json data
                        console.log(data);
                        if(data['status'] == false)
                        {
                            alert(data['error']);
                        } else
                        {
                            alert('Import Successfull');
                            $('#employee_table').html(data);
                        }                       
                     }
                })  
           });  
      });  
 </script>  

on php

<?php  

 if(!empty($_FILES["employee_file"]["name"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "db");  
      $output = '';  
      $allowed_ext = array("csv");  
      $tmp = explode(".", $_FILES["employee_file"]["name"]);
      $extension = end($tmp); 
      if(in_array($extension, $allowed_ext))  
      {  
           $file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');  
           fgetcsv($file_data);
           while($row = fgetcsv($file_data))  
           {
               $try = count($row);
               if($try === 6){             
                $id = mysqli_real_escape_string($connect, $row[0]);
                $name = mysqli_real_escape_string($connect, $row[1]);  
                $address = mysqli_real_escape_string($connect, $row[2]);  
                $gender = mysqli_real_escape_string($connect, $row[3]);  
                $designation = mysqli_real_escape_string($connect, $row[4]);  
                $age = mysqli_real_escape_string($connect, $row[5]);                
                $query = "INSERT INTO tbl_employee  
                     (id,name, address, gender, designation, age)  
                     VALUES ('$id','$name', '$address', '$gender', '$designation', '$age')";    
                mysqli_query($connect, $query);
               }else{
                   $status = false;
                   $error = 'Error3';
                   $output = [];
               }
            }
            $status = true;
            $error = [];
            $output = $output;  
      } else {
        $status = false;
        $error = "Error1";
        $output = [];
      }  
 } else {
    $status = false;
    $error = "Error2";
    $output = [];
 }

 header('Content-Type: application/json');
 echo json_encode(array('status'=>$status,'output'=>$output,'error'=>$error), JSON_PRETTY_PRINT);

 ?> 

sorry for my bad english

使用console.log()测试您的数据,警报无法获取很多数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM