首页 > 解决方案 > 为什么我的提交按钮调用php函数失败?

问题描述

我有一个非常简单的 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 函数

标签: phpdynamic-content

解决方案


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

'''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>

'''

并在此处返回动态内容

'''php

<?php
    echo '<form action="writeNotes.php"  method="POST">
          <td><input type = "submit"></input><input type = "hidden" name = "imageDate" value = "a_value"></input></form></td>';
?>

'''

还有 writeNotes.php 函数

'''php

<?php 
$file = $_POST["imageDate"];
print_r($file);
//header("Location: index.php"); //note there must be no output before using header
?>

'''

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


推荐阅读