为什么我的提交按钮无法调用php函数?

时间:2019-09-04 14:12:17

标签: php dynamic-content

我有一个非常简单的php网站,并且正在尝试使其更加动态。以前,我要求用户单击链接以打开一个新页面,其中包含多个图像以及一些“提交”按钮。按下一个名为php文件的按钮,该按钮将一些信息保存在文本框中。一切正常。但是,我现在使内容更具动态性,以便用户从弹出菜单中选择一项来更新同一页面中的内容。但是,现在动态生成的提交按钮似乎不起作用。我一般对Web开发还很陌生,所以不确定为什么会这样。任何帮助,不胜感激。

这是index.php文件,它填充列表框并设置页面以动态管理内容。

'''php

<!doctype html>
<html>
    <head>
        <script>
        function showImages( imageDate ) {
            if ( imageDate == "") {
                document.getElementById("imageTable").innerHTML ="";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200 ) {
                        document.getElementById("imageTable").innerHTML = this.responseText;
                    }
                }
                xmlhttp.open("GET", "dispData.php?imageDate="+imageDate, true );
                xmlhttp.send();
            }
        }   
        </script>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Title</title>
        <meta name="language" content="en" />  
        <meta name="description" content="" />  
        <meta name="keywords" content="" /> 
    </head>
    <body>

    <?php

    $myDirectoryStr = "trialImages/";

    // open this directory 
    $myDirectory = opendir($myDirectoryStr);

    // get each entry
    while($entryName = readdir($myDirectory)) {
        $dirArray[] = $entryName;
    }

    // sort into date order
    sort( $dirArray );

    // close directory
    closedir($myDirectory);

    //  count elements in array
    $indexCount = count($dirArray);

    ?>      
        <?php
        // loop through the array of files and print them all in a list
        $cnt = 0;
        for($index=0; $index < $indexCount; $index++) {
            $extension = substr($dirArray[$index], -10);
            // start new row of table
            if ($extension == 'tempIm.png'){

                $dateStr[$cnt] = substr($dirArray[$index], 0, 8 );

                $cnt++;
            }
        }

        //  count elements in array
        $indexCount = count($dateStr);

        // find unique dates
        $dateStrUnique = array_values( array_unique( $dateStr, SORT_STRING ) );

        //  count elements in array
        $indexCount = count($dateStrUnique);

        echo '<form>';
        echo '<select name="dates" onchange="showImages(this.value)">';
        echo '<option value="">select date ...</option>';   

        for($index=0; $index < $indexCount; $index++) {
            echo '<option value="' . $dateStrUnique[$index]. '">' . $dateStrUnique[$index] . '</option>';   
        }

        echo '</select>';
        echo '</form>';

        ?>

        <br>
        <div id="imageTable"><b> image information will be displayed here ... </b></div>
</body>
</html>

'''

这是dispData.php文件,该文件生成用于更新div标签并包含提交按钮和图像的动态内容

'''php

<?php
    // recover some information
    $imageDateTarget = $_GET['imageDate'];

    // image directory
    $myDirectoryStr = "trialImages/";

    // open directory 
    $myDirectory = opendir($myDirectoryStr);

    // get each entry
    while($entryName = readdir($myDirectory)) {
        $dirArray[] = $entryName;
    }

    // sort into date order
    sort( $dirArray );

    // close directory
    closedir($myDirectory);

    //count elements in array
    $indexCount = count($dirArray);

    echo    '<table class="fixed">
            <col width="200px">
            <col width="200px">
            <col width="100px">';       

        // loop through the array of files and display them in table
        for($index=0; $index < $indexCount; $index++) {
            $extension = substr($dirArray[$index], -10);
            // start new row of table       
            if ($extension == 'tempIm.png'){ // input image place in first column

                // image date
                $imageDate = substr($dirArray[$index], 0, 8 );

                // if image date the same as selected date then display image
                if ( strcmp( $imageDateTarget, $imageDate ) == 0 ) {

                    echo '<tr>';
                    echo '<td>' . $dirArray[$index] . '</td>';
                    echo '</tr>';

                    echo '<tr>';
                    echo '<td height=400><img src="' . $myDirectoryStr . $dirArray[$index] . '" class="rotate90" " alt="Image" border=3 height=200 width=400></img></td>';

                    echo '<form action="writeNotes.php"  method="POST">';

                    //search for matching image notes if they exist and display
                    $indexImageNotes = array_search( $imageDate . 'notes.txt', $dirArray );
                    if ($indexImageNotes){
                        $notes = file_get_contents($myDirectoryStr . $imageDate . 'notes.txt');  // read contents into string
                        echo '<td><textarea name = "notes" rows = "26" cols="30">' . $notes . '</textarea></td>';
                    }
                    else {
                        echo '<td><textarea name = "notes" rows = "26" cols="30">add some comments here ...</textarea></td>';
                    }
                    echo '<td><input type = "submit"></input><input type = "hidden" name = "imageDate" value = "' . $myDirectoryStr . $imageDate . '"></input></form></td>';
                    echo '</tr>';
                }
            }   
        }
?>  

'''

