将发布在选定选项上的数据库内容放入具有单个字段的表单中
Posted
技术标签:
【中文标题】将发布在选定选项上的数据库内容放入具有单个字段的表单中【英文标题】:Get the database content posted on a selected option into a form with single fields 【发布时间】:2013-01-25 16:04:27 【问题描述】:我有一个带有选择的表单。选项来自数据库并显示在选择中。
数据库上的列是:
id, date, title, body.
在选择时,我只显示日期和标题。在同一页面中,我有一个表单:
date -> input text
title -> input text
body -> text area
我希望当用户单击选择中的一个选项时,下面的字段将使用包含数据库数据的选定选项中保留的内容进行更新。
现在我不知道如何获取以下字段中的数据。 我的想法是获取下拉选项的索引。这可能吗?
编辑完整代码:
<html>
<body>
<div class="normal">
<table style="width:100%;height:100%;" border="1">
<tr>
<td ><strong>geste n</strong></td>
<td valign="middle" >
<input type="radio" name="n" id="new" value="nuova" onchange="shown(this.id);" <?php if($_POST['n'] == "nuova")echo "CHECKED"; ?> />Nuova n
<input type="radio" name="n" id="mod" value="modifica" onchange="shown(this.id);" <?php if($_POST['n'] == "modifica")echo "CHECKED"; ?> />Modifica n
<select id="sceltannascosta" style="display:none">
<?php
include("/login/flock/sql.php");
$conn = mysqli_connect($host, $user, $pw, $db) or die("Impossibile connettersi");
$a = "SELECT * FROM n ORDER BY id DESC";
$b = mysqli_query($conn, $a);
print '<option></option>';
while ($c = $b->fetch_object())
print '<option>'.$c->id.$c->data.$c->title.$c->body.'</option>';
$b->close();
$conn->close;
?>
</select>
<select id="sceltan" style="display:none" onChange="showEntry(this,this.value);setndamod(this);">
<?php
include("/login/flock/sql.php");
$conn = mysqli_connect($host, $user, $pw, $db) or die("Impossibile connettersi");
$a = "SELECT * FROM n ORDER BY id DESC";
$b = mysqli_query($conn, $a);
print '<option></option>';
while ($c = $b->fetch_object())
print '<option>'.$c->data.' - '.$c->title.'</option>';
$b->close();
$conn->close;
?>
</select>
<input type="submit" id="botenascostogeste" class="button">
</form>
</td>
</tr>
</table>
</div>
<p> </p>
<div class="normal" id="c" style="display:none">
<table style="width:100%;height:100%;">
<tr>
<td colspan="3" border="0">
<strong class="confirm">c nuova n</strong>
</td>
</tr>
<tr>
<td align="right">
<strong>Data</strong>
</td>
<td colspan="2">
<form name="nuovan" method="post" action="cht.php?c=yes">
<input name="datan" type="text" maxlength="10" size="8" value="<?php echo date(d.".".m.".".Y) ?>"> gg.mm.aaaa
</td>
</tr>
<tr>
<td align="right">
<strong>Titolo</strong>
<td >
<input name="titolon" type="text" maxlength="50" size="50" value="<? echo htmlspecialchars("titolo n - lunghezza massima 50 caratteri") ?>" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')">
</td>
<td >
<b><span id=myCounter>6</span></b> caratteri rimanenti per il titolo</font>
</td>
</tr>
<tr>
<td colspan="3">
<textarea name="teston"><?php if(isset($_POST['teston']))echo $_POST['teston'];elseecho 'Testo n con stile personalizzato'; ?></textarea>
<script>
CKEDITOR.replace('teston');
</script>
</td>
</tr>
<tr>
<td align="right">
<input type="submit" name="c" class="button" onClick="document.getElementById('botenascostogeste').click();">
</form>
</td>
<td colspan="2">
<?php
$c = $_GET['c'];
$titolon = $_POST['titolon'];
$teston = $_POST['teston'];
$datascritta = $_POST['datan'];
if($c == 'yes')
if($_POST['titolon'] == NULL || $_POST['teston'] == NULL || $_POST['datan'] == NULL)
echo '<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">';
else
function verificadata($datascritta)
list($dd,$mm,$yyyy)=explode(".",$datascritta);
if (is_numeric($dd) && is_numeric($mm) && is_numeric($yyyy))
return checkdate($mm,$dd,$yyyy);
return false;
if(verificadata($datascritta))
$connessione = mysql_connect ($host, $user, $pw) or //connect to the database
die('<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">');
mysql_select_db ($db); //select the database
mysql_query("INSERT INTO n(data, title, body) VALUES ('$datascritta', '$titolon', '$teston')");
mysql_close($connessione);
echo ('<img src="'.$imagey.'" style=width:"' . $width . 'px;height:' . $height . 'px;"> Ok'); // It worked.
else die('<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">');
?>
</td>
</table>
</div>
<div class="normal" id="modifica" style="display:none">
<table style="width:100%;height:100%;">
<tr>
<td colspan="3" border="0">
<strong class="confirm">Modifica n</strong>
</td>
</tr>
<tr>
<td align="right">
<strong>Data</strong>
</td>
<td colspan="2">
<form name="modifican" method="post" action="cht.php?modifica=yes">
<input name="idmodifican" type="text" style="display:none" value="INSERIRE ID DI MODIFICA DEL DATABASE QUI">
<input name="datanmodificata" type="text" maxlength="10" size="8" value="POST DATE FROM THE n TO MODIFY SELECTED"> gg.mm.aaaa
</td>
</tr>
<tr>
<td align="right">
<strong>Titolo</strong>
</td>
<td >
<input name="modificatitolo" type="text" maxlength="50" size="50" value="POST TITLE FROM THE n TO MODIFY SELECTED" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')">
</td>
<td >
<b><span id=myCounter>50</span></b> caratteri rimanenti per il titolo</font>
</td>
</tr>
<tr>
<td colspan="3">
<textarea name="modificatesto">POST BODY FROM THE n TO MODIFY SELECTED</textarea>
<script>
CKEDITOR.replace('modificatesto');
</script>
</td>
</tr>
<tr>
<td align="right">
<input type="submit" name="modifica" class="button">
</form>
</td>
<td colspan="2">
PHP I HAVE TO DO TO UPDATE THE n ON THE DATABASE (I'm able to do this part).
</td>
</table>
</div>
</body>
【问题讨论】:
好主意! What have you tried? 您对这是如何完成的有疑问吗?因为这听起来更像是您要求某人为您构建这个系统,这不是这个网站的目的。 当父select
索引发生变化时,使用 jQuery/Js (ajax) 填充 select
选项。
我添加了一些我尝试过但没有成功的文本...
你能提供更多的代码细节,以便我可以尝试:)
【参考方案1】:
为此,您需要进行 ajax 调用。 然后使用jquery根据选择字段中选择的值设置字段,您可以设置其他字段的值尝试根据需要修改它
<body>
<form>
<select id="sel_opt">
<?php
$i = 1;
foreach($valueformdb as $value)
echo "<option class=\"option\" id=\"opt_$i\">";
echo $value;
echo '</option>';
$i++;
?>
</select>
<input type="text" id="txt_date" value="" />
<input type="text" id="txt_title" value="" />
<textarea id="ta_body"></textarea>
</form>
<script type="text/javascript">
$(document).ready(function()
$('.sel_opt').on('change', function()
var id = $(this).attr('id');
$.ajax(
type:'POST',
dataType: 'json',
url:" ",// path of file to handle ajax request
data:'option='+id, // this will be posted on the page which handle the ajax and give back the data from db
success:function(data)
$('#txt_date').val(data[date]);// ajax respond the array containg the value its advised to use json
$('#txt_title').val(data[title]);
);
);
);
</script>
</body>
这不是您需要修改的确切代码,因为我不知道您要实现的确切目标
【讨论】:
以上是关于将发布在选定选项上的数据库内容放入具有单个字段的表单中的主要内容,如果未能解决你的问题,请参考以下文章