根据下拉列表php中的选择填写表单

时间:2015-12-08 03:20:22

标签: php mysql

我对这个研究做了很多研究但没有成功。所以我现在要做的是:

  1. 根据数据库中的表填充下拉列表(已完成)
  2. 从数据库生成的下拉列表中选择一个值后,我希望允许用户通过自动填充当前的内容来编辑记录,并允许他们更改存储在数据库中的内容,或者只需单击按钮即可删除记录。 (未完成)
  3. 数据库对象是一个具有以下属性的相册

    +-------------+-------------+------+-----+---------+----------------+
    | Field       | Type        | Null | Key | Default | Extra          |
    +-------------+-------------+------+-----+---------+----------------+
    | AlbumID     | int(11)     | NO   | PRI | NULL    | auto_increment |
    | AlbumName   | varchar(75) | YES  |     | NULL    |                |
    | Label       | varchar(75) | YES  |     | NULL    |                |
    | Genre       | varchar(30) | YES  |     | NULL    |                |
    | ReleaseDate | date        | YES  |     | NULL    |                |
    | Artist      | varchar(75) | YES  |     | NULL    |                |
    +-------------+-------------+------+-----+---------+----------------+
    

    我目前正在尝试使用的HTML是:

    <!DOCTYPE HTML>
    <html>
    <head>
        <title>MyMuzik Library</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="content/site.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/mask.js"></script>
        <script type="text/javascript">
            jQuery(function($){
                $("#txtReleaseDate").mask("9999/99/99",{placeholder:"yyyy/mm/dd"});
            });
        </script>
    </head>
    <body>
    <form action="insertAlbum.php" method="post">
        <div class="nav">
            <ul>
                <li><a href="fphome.php">Home</a></li>
                <li><a href="fpsongs.php">Songs</a></li>
                <li><a href="#">Albums</a></li>
            </ul>
        </div>
        <div class="content-container">
        <form action="insertAlbum.php" method="post" autocomplete="off">
            <h1>Albums</h1>
            <h3>This is where you add new albums</h3>
            <div class="form-group">
            <label for="ddlAlbumSelect" id="lblAlbumSelect">Select Album to Edit:</label>
            <div class="form-control">
                <select name="ddlAlbumSelect" id="ddlAlbumSelect">
                    <option value="0"> Select Album...</option>
                    <?php
                        mysql_connect('localhost', 'root', '1978afc247a');
                        mysql_select_db('finalproject');
                        $sql = mysql_query("SELECT * FROM albums ORDER BY AlbumName");
                        while ($row = mysql_fetch_array($sql)){
                        echo "<option value='" . $row['AlbumID'] . "'>" . $row['AlbumName'] . "</option>";
                        }
                    ?>
                </select>
            </div>
            </div>
            <div class="form-group">
            <label for="txtAlbumName" id="lblAlbumName">Album Name:</label>
            <div class="form-control">
                <input type="text" <?php if(ddlAlbumSelect.Value != 0) echo "<p>Selected a value!</p>" ?>  name="txtAlbumName" id="txtAlbumName" />
            </div>
            </div>
            <div class="form-group">
            <label for="txtArtist" id="lblArtist">Artist:</label>
            <div class="form-control">
                <input type="text" name="txtArtist" id="txtArtist" />
            </div>
            </div>
            <div class="form-group">
            <label for="txtLabel" id="lblLabel">Label:</label>
            <div class="form-control">
                <input type="text" name="txtLabel" id="txtLabel" />
            </div>
            </div>
            <div class="form-group">
            <label for="txtGenre" id="lblGenre">Genre:</label>
            <div class="form-control">
                <input type="text" name="txtGenre" id="txtGenre" />
            </div>
            </div>
            <div class="form-group">
            <label for="txtReleaseDate" id="lblReleaseDate">Release Date:</label>
            <div class="form-control">
                <input type="text" name="txtReleaseDate" id="txtReleaseDate" />
            </div>
            </div>
            <input style="margin-left:30px" type="submit" />
    </form>
    <div class="clearfix"></div>
    <br />
        </div>
        <footer>
            <p>MyMuzik Library&copy; <?php echo date("Y"); ?>
        </footer>
    </body>
    </html>
    

    引用的表单操作文件insertAlbum.php如下所示:

    <html>
    <body>
    
    
    <?php
    $con = mysql_connect("localhost","root","1978afc247a");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("finalproject", $con);
    
    $sql="INSERT INTO albums (AlbumName, Label, Genre, ReleaseDate, Artist)
    VALUES
    ('$_POST[txtAlbumName]','$_POST[txtLabel]', '$_POST[txtGenre]','$_POST[txtReleaseDate]','$_POST[txtArtist]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added <br/>";
    
    mysql_close($con)
    ?>
    <a href="<?php echo $_SERVER['HTTP_REFERER']; ?>"><< Back</a>
    </body>
    </html>
    

    我尝试过使用AJAX,但一直没有成功,只是朝着正确的方向寻找机会!感谢。

1 个答案:

答案 0 :(得分:0)

您至少应该提供Ajax次努力。无论如何,下面是我的答案。我已将我的解释放在评论表中。

基于SELECTED ALBUM的广告文字字段:

$(document).on("change", "#ddlAlbumSelect", function(){ /* WHEN AN ALBUM HAS BEEN SELECTED */

  var albumid = $(this).val(); /* GET THE ID OF THE SELECTED ALBUM */

  $.ajax({ /* START OF AJAX */
    type: "POST", /* METHOD TO USE TO PASS THE DATA */
    url: "getalbum.php", /* THE FILE WHERE WE WILL PROCESS THE PASSED DATA */
    data: {"albumid": albumid}, /* DATA TO BE PASSED */
    dataType: "json", /* TYPE OF DATA THAT WILL BE RETURNED FROM getalbum.php */
    success: function(result){ /* GET RETURNED DATA FROM getalbum.php */
      /* BELOW WILL POPULATE THE CORRESPONDING TEXT FIELDS IN YOUR FORM */
      $("#txtAlbumName").val(result.albumName);
      $("#txtArtist").val(result.artist);
      $("#txtLabel").val(result.label);
      $("#txtGenre").val(result.genre);
      $("#txtReleaseDate").val(result.releaseDate);
    } /* END OF GETTING THE RETURNED DATA FROM getalbum.php */
  }); /* END OF AJAX */

});

然后是getalbum.php

/* ESTABLISH CONNECTION */
$con = new mysqli("localhost", "root", "1978afc247a", "finalproject");

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $con->prepare("SELECT txtAlbumName, txtLabel, txtGenre, txtReleaseDate, txtArtist FROM albums WHERE AlbumID = ?");
$stmt->bind_param("i", $_POST["albumid"]);
$stmt->execute();
$stmt->bind_result($albumname, $label, $genre, $releasedate, $artist);
$stmt->fetch();
$stmt->close();

echo json_encode(array("albumName" => $albumname, "label" => $label, "genre" => $genre, "releaseDate" => $releasedate, "artist" => $artist"));

不要使用deprecated mysql_* API并使用mysqli_*PDO

更新专辑:

我以为你正在尝试更新所选专辑?为什么您的表单会转到insertAlbum.php