基于php中的动态下拉框填充文本框

时间:2015-04-12 23:20:26

标签: javascript php jquery

我正在尝试在选择项目时从数据库输入值(Rating)。我还需要能够更新该评级,然后将该值放入另一个表中。 我的问题是,我正在试图弄清楚如何使文本框中的值与下拉列表中的选择相关。我发现编码可以制作另一个选项框,但无法弄清楚如何制作一个文本框。

回答:最初当我发布问题时,我认为可以在没有JQuery的情况下执行此操作并尝试复制for循环并运行while循环来填充文本框。我现在知道这是不可能的,并想知道如何运行脚本来填充下拉框的更改文本框。问题是该功能必须连接到数据库以及下拉列表。 1.拉下下拉数据(从JOURNAL表中)。 2.从该下拉列表中选择一个日记3.在JOURNAL表中,JournalRating已分配给每个日记帐,并将该值放入文本框中。

我试图做的是

<?php
    include 'dbc.php';
    connect();

    //insert form values into database                                                                                                                                                   
$sql = "SELECT JournalName, JournalID, Rating, JournalActive from JOURNAL where JournalActive = 1;";      
//Can take out JournalActive if we do not want it                                                                                                                        
$result = mysqli_query($conn, $sql);                                                                                                                                                 
if (!$result) {                                                                                                                                                                      
    $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
    $message .= 'Whole query: ' . $query;                                                                                                                                            
    die($message);                                                                                                                                                                   
    echo "there was an issue";                                                                                                                                                       
} 

$sql2 = "SELECT FName, LName, FacultyID from FACULTY where FacultyActive = 1;"; 
//Can take out JournalActive if we do not want it                                                                                                                        
$result2 = mysqli_query($conn, $sql2);                                                                                                                                                 
if (!$result2) {                                                                                                                                                                      
    $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
    $message .= 'Whole query: ' . $query;                                                                                                                                            
    die($message);                                                                                                                                                                   
    echo "there was an issue";                                                                                                                                                       
} 

    //array to hold all of the data                                                                                                                                                      
$journals = array();                                                                                                                                                                  
    //print out all of the first names in the database   
$rownumber = 0;                                                                                                                                 
while ($row = mysqli_fetch_assoc($result)) {                                                                                                                                         
    $journals[$rownumber][0] = $row['JournalName'];
    $journals[$rownumber][1] = $row['JournalID'];
    $journals[$rownumber][2] = $row['JournalRating'];
    $journals[$rownumber][3] = $row['JournalActive'];
    $rownumber++;                                                                                                                                                       
}

$faculty = array();                                                                                                                                                                  
    //print out all of the first names in the database   
$rownum = 0;                                                                                                                                 
while ($row = mysqli_fetch_assoc($result2)) {                                                                                                                                         
    $faculty[$rownum][0] = $row['FName'];
    $faculty[$rownum][1] = $row['LName'];
    $faculty[$rownum][2] = $row['FacultyID'];
    $rownum++;                                                                                                                                                       
}

?>

<!DOCTYPE html>

<head>
<link href="styles.css" rel="stylesheet">
<h1> Miami University </h1>
<h4> Information Systems and Analytics Department </h4>
<script>
(function($){
    $(function(){ 
        $("#JournalID").on('change', function() {
              $("#JournalRating").val($(this).find("option:selected").data('Rating'));
        });
    }); 
})(jQuery);
</script>

</head>
<body>

<div class="StyleDiv" > 
<!-- coding for journal -->
<form id="form1" name="form1" method="post" action="RR2.php">


<label for="FacultyID">Faculty Name</label>
<select multiple="multiple" name="FacultyID[]" id="FacultyID">
<?php
    for($i = 0; $i < sizeof($faculty); $i++) {
    print "<option value=\"" . $faculty[$i][2] . "\">" . $faculty[$i][0] .' '. $faculty[$i][1] . "</option>\r\n";
    }
