只传递一个值到php文件

时间:2013-03-27 15:55:04

标签: php javascript jquery ajax

我正在尝试通过创建股票组合来学习php和javascript。我想要做的是让表的价格单元自动更新而不使用javascript刷新页面。我创建了一个新的php文件,只获得该组合的价格字段,但我似乎无法让它工作。我在这个价格领域遇到了很多错误。我已将所有文件放在pastebin。任何帮助,将不胜感激。 enter image description here

portfolio.php file

<table class="table table-hover center-table table-bordered">
        <tr>
        <th>Symbol</th>
        <th>Name</th>
        <th>Shares</th>
        <th>Price</th>
        <th>Total</th>
        </tr>
<?php foreach ($shares as $row): ?>

        <tr>
        <td><?= $row["symbol"]?></td>
        <td><?= $row["name"]?></td>
        <td><?= $row["shares"]?></td>

        <td id="price">$<?= number_format($row["price"],2)?></td>

        <td>$<?= number_format($row["total"],2)?></td>
        </tr>

<? endforeach ?>



<tr>
    <td>CASH</td>
    <td></td>
    <td></td>
    <td></td>
    <td>$<?= number_format($cash[0]["cash"], 2)?></td>

</tr>

</table>

<script type="text/javascript" src="js/update.js" ></script>

index.php文件

<?php

// configuration
require("../includes/config.php");

//query user's portfolio

$rows = query("SELECT * FROM shares WHERE id = ?", $_SESSION["id"]);
$cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);

    //create array to store the shares
    $shares = [];

    //for each of the user info

    foreach($rows as $row){

        //lookup stock info
        $stock = lookup($row["symbol"]);


        if($stock !== false){

            $shares[] = [
                "name" => $stock["name"],
                "price" => $stock["price"],
                "shares" => $row["shares"],
                "symbol" => $row["symbol"],
                "total" => $row["shares"]*$stock["price"]

            ];
                 //dump($shares);      
        }

    }

// render portfolio
render("portfolio.php", ["shares" => $shares,"cash" => $cash, "title" => "Portfolio"] );  
?>

update.php

<?php

require("../includes/functions.php"); 
require("../includes/config_update.php");

$rows = query("SELECT * FROM shares WHERE id = ?", $_SESSION["id"]);
$cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
        //create array to store the shares
    $shares = [];

    //for each of the user info

    foreach($rows as $row){

        $stock = lookup($row["symbol"]);

        if($stock !== false){

            $shares[] = [

                "name" => $stock["name"],
                "price" => $stock["price"],
                "shares" => $row["shares"],
                "symbol" => $row["symbol"],
                "total" => $row["shares"]*$stock["price"]
            ];

        }
    }

// render portfolio
render("portfolio.php", ['shares' => $shares, 'cash' => $cash]);  
?>

update.js

$(document).ready(function(){
    var updater = setTimeout(function(){
        $('#price').load('update.php', 'update=true');
    },6);
});

当我第一次运行时,我在价格单元格中获取整个表格以及这些错误消息注意:未定义的变量:portfolio.php第10行中的股票 在第10行的portfolio.php中为foreach()提供的参数无效 第31行的portfolio.php中的未定义变量现金

我不再有任何错误,但现在我将整个页面放在表格的价格字段中。

1 个答案:

答案 0 :(得分:1)

你在index.php中传递了一个参数数组,其中一个参数的密钥为shares,但是在update.php中你试图传回该数组,在update.php中更改它

 render("portfolio.php", $share);

render("portfolio.php", ['shares' => $share]);