我无法从ajax php调用访问wordpress函数

时间:2017-04-10 11:22:39

标签: javascript php jquery ajax

我在wordpress中的选择它正在工作但是 我有wordpress的问题我想更新表但返回问题

当我想在wordpress中更新我的表时,这是错误: this is the error when i want to update my table in wordpress

2 个答案:

答案 0 :(得分:1)

// Insert location of wp-load.php
require("/var/www/yourdomain.com/htdocs/wp-load.php");
// Insert function to get db connection environment
function db_connect()
{   
    $mydb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        return $mydb;
}
// Sql Query
$sql="UPDATE inscription SET ".$_POST["name"]." = '".$_POST["value"]."'WHERE id = '".$_POST["pk"]."'";

$result_insert = db_connect()->get_results( $sql );

答案 1 :(得分:-1)

这是我的页面我有ajax和jquery

 <?php /* Template Name: fetch presence*/
       $today = getdate();
       $y = $today['year'];
       $today = getdate();
       $y = $today['year'];
    ?>
    <!DOCTYPE html>
    <html>
    <head>
      <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
      <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.1/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.1/bootstrap3-editable/js/bootstrap-editable.js"></script>
    <style>
    .btn-a{
    display: flex; justify-content: center;
    margin-top: 20px;
    }
    .img{
      display: flex; justify-content: center;
    }
    .btn-group .button {
        background-color:#f44336; /* Green */
        border: none;
        color: white;
        padding: 15px 32px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        cursor: pointer;
        float: top;
        box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
    }
    .btn-group .button:hover {
        background-color: #3e8e41;
    }
    </style>
    </head>
    <body>
    <div class="img"><img src="http://localhost/wordpress/wp-content/uploads/2017/04/calender.png" height="117px"></div>
    <header class="btn-a">
    <div class="btn-group">
      <button class="button" Onclick="parent.location='Seminaire1.php'">Seminaire 1</button>
      <button class="button" Onclick="parent.location='Seminaire2.php'">Seminaire 2</button>
      <button class="button" Onclick="parent.location='Seminaire3.php'">Seminaire 3</button>
      <button class="button" Onclick="parent.location='Seminaire4.php'">Seminaire 4</button>
    </div>
    </header>
      <div class="container">
      <h1 align="center">Feuille de Présence Seminaire 2: Promotion <?php echo $y;?></h1>
      </br>
      <table class="table table-bordered table-striped">
        <thead>
         <tr>
         <th width="2%" >Id</th>
          <th width="10%" >Cin</th>
          <th width="10%">Nom</th>
          <th width="10%">Prenom</th>
          <th width="10%">Spécialité</th>
          <th width="10%">Presnece</th>
          <th width="10%">Presnece</th>
          <th width="10%">Presnece</th>
         </tr>
        </thead>
        <tbody id="employee_data">
        </tbody>
       </table>

    </body>
    </html>
    <script type="text/javascript" language="javascript">
                 $(document).ready(function(){

                  function fetch_employee_data()
                  {
                     $.ajax({
                     url:"http://localhost/wordpress/fetch",
                     method:"POST",
                     dataType:"json",
                     success:function(data)
                    {
                        for(var count=0; count<data.length; count++)
                    {
                     var html_data = '<tr><td>'+data[count].id+'</td>';
                     html_data += '<td data-name="cin" class="cin" data-type="text" data-pk="'+data[count].id+'">'+data[count].cin+'</td>';
                     html_data += '<td data-name="nom" class="nom" data-type="text" data-pk="'+data[count].id+'">'+data[count].nom+'</td>';
                     html_data += '<td data-name="prenom" class="prenom" data-type="text" data-pk="'+data[count].id+'">'+data[count].prenom+'</td>';
                    html_data += '<td data-name="Specialite" class="Specialite" data-type="select" data-pk="'+data[count].id+'">'+data[count].Specialite+'</td>';
                    html_data += '<td data-name="Presnecea" class="Presnecea" data-type="select" data-pk="'+data[count].id+'">'+data[count].Presnece+'</td>';
                    html_data += '<td data-name="Presneceb" class="Presneceb" data-type="select" data-pk="'+data[count].id+'">'+data[count].Presnece+'</td>';
                    html_data += '<td data-name="Presnecec" class="Presnecec" data-type="select" data-pk="'+data[count].id+'">'+data[count].Presnece+'</td>';
                     $('#employee_data').append(html_data);
                   }
                   }
                  });
                   }
      fetch_employee_data();
          $('#employee_data').editable({
            container: 'body',
            selector: 'td.cin',
            url: "http://localhost/wordpress/update",
            title: 'Cin Employee',
            type: "POST",
            //dataType: 'json',
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });
         $('#employee_data').editable({
            container: 'body',
            selector: 'td.nom',
            url: "http://localhost/wordpress/update",
            title: 'Nom Employee',
            type: "POST",
            //dataType: 'json',
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });
        $('#employee_data').editable({
            container: 'body',
            selector: 'td.prenom',
            url: "http://localhost/wordpress/update",
            title: 'Prenom Employee',
            type: "POST",
            //dataType: 'json',
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });       
        $('#employee_data').editable({
            container: 'body',
            selector: 'td.Specialite',
            url: "http://localhost/wordpress/update",
            title: 'Specialite',
            type: "POST",
            dataType: 'json',
            source: [{value: "redaction medicale", text: "redaction medicale"}, {value: "epidemologie et recherche clinique", text: "epidemologie et recherche clinique"}],
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });
        $('#employee_data').editable({
            container: 'body',
            selector: 'td.Presnecea',
            url: "http://localhost/wordpress/update",
            title: 'Presnecea',
            type: "POST",
            dataType: 'json',
            source: [{value: "Non", text: "Non"}, {value: "Oui", text: "Oui"}],
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });
        $('#employee_data').editable({
            container: 'body',
            selector: 'td.Presneceb',
            url: "http://localhost/wordpress/update",
            title: 'Presneceb',
            type: "POST",
            dataType: 'json',
            source: [{value: "Non", text: "Non"}, {value: "Oui", text: "Oui"}],
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });
        $('#employee_data').editable({
            container: 'body',
            selector: 'td.Presnecec',
            url: "http://localhost/wordpress/update",
            title: 'Presnecec',
            type: "POST",
            dataType: 'json',
            source: [{value: "Non", text: "Non"}, {value: "Oui", text: "Oui"}],
            validate: function(value){
             if($.trim(value) == '')
             {
              return 'Ce Champ Est Obligatoire';
             }
             }
             });               
             });


    </script>