?>
</select>


<br class="clear" />

<br class="clear" /> 
<label for="JournalID">Journal Name</label>
<select name="JournalID" id="JournalID">
<?php

    for($i = 0; $i < sizeof($journals); $i++) {
    print "<option value=\"" . $journals[$i][1] . "\" data-rating=\"" . $journals[$i][2] . "\">" . $journals[$i][0] . "</option>\r\n";
}
?>
</select>
<br class="clear"/>

<label for="JournalRating">Journal Rating</label><input type="text" name="JournalRating" id="JournalRating" />

<br class="clear" /> 


<!-- coding for publication --> 
<label for="Title">Publication Title</label><input type="text" name="PubID" id="PubID" />
<br class="clear" /> 
<label for="Year">Year</label><input type="text" name="Year" id="Year" />
<br class="clear" /> 
<label for="Volume">Volume</label><input type="text" name="Volume" id="Volume" />
<br class="clear" /> 
<label for="Issue">Issue</label><input type="text" name="Issue" id="Issue" />
<br class="clear" /> 
<label for="Comments">Comments</label><textarea name="Comments" id="Comments" cols="45" rows="5"></textarea>
<br class="clear" /> 



<input type="submit" name="Submit" id="Submit" value="Submit" />
<br class="clear" /> 
</br>
</br>

</div>
</form>

<?php

//Post Parameters 
$JournalID = $_POST['JournalID'];
//for($i = 0; $i < sizeof($journals); $i++) {
    //if ($JournalID = $journals[$i][1]) {
    //$JournalName = $journals[$i][0];
    //}
    //}
$Year = $_POST['Year'];  
$Comments = $_POST['Comments'];  
$Volume = $_POST['Volume'];  
$Issue = $_POST['Issue'];  
$Title = $_POST['Title'];
$JournalRating = $_POST['JournalRating'];
$FacultyMemID = $_POST['FacultyID'];


//Query 



 //INSERT 
$stmt = $conn->prepare(" INSERT INTO PUBLICATION ( JournalID, Year, Comments, Volume, Issue, Title, JournalRating )  VALUES ( ?, ?, ?, ?, ?, ?, ? )"); 
$stmt->bind_param('sssssss', $JournalID, $Year, $Comments, $Volume, $Issue, $Title, $JournalRating);
$stmt->execute(); 

$pubID = $stmt->insert_id;
$facmemid = 0; 

$stmt = $conn->prepare(" INSERT INTO FACULTYPUBLICATIONS ( FacultyID, PubID )  VALUES ( ?, ? )"); 
$stmt->bind_param('ii', $facmemid, $pubID);
 //for ($_POST['FacultyID'] as $FacultyMemID) {
 for($i = 0; $i < sizeof($FacultyMemID); $i++) {
    $facmemid = $FacultyMemID[$i]; 
    $stmt->execute();
    }

mysqli_close($conn);
?>



</body>
</html>

1 个答案:

答案 0 :(得分:-1)

使用数据属性将评分添加到HTML中:

<select name="JournalID" id="JournalID">
<?php
    for($i = 0; $i < sizeof($journals); $i++) {
    print "<option value=\"" . $journals[$i][1] . "\" data-rating=\"" . $journals[$i][2] . "\">" . $journals[$i][0] . "</option>\r\n";
}
?>
</select>

然后你可以使用jQuery .data()

来访问它

&#13;
&#13;
(function($) {
  $(function() {
    $("#JournalID").on('change', function() {
      $("#JournalRating").val($(this).find("option:selected").data('rating'));
    });
  });
})(jQuery);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="JournalID">
  <option>Select a journal</option>
  <option value="1" data-rating="3">Journal #1</option>
  <option value="2" data-rating="2">Journal #2</option>
  <option value="3" data-rating="5">Journal #3</option>
  </select>
<br>
Rating: <input id="JournalRating">
&#13;
&#13;
&#13;