【发布时间】:2010-10-06 14:15:22
【问题描述】:
我想使用 php 和 ajax 向 mySql 数据库提交信息。
从 (form.php) 发送信息的页面有多个从“while()”循环生成的表单。
如果成功,我会响应更新提交数据的特定表单上方的 div。
我目前正在使用 jQuery 和 jquery form plugin。
我已成功将数据输入数据库,但是我无法将响应发送回正确的 div。我已经成功地获得了对 while() 循环之外的 div 的响应。但是,我还没有成功地在循环中获得对 div 的响应。我已将代码放在名为 div 的下面: "> 我想把笔记放在哪里。
我知道这与我的 javascript 函数有关:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('form').ajaxForm({
target: '#noteReturn',
success: function() { $('#noteReturn').fadeIn('slow'); }
});
});
</script>
#noteReturn 函数并没有指定它应该放在哪个业务 div 中。
我希望这是有道理的。
感谢您的帮助。
代码如下:
<!-- the form.php page -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/forms.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('form').ajaxForm({
target: '#noteReturn',
success: function() {
$('#noteReturn').fadeIn('slow'); }
});
});
</script>
<?php
$query = mysql_query("SELECT * FROM businesses");
while( $row = mysql_fetch_assoc( $query ) ):
$b_id = $row['bid'];
?>
<div class='notes'>
<?php
// query the db for notes associated with business... return notes texts and notes dates
$notesQuery = mysql_query("SELECT business_id, notes, messageDate FROM notes WHERE notes.business_id = $b_id ORDER BY messageDate");
while( $NoteRow = mysql_fetch_assoc( $notesQuery ) ) {
extract($NoteRow);
echo "$notes<br/><span class='noteDate'>$messageDate</span><br />";
} // end while$notesQuery
?>
<!-- this is where i would like jQuery to return the new note -->
<div id="noteReturn<?php echo $b_id; ?>"></div>
<!-- begin note form -->
<form name="noteForm" action="notesProcess.php" method="post">
<input type="text" name="note" />
<input type="hidden" value="<?php echo $b_id ?>" name="bid" />
<input type="submit" class="button" value="Send" />
</form>
</div> <!-- end div.notes -->
<?php
endwhile;
?>
<!-- /////////////////////////////////////////////////////
The page that the form submits to is this (notesProcess.php):
///////////////////////////////////////////////////// -->
<?php
$note = $_POST['note'];
$id = $_POST['bid'];
$sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')";
$result = mysql_query( $sql );
if( $result ) {
echo " $note"; }
?>
【问题讨论】:
-
为您修正了格式。缩进代码 4 个空格,这样它就不会被处理。
标签: php javascript jquery mysql ajax