如何在表单POST中创建值到同一页面?

时间:2014-01-22 17:44:20

标签: php mysql

我必须让所有事情都发生在同一页面上。我在这里使用了action="<?PHP echo $_SERVER['PHP_SELF']; ?>",但它不起作用。我在表单下面插入了PHP查询。基本上,我的问题是如何确保表单在同一页面上发布值。如果更新,则会弹出一个弹出窗口。

$user_id=$_SESSION['user_id'];
$date = date("l jS \of F Y h:i:s A");

$query1 ="SELECT daily_limit FROM user WHERE user_id='$user_id'"; 
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
while ($row1 = mysqli_fetch_array($result1)) {
    $dailylimit=$row1['daily_limit'];
}               

$query2 = "SELECT SUM(debit) AS debited_today FROM transaction WHERE user_id = '$user_id' AND date = CURRENT_DATE" ;
$result2 = mysqli_query($link, $query2) or die (msqli_error($link));
while ($row2 = mysqli_fetch_array($result2)){
    $debited_today = $row2['debited_today'];
}

//表单

    <form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" > 
        <table id="table">
            <tr>
                <td class="alt">Existing Daily Limit</td> 
                <td>S$ <?php echo $dailylimit; ?> </td>
                <input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
            </tr> 
            <tr>
                <td class="alt"><label for="newdailylimit">New Daily Limit</label></td>
                <td>$ <select name="newdailylimit">
                        <option value="100.00">100.00</option>
                        <option value="500.00">500.00</option>
                        <option value="1000.00">1000.00</option>
                        <option value=5000.00">5000.00</option>
                    </select></td>
            </tr>
            <tr>
                <td class="alt">Amount Debited Today</td>
                <td>S$ <?php echo $debited_today; ?></td>
            </tr>
            <tr>
                <td class="alt">Amount Debited Left</td>
                <td>S$ <?php echo ($dailylimit - $debited_today); ?> </td>
            </tr>
        </table>
        <br/>
        <input type="submit" name="submit "value="Submit"></input>
    </form>

//我需要POST的值

$dailylimit = $_POST['dailylimit'];
$newdailylimit = $_POST['newdailylimit'];

if ($dailylimit != $newdailylimit){
    $query = "UPDATE user SET daily_limit='$newdailylimit' WHERE user_id='$user_id'";
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    echo "<script>alert('You have successfully updated your daily limit');</script>";
}
else if ($dailylimit == $newdailylimit){
    echo "<script>alert('You have selected the same daily limit as your previous one. Please choose a different one. ');</script>";
}
else{

}

2 个答案:

答案 0 :(得分:1)

啊,我可能已经发现了问题所在:

submit后面有一个空格,value之前没有空格。

这会阻止您的表单提交。

<input type="submit" name="submit "value="Submit"></input>
                              ---^ ^

将其更改为:

<input type="submit" name="submit" value="Submit">

双引号可能搞砸了。


其他错误但无法解决问题的事情

另外

<option value=5000.00">5000.00</option>

应该是

<option value="5000.00">5000.00</option>

另外

<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
                                       // You have an extra space here ^  

这将更改您的$dailylimit,并附加一个空格。

答案 1 :(得分:-2)

让你的行动=''。它将发布到自己。 然后在你的页面顶部检查请求是否是post ex。 if($ _ POST){//添加你的代码} else {// yourform}