数据库查询,用于根据可更改的选择设置输入元素的值

时间:2014-05-01 14:49:32

标签: php html mysql

所以我有一个用于编辑数据库中现有文章的表单。它看起来像这样:

<div id="a_EditDiv">
    <h2 id="H-a_Edit">Edit an existing article</h2>
    <form id="F-a_New" action="editarticle.php" method="post" onsubmit="a_Editvalidate();">
        <fieldset>
            <legend>Edit an existing article: </legend>
            <select>
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
            <label for="a_Title_Edit">Article title: </label>
            <br/>
            <input type="text" name="a_Title_Edit" id="a_Title_Edit" value="" />
            <span id="err_a_Title_Edit"></span>
            <br/>
            <label for="a_Details_Edit">Article Details </label>
            <br/>
            <textarea rows="30" cols="" name="a_Details_Edit" id="a_Details_Edit"></textarea>
            <br/>
            <div id="a_Details-rmn_Edit">Remaining characters: 8192/8192</div>
            <span id="err_a_Details_Edit"></span>
            <div>
                <pre>
                    <code>
                        You may use the following html tags: 
                        &lt;a href=""&gt;&lt;/a&gt;
                        &lt;b&gt;&lt;/b&gt;   &lt;i&gt;&lt;/i&gt;   &lt;u&gt;&lt;/u&gt;
                        &lt;object&gt;&lt;/object&gt;
                        Please use:
                        &amp;lt; = &lt;   &amp;gt; = &gt;
                        &amp;euro = &euro;   &amp;dollar; = &dollar;   &amp;pound; = &pound;
                        &amp;amp; = &amp;
                        &amp;copy; = &copy;   &amp;reg; = &reg;
                        Please DO NOT use any other HTML tags.
                    </code>
                </pre>
            </div>
            <label for="a_Author_Edit">Author: </label>
            <br/>
            <input type="text" value="admin" name="a_Author_Edit" id="a_Author_Edit" readonly="readonly" />
            <span id="err_a_Author_Edit">    </span>
            <br/>
            <input class="gray_button" type="button" value="Edit Author" />
            <br/>
            <label for="a_Timestamp">Timestamp: </label>
            <br/>
            <input type="text" value="<?php echo date('d-m-Y') . ' ' . strtoupper(date('h:i:sa'));?>" name="a_Timestamp_Edit" id="a_Timestamp_Edit" readonly="readonly" />
            <br/>
            <input class="gray_button" type="button" value="Edit Timestamp" id="a_Timestamp-enable" />
            <br/><br/>
            <input class="gray_button" type="submit" value="Edit Article" />
            <br/>
            <span id="err_a_Edit"></span>
            <?php 
                if(isset($_GET["article"])){
                    if($_GET["article"] === "success"){
                        echo "Article successfully published.";
                    } else if($_GET["article"] === "failed"){
                        echo "Article was not published.";
                    }
                }
            ?>
        </fieldset>
    </form>
</div>

是的......大代码。因此,当我更改<select>元素的值时,我希望输入字段根据<select>元素的值填充文章的标题,详细信息等。 所以我需要查询数据库以检索与<select>的值相对应的值,然后将<input>元素的值设置为相应的值。我可以轻松地UPDATE articles WHERE ID = value of select id,但我不想这样做,我想要一种互动的方式来做到这一点,通过这样做,编辑可以看到帖子已经是什么样的。

1 个答案:

答案 0 :(得分:0)

经过长时间的研究后,我找到了答案。在W3Schools上有一个PHP,AJAX和MySQL教程,它给了我完美的解决方案。它很容易找不到。

链接:http://www.w3schools.com/php/php_ajax_database.asp