使用jquery

时间:2019-04-01 18:07:21

标签: javascript php jquery html database

我创建了一个查看模块,用户可以在其中查看数据库中的值,并添加了一个编辑按钮,当您单击该按钮时,模态应弹出并带有基于id的值。

当前,这是单击编辑按钮时得到的:

现在我仍然缺少一件事,这是我已经创建的JavaScript:

<script>
    $('#exampleModal').on('show.bs.modal', function (event) {
          var button = $(event.relatedTarget) // Button that triggered the modal
          var recipient = button.data('whatever') // Extract info from data-* attributes
          var modal = $(this);
          var dataString = 'id=' + recipient;

            $.ajax({
                type: "GET",
                url: "editdata.php",
                data: dataString,
                cache: false,
                success: function (data) {
                    console.log(data);
                    modal.find('.dash').html(data);
                },
                error: function(err) {
                    console.log(err);
                }
            });
    })
</script>

我的fetch.php纯粹是PHP,不确定如何将JS添加到其中。这是我的fetch.php

<?php
$connect = mysqli_connect("localhost", "root", "", "seatrequest");
$output = '';
$colors = array();
$colors["Ongoing"] = "red";
$colors["Closed"] = "#00FF00";
if(isset($_POST["query"]))
{
	$search = mysqli_real_escape_string($connect, $_POST["query"]);
	$query = "
	SELECT * FROM request 
	WHERE req_date LIKE '%".$search."%'
	OR reqname LIKE '%".$search."%'
	OR natureofreq LIKE '%".$search."%'
	OR postitle LIKE '%".$search."%'
	OR critlevel LIKE '%".$search."%'
	OR deadline LIKE '%".$search."%'
	OR account LIKE '%".$search."%'
	OR newaccname LIKE '%".$search."%'
	OR lob LIKE '%".$search."%'
	OR site LIKE '%".$search."%'
	OR status LIKE '%".$search."%'
	";
}
else
{
	$query = "
	SELECT * FROM request ORDER BY reqnumber";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
	$output .= '<div class="table-responsive">
					<table class="table table bordered">
						<tr>
							<th style="background-color: #e6ecff;">Date Requested</th>
							<th style="background-color: #e6ecff;">Requested By</th>
							<th style="background-color: #e6ecff;">Nature of Request</th>
							<th style="background-color: #e6ecff;">Position Title</th>
							<th style="background-color: #e6ecff;">Critical Level</th>
							<th style="background-color: #e6ecff;">Deadline</th>
							<th style="background-color: #e6ecff;">Account</th>
							<th style="background-color: #e6ecff;">Name of Account (For New Seat)</th>
							<th style="background-color: #e6ecff;">LOB</th>
							<th style="background-color: #e6ecff;">Site</th>
				            <th style="background-color: #e6ecff;">Status</th>
				            <th style="background-color: #e6ecff;">Action</th>
				            <th style="background-color: #e6ecff;">Edit</th>
						</tr>';
	while($row = mysqli_fetch_array($result))
{
    $output .= '<tr>
    			<td>'.$row["req_date"].'</td>
    			<td>'.$row["reqname"].'</td>
    			<td>'.$row["natureofreq"].'</td>
    			<td>'.$row["postitle"].'</td>
    			<td>'.$row["critlevel"].'</td>
                <td>'.$row["deadline"].'</td>
                <td>'.$row["account"].'</td>
                <td>'.$row["newaccname"].'</td>
                <td>'.$row["lob"].'</td>
                <td>'.$row["site"].'</td>
                <td style="color:' . $colors[$row["status"]] . ';">' .$row["status"] . '</td>
                <td>
                	<form method="post" action="update-work-status.php">
                		<input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
                		<button class="fa fa-check" style="color: green" type="submit" name="approve" value=""></button><button class="fa fa-close" style="color: red" type="submit" name="decline" value=""></button>
                	</form>
                </td>
                <td><a class="btn btn-small btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="'.$row['reqnumber'].' ">Edit</a></td>
            	</tr>
        ';
}  
echo $output;
}
else
{
	echo 'Data Not Found';
}
?>

我想我的问题是如何将JS合并到fetch.php中?我不确定在添加JS后它是否还能工作,但我会找出来的。

Edit Modal

1 个答案:

答案 0 :(得分:0)

要将数据从JS传递到您的fetch.php,我将创建数据array并使用POST方法,如下所示:

var dataString = {
  id: recipient,
  other: 'string'
}
$.ajax({
  type: "POST",
  url: "fetch.php",
  data: dataString,
  cache: false,
  success: function (data) {
      console.log(data);
      modal.find('.dash').html(data);
  },
  error: function(err) {
      console.log(err);
  }
});

并在您的php中:

$id = $_POST['id'];
$other = $_POST['other'];
//Do something with your data

让我知道是否有帮助。