我无法获得文本框的值

时间:2015-12-18 14:47:04

标签: php twitter-bootstrap

我想将文本框中的数据保存到变量中,以便我可以使用POST将它们发送到另一个页面,我在模态对话框中有三个输入标记:

<div class="modal fade" id="salaryModal" >
      <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-body">
            <div class="form-group ">
            <input class="form-control" type="text" placeholder="hourly rate" name="hourly_rate"/<br>
            <input class="form-control" type="text" placeholder="overtime rate" name="overtime_rate"/<br>
            <input class="form-control" type="text" placeholder="vacation rate" name="vacation rate"/<br>
            <?php while($row = mysqli_fetch_assoc($auth)) {
            $username = $row["username"]; ?>

              <input class="btn btn-success center-block" id="calculate_salary_button" onclick="assignsalary('<?php echo $username; ?>')" value="<?php echo $username; ?>"/></a>
<?php } ?>
            </div>
            <?php } } ?>
          <!--span style="color:blue" id="span_result"></span-->


          </div>
      </div>
      </div>
      </div>

按钮调用为传递的用户名分配工资(工作得很好)

function assignsalary(username) {
      var uname = username;
      var hour_rate = $("#hourly_rate").val();
      var overtime_rate = $("#overtime_rate").val();
      var vacation_rate = $("#vacation_rate").val();
      $('#test_box').html("");  
      $('#test_box').html(hour_rate);  
      var data = new FormData();
      data.append('hourly_rate',hour_rate);
      data.append('username',uname);
      data.append('overtime_rate',overtime_rate);
      data.append('vacation_rate',vacation_rate);
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'set_salary.php', true);
      xhr.onload = function () {
        $('#test_box').html("");  
        $('#test_box').html(this.responseText);
        };
      xhr.send(data);
    }

用户名成功发送,这是其他不正确发送的变量:

以下是set_salary返回的内容:

[age:username:adel hourly_rate:undefined overtime_rate:undefined vacation_rate:undefined

这是set_salary代码:

<?php session_start();
$overtime_rate = $_POST['overtime_rate'];
$vacation_rate = $_POST['vacation_rate'];
$username = $_POST['username'];
$hourly_rate = $_POST['hourly_rate'];
echo "username: {$username}\n";
echo "hourly_rate: {$hourly_rate}\n";
echo "overtime_rate: {$overtime_rate}\n";
echo "vacation_rate: {$vacation_rate}\n";
?>

1 个答案:

答案 0 :(得分:2)

您在Id中错过了<input>并且没有关闭<input>代码

UPDATD CODE

<div class="modal fade" id="salaryModal" >
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-body">
      <div class="form-group ">
      <input class="form-control" type="text" placeholder="hourly rate" id="hourly_rate" name="hourly_rate"/><br>
      <input class="form-control" type="text" placeholder="overtime rate" id="overtime_rate" name="overtime_rate"/><br>
      <input class="form-control" type="text" placeholder="vacation rate" id="vacation_rate" name="vacation rate"/><br>
      <?php while($row = mysqli_fetch_assoc($auth)) {
      $username = $row["username"]; ?>

        <input class="btn btn-success center-block" id="calculate_salary_button" onclick="assignsalary('<?php echo $username; ?>')" value="<?php echo $username; ?>"/></a>
<?php } ?>
      </div>
      <?php } } ?>
    <!--span style="color:blue" id="span_result"></span-->


    </div>
</div>
</div>
</div>

function assignsalary(username) {
      var uname = username;
      var hour_rate = $("#hourly_rate").val();
      var overtime_rate = $("#overtime_rate").val();
      var vacation_rate = $("#vacation_rate").val();
      $('#test_box').html("");  
      $('#test_box').html(hour_rate);  
      var data = new FormData();
      data.append('hourly_rate',hour_rate);
      data.append('username',uname);
      data.append('overtime_rate',overtime_rate);
      data.append('vacation_rate',vacation_rate);
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'set_salary.php', true);
      xhr.onload = function () {
        $('#test_box').html("");  
        $('#test_box').html(this.responseText);
        };
      xhr.send(data);
    }