dougong2005 2018-12-12 02:00
浏览 44
已采纳

如何填充表单以编辑现有数据[重复]

This question already has an answer here:

Code updated to reflect double quote vs single quote.

Instead of populating with the date_requested value from the mysql table, I am getting the following.

Date  <input type="text" id="datepicker" name="date_requested" value="$date_requested"> <br/><br/>

This line is placing $date_requested in the text box.

I have also tried

Date  <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>

This line places php echo in the text box.

Any direction is appreciated. Here is the surrounding section of code:

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
    mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from time_off_requests Where time_off_key='$time_off_key'"); // SQL Query
    $date_requested = $_POST["date_requested"];
    $time_off_begin = $_POST["time_off_begin"];
    $time_off_end = $_POST["time_off_end"];//date
    $use_pto = $_POST["use_pto"];
    $user = $_SESSION["user"];
    $time_off_key = $_SESSION["time_off_key"];
    mysql_query("UPDATE time_off_requests SET date_requested='$date_requested', time_off_begin='$time_off_begin', time_off_end='$time_off_end', use_pto='$use_pto' WHERE time_off_key='$time_off_key'") ;
    header("location: home.php");
}
if($tok_exists)
    {
    Print '
    <form action="edit.php" method="POST">
        Date  <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>
        All Day  <input type="checkbox" name="all_day[]" value="no" /> <br/>
        Start Time  <input type="time"  id="timepicker" name="time_off_begin" /> <br/>
        End Time  <input type="time" id="timepicker2" name="time_off_end" /> <br/> <br/>
        Use PTO <Select name="use_pto">
        <option value="">Select</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
        </select> <br/><br/>
       <input type ="submit" value="Edit Request">  
    </form>
    ';
    }
    else
    {
        Print '<h2 align="center">There is no data to be edited.</h2>';
    }
?>
</div>
  • 写回答

1条回答 默认 最新

  • doujiao3346 2018-12-12 06:18
    关注

    You are mixing single and double quotes wrong. An easier way to set the values into the string from the variables is using variable interpolation, which requires double quotes ("").

    So

    Print "<form action='edit.php' method='POST'>
        Date  <input type='text' id='datepicker' name='date_requested' value='$date_requested'/> <br/><br/>
    </form>";
    

    would indeed output

    <form action='edit.php' method='POST'>
        Date  <input type='text' id='datepicker' name='date_requested' value='2018-12-12'/> <br/><br/>
    </form>
    

    This is because PHP sees the variable $date_requested inside a normal string enclosed within double quotes and replaces it with its value. The single quotes around the variable are interpreted literally and outputted as the quotes enclosing the HTML attribute's value

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能