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

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

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件