PHP - 无法从前端表单获取值

时间:2017-04-21 09:58:47

标签: javascript php

我在从前端表单获取值并在更新查询中使用它时遇到问题。前端表格填充一个工作正常的选择框...

 echo "<select>";
 while ($row = $result->fetch_assoc()) {
       echo "<option name='plot' value=\"PlotNumber\">" . 
 $row['PlotNumber'] . "</option>";
 }  
 echo "</select>";

以下是AJAX调用:

 $('.update').click(function() {
    $.ajax({
        type: "POST",
        url: '../php/update.php',
        success:function(html){
            document.getElementById("result").innerHTML=html;   
        }  
    });
    return false;
});

这是php:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $plot = $_POST['plot'];
    $sql = "UPDATE Developments SET Price = 'BOOKED' WHERE PlotNumber='".$plot."'";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    echo " records UPDATED successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

我真的难过哪里出错了......有人能发现错误吗?

2 个答案:

答案 0 :(得分:0)

  

您的错误在选择它没有名称属性

echo "<select>";
 while ($row = $result->fetch_assoc()) {
       echo "<option name='plot' value=\"PlotNumber\">" . 
 $row['PlotNumber'] . "</option>";
 }  
 echo "</select>";
  

上面应该是这样的:

<?php

echo "<select name=\"plot\">";
 while ($row = $result->fetch_assoc()) {
       echo "<option value=\"PlotNumber\">" . 
 $row['PlotNumber'] . "</option>";
 }  
 echo "</select>";

 ?>
  

然后修复你的ajax以将数据发送到你的php脚本

$('.update').click(function() {
    var FormData = $('#formID').serialize(); 
 $.ajax({
        type: "POST",
        url: '../php/update.php',
        data : FormData,
        success:function(html){
            document.getElementById("result").innerHTML=html;   
        }  
    });
    return false;
    });
  

然后你需要以正确的方式使用预备语句

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$plot = $_POST['plot'];
$sql = "UPDATE Developments SET Price = 'BOOKED' WHERE PlotNumber='".$plot."'";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
  

上面应该是这样的:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$plot = $_POST['plot'];
$sql = "UPDATE Developments SET Price = 'BOOKED' WHERE PlotNumber= ? ";
$stmt = $conn->prepare($sql);
$stmt->execute([$plot]);
echo " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
 ?>

答案 1 :(得分:0)

首先,您必须为HTML中的select标记命名:

.card-block {
  .statistics {
    display: flex;
    justify-content: space-around;
   .statistic {
      height: 65px;
      display: flex;
      .count {
        display: block;
        width: 100%;
      }
      .rank {
        display: block;
        width: 100%;
      }

你已经像这样更新了你的jquery。你没有向你的后端发送任何数据。

echo "<select  name='plot'>";
 while ($row = $result->fetch_assoc()) {
       echo "<option value='".$row['PlotNumber']."'>" . 
 $row['PlotNumber'] . "</option>";
 }  
 echo "</select>";