2017-12-27 1672 views
0

我是新來的Wordpress和Ajax和真的不知道如何解決這個問題,所以任何幫助,將不勝感激狀態迴應。無法加載資源:服務器與404未找到

我上一個WordPress主題,在這裏從數據庫中所有的學生都在表中顯示工作:

<?php 

get_header(); 

if(have_posts()): 
    while(have_posts()) : the_post(); ?> 

<article class="post page"> 
<a href="<?php the_permalink(); ?>"><?php the_content(); ?> 

<!DOCTYPE html> 
<html> 
<head> 



     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 

<style> 

} 
    img { 
    vertical-align: middle; 

} 
    table { 
    border-collapse: collapse; 
    width: 100%; 
    border-spacing: 50px 0; 
    text-align: center; 
} 


    .hover:hover{ 
     background-color: antiquewhite; 

    } 
    td{ 
     padding: 10px; 
    } 
    .aligncenter{ 
     text-align: center; 
    } 


    div.scroll { 
    position: static; 
    width: 700px; 
    height: 400px; 
    overflow: scroll; 
    overflow-x: hidden; 
    } 
    a { 
    text-decoration: none; 
    } 


</style> 
</head> 

<body> 
    <div class="scroll"><a href="<?php the_permalink(); ?>"><?php the_content(); ?> </a> 
<?php 
$mysqli = NEW MySQLi('localhost','root','','wp_db'); 

     if(isset($_GET['order'])){ 
      $order = $_GET['order']; 
     }else{ 
      $order = 'name'; 
     } 

     if(isset($_GET['sort'])){ 
      $sort = $_GET['sort']; 
     }else{ 
      $sort ='ASC'; 
     } 

     $result = $mysqli->query("SELECT * FROM wp_absolventen ORDER BY $order $sort"); 

     $image = $row['bild']; 

     if($result->num_rows > 0){ 

      $sort == 'DESC' ? $sort = 'ASC' : $sort ='DESC'; // short IF statements 
       ?> 
       <div class="table-responsive" style=overflow-x:auto;> 
      <table class="table table-bordered"> 
      <?php 
      echo " 
      <th></th> 
      <th class='aligncenter'> 
      <a href='?order=name&&sort=$sort'>Name</a></th> 
      <th class='aligncenter'><a href='?order=geburtsdatum&&sort=$sort'>Geburtsdatum</a></a></th> 
      <th class='aligncenter'><a href='?order=stadt&&sort=$sort'>Wohnort</a></th> 
      "; 
      ?> 
      <?php 
      while($rows = $result->fetch_assoc()) 
      { 
       $name = $rows['name']; 
       $geburtsdatum= $rows['geburtsdatum']; 
       $stadt = $rows['stadt']; 
       $p='<img src="data:image/jpg;base64,' . base64_encode($rows['bild']) . '"/>'; 
       $id = $rows['id']; 

       ?> 

       <tr class='hover'> 


       <td> <?php echo $p; ?></td> 



       <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td> 



       <td><?php echo $geburtsdatum; ?></td> 



       <td><?php echo $stadt; ?></td> 
       </tr> 

       <?php 

      } 

     }else{ 
      echo "No records returned."; 
     } 
     ?> 
     </table> 
     </div> 


</body> 
</html> 
</article> 
    <div id="dataModal" class="modal fade"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title">Employee Details</h4> 
       </div> 
       <div class="modal-body" id="employee_detail"> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
</div> 
    <script> 
$(document).ready(function(){ 
     $('.view_data').click(function(){ 
      var employee_id = $(this).attr("id"); 
      $.ajax({ 
       url:"select.php", 
       method:"post", 
       data:{employee_id:employee_id}, 
       succes:function(data){ 
        $('#employee_detail').html(data); 
        $('#dataModal').modal("show"); 
       } 
      }); 
     }); 
}); 

</script> 










<?php 
    endwhile; 

    else : 
     echo '<p>No contect found</p>'; 
    endif; 

get_footer(); 
?> 

到目前爲止好,但是當你點擊一個學生的名字,它應該打開一個自舉模態,顯示有關學生的信息(從數據庫中提取)。

 <script> 
