使用没有jquery或ajax的纯javascript将php值传递给模态

时间:2021-05-05 09:51:33

Is it possible to pass values from PHP to a modal box using plain Javascript? I researched here in SO and found that most of the answers are either JQuery or AJAX. I'd like to know how because I'm new in Javascript and PHP so I'd like to first have a thorough practice and understanding in Javascript before diving into JQuery and AJAX.

是否可以使用普通的Javascript将值从PHP传递到模式框?我在这里研究了一下,发现大多数答案都是JQuery或者AJAX。我想知道为什么,因为我是Javascript和PHP的新手,所以在深入了解JQuery和AJAX之前,我想先深入了解一下Javascript。

I'm working on a small project which has a modal box and a <table> with an Edit control.

我正在做一个小项目,它有一个模态框和一个带有编辑控件的

<div id="modalBox" class="w3-modal">
    <div class="w3-modal-content">
        <div class="w3-container">
            <span onclick="document.getElementById('modalBox').style.display='none'" class="w3-button w3-display-topright">&times;</span>
            <p>Some text. Some text. Some text.</p>
            <p>Some text. Some text. Some text.</p>
        </div>
    </div>
</div>

<div class="record-container">
    <table class="table-record">
        <tr>
            <th>Title</th>
            <th>Date Created</th>
            <th>Control</th>
        </tr>

        <?php
        $announcementDaoImpl = new AnnouncementDaoImpl($pdo);
        $announcementList = $announcementDaoImpl->getAllAnnouncementByMostRecent();
        foreach($announcementList as $key => $value): ?>
        <tr>
            <td><?php echo $key->getTitle(); ?></td>
            <td><?php echo $value->getDateAdded(); ?></td>
            <td>
                <a href="#" onclick="showEditModal('modalBox')">Edit</a>
            </td>
        </tr>
     <?php endforeach; ?>
    </table>
</div>

Let's say I want to fill the modal box with the value returned by $value->getDateAdded, is that possible without JQuery and AJAX?

假设我要用$value返回的值填充模态框——> getdateadd,没有JQuery和AJAX是否可能?

How can I go about it? Can you provide some ideas.

我该怎么做呢?你能给我一些建议吗?

Thank you.

谢谢你!

2 个解决方案

#1


1  

Please replace your code with these lines from below code.

 // Add id where you want to add this
id="subjectdiv" 

// On click add your php value

onclick="showEditModal('modalBox','<?php echo $value->getDateAdded(); ?>')"

function showEditModal(modalid,phpval) {

//You code here

//code for send value on modal box
var theDiv = document.getElementById("subjectdiv");
theDiv.innerHTML = phpval; 

}

Hope this can help.

#2


1  

Ajax IS plain JavaScript. But if you're just trying to echo something in PHP to the client to be run as JavaScript, you're looking for json_encode

Ajax是纯JavaScript。但是,如果您只是试图将PHP中的一些内容回显到要作为JavaScript运行的客户端,那么您正在寻找json_encode

#1


1  

Please replace your code with these lines from below code.

 // Add id where you want to add this
id="subjectdiv" 

// On click add your php value

onclick="showEditModal('modalBox','<?php echo $value->getDateAdded(); ?>')"

function showEditModal(modalid,phpval) {

//You code here

//code for send value on modal box
var theDiv = document.getElementById("subjectdiv");
theDiv.innerHTML = phpval; 

}

Hope this can help.

#2


1  

Ajax IS plain JavaScript. But if you're just trying to echo something in PHP to the client to be run as JavaScript, you're looking for json_encode

Ajax是纯JavaScript。但是,如果您只是试图将PHP中的一些内容回显到要作为JavaScript运行的客户端,那么您正在寻找json_encode