doutong2132 2018-07-24 13:58
浏览 55

将值从php页面传递到其他页面的模态

I'm trying to edit a post using this snippiet of from my query.php page:

echo '<button type="submit"  onclick="openModal(this)" id="btn-edit"
name="edit" value='.$postID.'></button>';


function openModal(id)
{
    var editpost = id.value;
        $.ajax({
            url:"query.php",
            method:"POST",
            data:{ editpost : editpost },
            success:function(data)
            { //
            },
            error: function () {//
            }
        });
    $('#edit').modal('show');
}

The function to get the data from the post selected is on the same php page. This is the code:

if(isset($_POST['editpost'])) 
{
    session_start();
    $editpostid = $_POST['editpost'];

    if($editpostid != "")
    {
        $sql = "SELECT * FROM post WHERE PostId = '" . $editpostid . "'";
    }

    if($sql != "")
    {
        $qry = mysqli_query($connection, $sql);
        if (mysqli_num_rows($qry) > 0) 
        {
            foreach($qry as $row)
            {
                $_SESSION['editpostdesc'] = $row['PostDesc'];
                $_SESSION['editpostfile'] = $row['PostFile'];
                $_SESSION['editpostid'] = $row['PostId'];
            }
        }
    }
}

But the modal is on the other page (main page). I want to get the data of the post and display it on the modal, so I tried using $_SESSION. Yes, I got the data and was able to display it on the modal, but the problem is when I try to edit another post, the first value assigned to the session cannot be replaced. Is there is any other way that I can pass the values without using session? I'm really running out of ideas, I'm just starting my first web project.

  • 写回答

1条回答 默认 最新

  • dongyi2159 2018-07-24 15:12
    关注

    if you get multiple row means this methods will worked...

    if(isset($_POST['editpost'])) 
    {
        session_start();
        $editpostid = $_POST['editpost'];
    
        if($editpostid != "")
        {
            $sql = "SELECT * FROM post WHERE PostId = '" . $editpostid . "'";
        }
    
        if($sql != "")
        {
            $qry = mysqli_query($connection, $sql);
            if (mysqli_num_rows($qry) > 0) 
            {
                foreach($qry as $row)
                {
                    $res[] = array(
                                    'editpostdesc' => $row['PostDesc'],
                                    'editpostfile' => $row['PostFile'],
                                     'editpostid'  => $row['PostId']
                                   ); 
                }
    
    
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大