dongxiaowei_1234 2011-10-15 07:17 采纳率: 0%
浏览 25
已采纳

无法从$ _POST设置变量

I can't set a variable from a post array.

I have a simple form with a hidden field in it: <input name="sid" type="hidden" id="sid" value="<?=$sid?>">

This hidden field gets sent off to a second file (exec.php) where I have the following code:

$sid = $_POST['sid'];

For some reason, when trying to set $sid, it gets a NULL value. For haha's, I ran the following:

foreach($_POST as $var => $value)
{
echo $var . ' : ' . $value . "<br>";
}

This provided a correct value of 1938 for sid. I've looked at this for 3 hours and can't find what is happening. I expect something extremely stupid...any thoughts?


Here is the form on enter.php

    <form name="form1" method="post" action="exec.php">
     <input name="sid" type="hidden" id="sid" value="<? echo($sid); ?>">
     <input name="ticket_totals" type="hidden" id="ticket_totals" value="<?=$ticket_totals?>">
     <input name="emp" type="hidden" id="emp" value="<?=$emp?>">
     <input name="submit" type="submit" id="submit" value="Submit">
     <input type="submit" name="submit" id="submit" value="Close">
    </form>

Here is the POST output on exec.php: type : Other ticket_totals : 0 emp : 105 sid : 1939 submit : Submit


Okay - this was poor syntax on my part but now I'm curious as to why.

I left out quotation marks - the solution is as simple as this: $sid = $_POST["sid"]

Now it works like a champ.

Any takers on why? I'd guess there is a setting in the php.ini that requires the quotes. Strangely enough, I have other variables called from the POST array that i'm not using quotes for and they're working fine...

  • 写回答

4条回答 默认 最新

  • duandai2178 2011-10-15 07:25
    关注

    Use Console in FireBug to inspect the POST request to see what is the sid value that is being sent.

    If the sid value in request is ok, use var_dump($_POST["sid"]); to see the results on the server.

    EDIT: it's considered good PHP style to use the quotes when accessing the associative array because quote-less keys are indistinguishable from constants:

    define('myVar',3);
    echo $array[myVar]; // retrieves $array[3], not $array['myVar'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装