这是writeNotes.php函数,用于保存文本注释,应在按下提交按钮时调用

'''php

<?php 
print_r("I am here");
$file = $_POST["imageDate"] . 'notes.txt';
$data = $_POST["notes"];
file_put_contents($file, $data);
?>

'''

部分问题是我没有收到任何错误消息,只是执行writeNotes.php函数失败

1 个答案:

答案 0 :(得分:0)

我认为在我先前的代码中肯定有一些错误,因为我尝试了进一步的简化版本,这似乎可行。我只是在下面填充一个弹出菜单

'''php

detected_chems=['chemical_strange_character$$','chemical___WeirdSPELLING','chem3','chem4','chem5','chem6','chem7','chem8','chem9','chem10','chem11','chem12','chem13','chem14','chem15','chem16','chem17','chem18','chem19','chem20']
chem_db = {'chem1':{'property1':'smells','property2':'poisonous'},'chem2':{'property1':'stinks','property2':'toxic'},'chem3':{'property1':'smells','property2':'poisonous'},'chem4':{'property1':'smells','property2':'poisonous'},'chem5':{'property1':'smells','property2':'poisonous'},'chem6':{'property1':'smells','property2':'poisonous'},'chem7':{'property1':'smells','property2':'poisonous'},'chem8':{'property1':'smells','property2':'poisonous'},'chem9':{'property1':'smells','property2':'poisonous'},'chem10':{'property1':'smells','property2':'poisonous'},'chem11':{'property1':'smells','property2':'poisonous'},'chem12':{'property1':'smells','property2':'poisonous'},'chem13':{'property1':'smells','property2':'poisonous'},'chem14':{'property1':'smells','property2':'poisonous'},'chem15':{'property1':'smells','property2':'poisonous'},'chem16':{'property1':'smells','property2':'poisonous'},'chem17':{'property1':'smells','property2':'poisonous'},'chem18':{'property1':'smells','property2':'poisonous'},'chem19':{'property1':'smells','property2':'poisonous'},'chem20':{'property1':'smells','property2':'poisonous'}}

not_matched = []

res = {det_chem:chem_db[det_chem]
       for det_chem in detected_chems
       if det_chem in chem_db or not_matched.append(det_chem)}

print(len(detected_chems))
print(len(res))
print(len(not_matched))

print(not_matched)

'''

并在此处返回动态内容

'''php

20
18
2
['chemical_strange_character$$', 'chemical___WeirdSPELLING']

'''

还有writeNotes.php函数

'''php

<!doctype html>
<html>
    <head>
        <script>
        function showImages( imageDate ) {
            if ( imageDate == "") {
                document.getElementById("imageTable").innerHTML ="";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200 ) {
                        document.getElementById("imageTable").innerHTML = this.responseText;
                    }
                }
                xmlhttp.open("GET", "dispData.php?imageDate="+imageDate, true );
                xmlhttp.send();
            }
        }   
        </script>
    </head>
    <body>
        <form>
            <select name="dates" onchange="showImages(this.value)">
                <option value="">select date ...</option>
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
        </form>

        <br>
        <div id="imageTable"><b> information will be displayed here ... </b></div>
</body>
</html>

'''

这似乎没有问题。很抱歉提出先前的问题,并再次感谢您的评论。