因某些原因无法传递变量? PHP

时间:2018-03-04 15:08:47

标签: php html sql chart.js

当我调用$_POST['xalue']时,它只会返回$xaxis的第一个索引。

$xaxis = '["-100","-100","240","1056","3560","-650","5980.5","1020"]'

我只是试图传递信息,所以当按下按钮添加仪表板时,我可以传递数据。谢谢

$query = mysqli_query($db, "SELECT ID From users WHERE Username='$user'");
$fetch = mysqli_fetch_assoc($query);
$data = $fetch{"ID"};

$sql = "SELECT dashboardname FROM dashboards WHERE dashboarduserID=$data";
$result = $db->query($sql);
$addarray = array();
if ($result->num_rows > 0) {
    global $xaxis;
    echo $xaxis;
    while ($row = $result->fetch_assoc()) {
        array_push($addarray, $row['dashboardname']);
    }
    echo '<form method="post">';?>
    <input type="text" name="xvalue" value="<?php echo $xaxis ?>">
    <?php
    echo 'Add current graph to :';
    echo '<select name="addgraph">';
    foreach ($addarray as $item) {
        echo '<option value="' . $item . '">' . $item . '</option>';
    }
    echo '</select>';
    echo '<input type="submit" name="addtodash" value="Add">';
    echo '</form>';
} else {
    echo "0 results";
    if (isset($_REQUEST["addtodash"])) {
    $xaxis = $_POST['xvalue'];
    $query = mysqli_query($db, "SELECT ID From users WHERE Username='$user'");
    $fetch = mysqli_fetch_assoc($query);
    $data = $fetch{"ID"};
    $nameofdash = $_POST['addgraph'];
    mysqli_query($db, "INSERT INTO dashboardsdata (userid,dashboardname,xdata,ydata) VALUES ($data,\"$nameofdash\",\"$xaxis\",\"$yaxis\");");
    echo "done";
}

2 个答案:

答案 0 :(得分:0)

$xvalue中的双引号将匹配用于分隔value="..."属性的双引号。你需要对它们进行编码:

<input type="text" name="xvalue" value="<?php echo htmlspecialchars($xaxis); ?>">

答案 1 :(得分:0)

你可能需要做两件事:

  1. 正如Barmar所说,你不能“简单地”回应阵列。您需要将其解析为值,例如

    value="implode(';', $array)"

  2. 另一种选择是声明多个输入,每个输入都有一个数组值。