我正在编写一个允许人们将电影添加到数据库中的网站。
目前,用户可以从已经在数据库中的导演中进行选择,也可以创建一个新导演。
<div class='dropdownrow' id='director_namerow'>
<div>
<label for='director_name'>Director:</label>
<select name='director_name'>
<option value='blank' selected>Select...</option>
<?php
$sql = "SELECT *
FROM director
ORDER BY director_name ASC";
$director = mysql_query($sql);
while ($directors=mysql_fetch_array($director)) {
?>
<option value="<?php echo $directors['director_id']; ?>"><?php echo $directors['director_name']; ?></option>
<?php
}
?>
</select>
<span style = 'color:red;'> *</span>
</div>
<div>
<label for='director_namenew'>Or new director:</label>
<input type='text' name='director_namenew' size='25' maxlength='128' />
</div>
</div>
问题是,如何检查&#34; director_namenew&#34;并不等于&#34; director_name&#34;并且director_namenew已经不在数据库中。
此外,是&#34; director_namenew&#34;不在数据库中,我也需要将它们添加到数据库中。
控制器脚本
function validateDirector ($formdata) {
if(($formdata['director_name'] == "blank") && ($formdata['director_namenew'] == "")){
return false;
}
else if($formdata['hidden_director_name'] == $formdata['director_namenew']){
echo 'cannot have directors match';
return false;
}
else {
return true;
}
} // TO COMPLETE
谢谢你们, PB。
答案 0 :(得分:0)
您可以使用Ajax将前端需求传输到后端,以便分离视图和控制器。
view.html (根据原始代码进行一些修改):
<script src="checkRepeatName.js"></script>
<div>
<label for='director_namenew'>Or new director:</label>
<input type='text' name='director_namenew' size='25' maxlength='128' />
<span id="notation"></span>
</div>
checkRepeatName.js (用jQuery编写):
$("input[name='director_namenew']").change(function(){
$.get("backend.php?dnamenew=" + this.value,function(data,status){
$(".notation").text(data);
});
});
backend.php (我使用面向对象的PHP代码样式,并参考@Barmar的推荐):
<?php
if (isset($_GET['dnamenew'])) {
$director_namenew = $_GET['dnamenew'];
};
$dbconn = new mysqli(HOST, USERNAME, PASSWORD, DBNAME, PORT);
$sqlStat = sprintf('SELECT director_id FROM director WHERE director_name = "%s";', $director_namenew);
if ($sqlQuery = $dbconn->query($sqlStat)){
if ($sqlQuery->num_rows == 0){
echo 'This is validated database name';
} else {
echo 'Invalidated database name!';
}
}
$dbconn->close;
?>
--------补充一些东西-------- view.html :
<div class='dropdownrow' id='director_namerow'>
<div>
<label for='director_name'>Director:</label>
<select name='director_name'>
<option value='blank' selected>Select...</option>
</select>
<span style = 'color:red;'> *</span>
</div>
<div>
controllor.js :
$("select[name='director_name']").change(function(){
$.get("optionOutput.php", function(data,status){
$("select[name='director_name']").append(data); // append the call-back message from back-end file
});
});
optionOutput.php :
<?php
include 'dbmng/dbconfig.php';
$dbconn = new mysqli(HOST, USERNAME, PASSWORD, DBNAME, PORT);
$sqlStat = 'SELECT * FROM director ORDER BY director_name ASC;';
if ($sqlQue = $dbconn->query($sqlStat)){
while($sqlRes = $sqlQue->fetch_assoc()){
$output = sprintf('<option value = "%d">%s</option>', $sqlResult['director_id'], $sqlRes['director_name']);
echo $output;
}
}
$dbconn->close;
?>
答案 1 :(得分:0)
回答我自己的问题
if(($formdata['director_name'] == "blank") && ($formdata['director_namenew'] == "")){
print "<p>Please enter in director information - Use the back button on your browser to rectify this problem.</p>";
return false;
}
else if($formdata['director_namenew']) {
$doesntExist = true;
$db = getDBConnection();
$directors = $db->query("SELECT director_name FROM director");
//Check each one
foreach ($directors as $director){
//If the username is already in the DB stop looking
if($formdata['director_namenew'] == $director['director_name']){
$doesntExist = false;
print "<p>Director entered in new director already exists, please enter in new director or select from drop down menu - Use the back button on your browser to rectify this problem.</p>";
break;
}
}
//DB connection closed when PDO object isn't referenced
//ie setting $db to null closes the connection
$db = null;
return $doesntExist;
}
- 但是,我如何检查下拉和输入字段是否都已填充?
我在想这样的事情:
else if(($formdata['director_name']) && ($formdata['director_namenew'])) {
print "Please select either new director or director from drop down menu";
}
检查提交以查看是否已提交两个$ _POST变量。
您怎么看?