从href链接获取价值

时间:2015-08-09 16:25:59

标签: javascript php jquery html

我正在尝试从href链接获取值并使用if用于我的数据库。但我无法获得价值。

以下是我的按钮代码

<a class="btn btn-info" href="scheduleSetTime.php?id='. $id .'">' . 'Set Time' . '</a>

scheduleSetTime.php

<div class="container bs-docs-container">
  <div class="panel panel-default">
    <div class="panel-heading">Schedule Activity</div>
    <div class="panel-body">
      <div id="formbox">
        <form id="formTable">

          <table class="table table-hover">
            <thead>
              <tr>
                <th>Start Time</th>
                <th>End Time</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
                <th>Sunday</th>
                <th></th>


              </tr>
            </thead>

            <tbody>
              <tr>
                <th>
                  <input type="text" class="form-control" name="startTime" id="starttime" width="50px">
                </th>
                <th>
                  <input type="text" class="form-control" name="endTime" id="endtime">
                </th>
                <th>
                  <input type="checkbox" name="Monday" value="1" id="monday">
                </th>
                <th>
                  <input type="checkbox" name="Tuesday" value="1" id="tuesday">
                </th>
                <th>
                  <input type="checkbox" name="Wednesday" value="1" id="wednesday">
                </th>
                <th>
                  <input type="checkbox" name="Thursday" value="1" id="thursday">
                </th>
                <th>
                  <input type="checkbox" name="Friday" value="1" id="friday">
                </th>
                <th>
                  <input type="checkbox" name="Saturday" value="1" id="saturday">
                </th>
                <th>
                  <input type="checkbox" name="Sunday" value="1" id="sunday">
                </th>
                <th>
                  <input type="button" class="btn btn-success" value="Add" id="send">
                </th>
                <th>Click the Add button to add to the list below</th>
              </tr>
            </tbody>
          </table>

        </form>
      </div>

      <table class="table table-hover">
        <thead>
          <tr>
            <th>Start Time</th>
            <th>End Time</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
            <th>Sunday</th>
            <th></th>
            <th></th>
          </tr>
        </thead>
        <tbody id="comment">
        </tbody>
      </table>

我用于scheduleSetTime.php的JavaScript,

$(document).ready(function() {

  function showComment() {
    $.ajax({
      type: "post",
      url: "registerarray.php",
      data: "action=showcomment",
      success: function(data) {
        $("#comment").html(data);
      }
    });
  }
  showComment();

  $("#send").click(function() {

    var starttime = $("#starttime").val();
    var endtime = $("#endtime").val();

    if (document.getElementById('monday').checked) {
      var monday = $("#monday").val();
    }
    if (document.getElementById('tuesday').checked) {
      var tuesday = $("#tuesday").val();
    }
    if (document.getElementById('wednesday').checked) {
      var wednesday = $("#wednesday").val();
    }
    if (document.getElementById('thursday').checked) {
      var thursday = $("#thursday").val();
    }
    if (document.getElementById('friday').checked) {
      var friday = $("#friday").val();
    }
    if (document.getElementById('saturday').checked) {
      var saturday = $("#saturday").val();
    }
    if (document.getElementById('sunday').checked) {
      var sunday = $("#sunday").val();
    }



    var dataString = "starttime1=" + starttime + "&endtime1=" + endtime + "&monday1=" + monday + "&tuesday1=" + tuesday + "&wednesday1=" + wednesday + "&thursday1=" + thursday + "&friday1=" + friday + "&saturday1=" + saturday + "&sunday1=" + sunday + "&action=addcomment";

    $.ajax({
      type: "post",
      url: "registerarray.php",
      data: dataString,
      success: function(data) {
        showComment();
        $("#formTable")[0].reset();

      }

    });
    return false;

  });
});

我的registerarray.php,

<?php

$action = $_POST["action"];

if ($action == "showcomment") {

    require_once 'dbfunction.php';
    include_once 'checkLoginStatus.php';
    $con = getDbConnect();
    $schedule_Id = $_GET["id"];
    $staff_Id = $staff_information["staff_Id"];

    if (!mysqli_connect_errno($con)) {
        $show = "SELECT * FROM scheduletime WHERE schedule_Id = '$schedule_Id' AND staff_Id = '$staff_Id'";

        $result = mysqli_query($con, $show);

        $html = "";
        while ($row = mysqli_fetch_array($result)) {
            $r = "<tr>";
            $r .= "<td>" . $row["startTime"] . "</td>";
            $r .= "<td>" . $row["endTime"] . "</td>";
            $r .= "<td>" . $row["Monday"] . "</td>";
            $r .= "<td>" . $row["Tuesday"] . "</td>";
            $r .= "<td>" . $row["Wednesday"] . "</td>";
            $r .= "<td>" . $row["Thursday"] . "</td>";
            $r .= "<td>" . $row["Friday"] . "</td>";
            $r .= "<td>" . $row["Saturday"] . "</td>";
            $r .= "<td>" . $row["Sunday"] . "</td>";
            $r .= "<td></td>";
            $r .= "</tr>";
            $html .= $r;
        }
        header("Content-type: text/plain");
        echo( $html);

        
        mysqli_close($con);
    } else {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    ?>


    <?php

} else if ($action == "addcomment") {
    require_once 'dbfunction.php';
    $con = getDbConnect();
    $schedule_Id = $_GET["id"];

    $starttime2 = $_POST['starttime1'];
    $endtime2 = $_POST['endtime1'];
    $monday2 = $_POST['monday1'];
    $tuesday2 = $_POST['tuesday1'];
    $wednesday2 = $_POST['wednesday1'];
    $thursday2 = $_POST['thursday1'];
    $friday2 = $_POST['friday1'];
    $saturday2 = $_POST['saturday1'];
    $sunday2 = $_POST['sunday1'];
    $staff_Id = $staff_information["staff_Id"];

    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
        $query = "INSERT INTO scheduletime (schedule_Id, staff_Id, startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)" .
                "VALUES ('$schedule_Id', '$staff_Id', '$starttime2', '$endtime2', '$monday2', '$tuesday2', '$wednesday2', '$thursday2', '$friday2', '$saturday2', '$sunday2')";

        mysqli_query($con, $query);
    }
    echo "Your comment has been sent";
    mysqli_close($con);
}

scheduleSetTime.php的视图

enter image description here

在registerarray.php中,我使用了$ _GET [“id”];获取ID号,但它没有用。是否有另一种获取id值的方法?

1 个答案:

答案 0 :(得分:1)

当用户单击其中一个按钮时,GET变量id将发送到scheduleSetTime.php。您尝试在registerarray.php中检索它。

解决方案是:

  1. 在scheduleSetTime.php中为表单添加一个隐藏的输入元素,其中de id为值。例如:

    <input type="hidden" name="id" value="<?php echo htmlentities($_GET['id']); ?>" id="id">
    
  2. 将POST请求中的id值发送到registerarray.php。在声明dataString之后添加以下代码应该可以解决问题:

    dataString += "&id=" + $("#id").val();
    
  3. 在registerarray.php中检索id。由于id是作为POST变量发送的,因此您应该在$_GET['id']的registerarray.php中$_POST['id']

  4. 那应该可以解决你的问题!