按钮添加模态ajax

时间:2018-06-05 23:14:18

标签: php ajax modal-dialog

我正在尝试添加一个按钮,在表中添加一行也在数据库中添加值但是当我单击按钮时它会刷新我的页面并且不会添加任何行并且在我的数据库中不会添加任何价值观。

这是html表单:

<div class="modal fade" id="modaladdWork" tabindex="-1" role="dialog" aria- 
labelledby="exampleModalLabel" aria-hidden="true" >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="modaladdWorkLabel">New Work</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
            <form name="formaddWork">
              <div class="modal-body">
                <div class="container w-75">
                    <select id="roleaddWork" name="role" class="form-control" aria-label="Role" aria-describedby="basic-addon1">
      <option>Direttore</option>
      <option>Vice Direttore</option>
      <option>Capo Settore</option>
      <option>Dipendente</option>
      <option>Stagista</option>
      </select>    <br>
                    <input type="number" id="yearaddWork" name="year" min="1900" class="form-control" placeholder="Year" aria-label="Year" aria-describedby="basic-addon1">
                    <br/>
                    <input type="text" id="placeaddWork" name="place" class="form-control" placeholder="Place" aria-label="Place" aria-describedby="basic-addon1">
                    <br />
                      <?php  
      $query = "select * from company;";
     $result=mysqli_query($connessione, $query);
      echo mysqli_error($connessione);
      if(mysqli_num_rows($result) > 0){ 
     echo "<div style='float:left';><select id='companyaddWork' name='company' class='form-control' aria-label='Company' aria-describedby='basic-addon1'>"; 
     while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

     echo "<option>".$row["name"]."</option>";

    }
     echo "</select></div>  <div class='modal-footer'><div style='float:right';>
                <button type='button' class='btn btn-primary float-right' data-toggle='modal' data-target='#modalCompany'>
          Add Company
        </button>
              </div></div>";
     }
      else{
          echo "Inserisci prima una compagnia";
      }

      ?>
                </div>
              </div>
              <div class="modal-footer">
                <button type="submit" id="submitaddWork" class="btn btn-primary">Save</button>
              </div>
            </form>
        </div>
      </div>
    </div>

这是javascript代码:

function openmodal_addWork()
        {

            $('#modaladdWork').find('.modal-title').text("Add Work row");
            $('#modaladdWork').find(':submit').text("Add");
            $('#submitaddWork').click(function() { 
                op_addWork();
            });
            $('#modaladdWork').modal('show');
        }

        function op_addWork()
        {   
            role = $('#roleaddWork').val();
            year = $('#yearaddWork').val();
            place = $('#placeaddWork').val();
            company = $('#companyaddWork').val();
            $('#modaladdWork').modal('hide');
            $.ajax({
                type: "POST",
                url: "json/Lavoro.php",
                data: {
                    'role': role,
                    'year': year,
                    'place': place,
                    'company': company
                },
                success: function(data){
                        console.log(data);
                        columns = $(button).closest('tr').find('td');
                        columns[0].innerHTML = role;
                        columns[1].innerHTML = year;
                        columns[2].innerHTML = place;
                        columns[3].innerHTML = company;
                }
            });
        }

这个文件“Lavoro.php”位于“json”文件夹中:

 <?php
 if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
 {
   header("Location: ../Principale.php");
   exit();
 }

 if(!isset($_COOKIE["id"]))
 {
   $json_data = array(
'draw'            => 0,
'recordsTotal'    => 0,
'recordsFiltered' => 0,
'data'            => [],
'error'           => 'Laravel Error Handler',
          );
                  $json = json_encode($json_data);
             echo $json;
        exit();
 }   



 include("../DB.php"); //dati configurazione del database


 if(isset($_POST["role"]) && isset($_POST["year"]) && isset($_POST["place"]) && isset($_POST["company"]))
 { 
                  $role = mysqli_real_escape_string($connessione, $_POST["role"]);
             $year = mysqli_real_escape_string($connessione, $_POST["year"]);
        $place = mysqli_real_escape_string($connessione, $_POST["place"]);
   $company = mysqli_real_escape_string($connessione, $_POST["company"]);

           $result=mysqli_query($connessione,"select company_id from company where name='".$company."';");
       $row=mysqli_fetch_array($result,MYSQLI_NUM);
  $comp_id=$row[0];
$eeid=$_COOKIE['id'];

   $result = mysqli_query($connessione, "INSERT into WORK_EXPERIENCE (role,year,place,company_id,user_id) VALUES ('$role','$year','$place','$comp_id,'$eeid')");

   $json_data = array(
                                //"id" => $id,
                                "result" => 1
                         );
$json = json_encode($json_data);
echo $json;
 }



 ?>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决。在这一行:

 $result = mysqli_query($connessione, "INSERT into WORK_EXPERIENCE 
 (role,year,place,company_id,user_id) VALUES 
 ('$role','$year','$place','$comp_id,'$eeid')")

$ comp_id没有峰会。