2017-12-27 1147 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!:)

相关问题