douyou9923 2015-09-15 20:56
浏览 21
已采纳

表单提交后PHP更新会话值

I have a form with a textbox that requires a date value (date1). When date1 is updated and submitted (posts to same page), it updates the session value with no issues but doesn't update date2 - Explained below.

When date1 is updated date2 doesn't update. Date2 will get a value by adding 30 days to date1.

In summary: Date1 - session variable gets updated after submitting the form. Date2 - session variable does not get updated after submitting the form. BUT both date1 & date2 updates in the database.

I have spent days trying to find the issue. I figured out how to get date1 to update the session variable but date2 I cannot figure it out.

Note: I echo the session variable. That's how I know it's not updating on the page. The echo isn't in the code below.

if(isset($_POST['submit'])){

$memberid = $_SESSION['memberid'];  
$date1 = $_POST['date1'];
$date2 = $_SESSION['date2'];    

if(empty($_POST['date1'])){
$errordate1[] = 'Select a Date';
$_SESSION['date1'];

} else {

echo $_SESSION['date1'] = $_POST['date1'];

}


if(empty($_POST['date2'])){

$_SESSION['date2'];

} else {

echo $_SESSION['date2'] = $_POST['date2'];

}



if(!isset($errordate1)){

$date1 = date('Y-m-d', strtotime($_POST['date1']));
$date2 = date('Y-m-d', strtotime($date1 . " +30 days"));
echo $date2; // I do not get the updated date.  
//die($date2); // if I uncomment this line I get the updated value for date2.


    try {

    $_SESSION['date2'] = $_POST['date2'];
    $stmt = $db->prepare('UPDATE theDates SET date1 = :date1, date2 = :date2, WHERE 
memberid=:memberid');
    $stmt->execute(array(':date1'=>$date1, ':date2'=>$date2, ':memberid'=>$memberid));

    header('Location: profile.php');
    exit;

     } catch(PDOException $e) {
        $errordate1[] = $e->getMessage();
     }

    }

    } 

HTML - I added a hidden value for date2 for testing. Thought I would try it and see what happens to trying to get something to work.

<form method="post" action="prof.php">
<input name="date1" type="text" value="<?php if(isset($errordate1)){ echo $_POST['date1']; } ?>" />
<input name="date2" type="hidden" value="<?php echo $_SESSION['date2']; ?>" />
<input type="submit" name="submit" value="Submit">
</form>
  • 写回答

1条回答 默认 最新

  • dousi2013 2015-09-15 22:53
    关注

    Finally found a way to get this to work rather than relying on a bunch of kids. The whole time during the process of changing the code I used:

    $_SESSION['date2'] = $_POST['date2'];
    

    But that was wrong. Instead it's:

    $_POST['date2'] = $date2; 
    

    The code:

    if(!isset($errordate1)){
    
        $date1 = date('Y-m-d', strtotime($_POST['date1']));
        $date2 = date('Y-m-d', strtotime($date1 . " +30 days"));
    
        if(isset($_POST['date2'])) { 
           $_POST['date2'] = $date2; 
    
           }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答