$(document).ready(function(){ 
     $('.view_data').click(function(){ 
      var employee_id = $(this).attr("id"); 
      $.ajax({ 
       url:"select.php", 
       method:"post", 
       data:{employee_id:employee_id}, 
       succes:function(data){ 
        $('#employee_detail').html(data); 
        $('#dataModal').modal("show"); 
       } 
      }); 
     }); 
}); 

</script> 

這是調用另一個php文件(select.php),打開模式並從數據庫獲取信息的函數。該select.php文件:

<?php 
if(isset($_POST["employee_id"])) 
{ 
     $output = ''; 
     $connect = mysqli_connect("localhost", "root", "", "wp_db"); 
     $query = "SELECT * FROM wp_absolventen WHERE id = '".$_POST["employee_id"]."'"; 
     $result = mysqli_query($connect, $query); 
     $output .= ' 
     <div class="table-responsive"> 
      <table class="table table-bordered">'; 
     while($row = mysqli_fetch_array($result)) 
     { 
      $output .= ' 
       <tr> 
        <td width="30%"><label>Name</label></td> 
        <td width="70%">'.$row["name"].'</td> 
       </tr> 

       '; 
     } 
     $output .= "</table></div>"; 
     echo $output; 
} 
?> 

但是當我嘗試點擊該學生的名字,模態犯規開放的,而不是我得到這個404錯誤:無法加載資源:服務器迴應的狀態http://localhost/wordpress/sample-page/select.php 404(未找到)。這兩個文件都在同一個文件夾中。

代碼工作時,它不是在WordPress的文件夾中。

M.M.

回答

0

我回答類似的問題,從不同的線程...基本上,建立/寫一個簡單的PHP文件,如果您可以通過瀏覽器直接訪問它,那麼你通過AJAX調用它....的成立是在前綴與頁面級「的頁面標題 - 」 PHP文件名,然後創建「頁面標題 - 的 - 」,類似上述公約,然後設置你的永久鏈接的網址一個空白頁。這是另一個thread

+0

我已經將我的select.php更改爲「page-select.php」,保存在我的functions.php所在的位置,創建了一個名爲「select」的新空白頁面,然後在我的ajax中將此文件稱爲/ select。現在的錯誤是POST POST http:// localhost/select 404(Not Found)Btw.:感謝您的快速響應:) – blubezblaze

+0

轉到您的設置>永久鏈接,應選擇帖子名稱。然後檢查您的瀏覽器是否可以檢索localhost/select,如果不是localhost/index.php/select – Anthony

+0

localhost/select:找不到對象,嘗試localhost/index.php/select:打開XAMPP站點(歡迎使用xampp ) – blubezblaze

0

嘗試使用數據屬性,以便下面線

<td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td> 

可以

<td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" data-file="<?php echo get_template_directory() . '/select.php'; ?>" /></td> 


及以下線路發送的文件路徑,

url:"select.php", 

可以

url: $(this).data('file'), 

我還沒有從我身邊測試。但這只是您的代碼方式的一個想法。


EDITED:

<td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td> 

可以

<td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" data-file="<?php echo get_template_directory_uri() . '/select.php'; ?>" /></td> 


及以下線路,

url:"select.php", 

可以

url: $(this).data('file'), 
+0

否則,你可以像這樣使用' url:<?php echo get_template_directory(); ?> +'select.php',' –

+0

使用時:「url:$(this).data('file'),」錯誤:jquery.min.js:4無法加載file:/// C: /xampp/htdocs/wordpress/wp-content/themes/newthemeselect.php:跨協議請求僅支持協議模式:http,data,chrome,chrome-extension,https顯示!當使用「url:<?php echo get_template_directory();?> +'select.php',」錯誤:「Uncaught SyntaxError:Unexpected token:」出現 – blubezblaze

+0

將「'select.php'」替換爲「'/ select .php'「並用get_template_directory_url()替換'get_template_directory()'' –

0

保存PHP文件頁面select.php並創建一個名爲「選擇」在WordPress頁空白頁的伎倆!雖然而不是網址:「/選擇」我只用「選擇」,它的工作!再次感謝您的時間安東尼和Thirumani guhan!:)

相關問題