douweng7308 2009-02-10 07:50
浏览 50
已采纳

jQuery forms.js每页有多个表单

I would like to submit information to a mySql database using php and ajax.

The page that the info is being sent from (form.php) has multiple forms that are generated from a "while()" loop.

On success, I would a response to update a div above the particular form from which the data was submitted.

I am currently using jQuery and the jquery form plugin.

I have been successful in getting the data to the database, however I am having trouble with the response being sent back to the proper div. I have been successful in getting a response back to a div that is outside of the while() loop. I have not, however, been successful in getting a response back to a div within the loop. I have placed in the code below a div called: "> Where I would like the note to be placed.

I know that this has everything to do with my javascript function:

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('form').ajaxForm({
        target: '#noteReturn',
        success: function() { $('#noteReturn').fadeIn('slow'); }
    });
});
</script>

The #noteReturn function does not specify which businesses div it should be placed in.

I hope that this makes sense.

Thank you for your help.

The code is below:

<!-- 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"; }
?>
  • 写回答

1条回答 默认 最新

  • duanhui9840 2009-02-10 08:08
    关注

    Change this code:

    jQuery('form').ajaxForm({ 
        target: '#noteReturn',
        success: function() { 
            $('#noteReturn').fadeIn('slow');
        } 
    });
    

    To this:

    jQuery('form').ajaxForm({ 
        target: '#noteReturn',
        dataType: 'json',
        success: function(data) { 
            $('#noteReturn' + data.id).html(data.note).fadeIn('slow');
        } 
    });
    

    And this code:

    <?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";
    }
    ?>
    

    To this:

    <?php
    $note = mysql_real_escape_string($_POST['note']);
    $id = mysql_real_escape_string($_POST['bid']);
    $sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')";
    $result = mysql_query( $sql );
    if($result) {
        print json_encode(array("id" => $id, "note" => $note));
    }
    ?>
    

    What happened?

    The change to the PHP code is making use of PHP's json_encode function to print out the id of the business to which the note was added as well as the actual note text. In the javascript code, I added the dataType of 'json' to tell the script what format of response to expect. Once the request is received in the success callback, the data variable is an object with the values we passed through json_encode. So data.id has the business id and data.note has the new note. Using jQuery's html() manipulation function, the inner html of the div is updated to the latest note. The div selector uses the id we passed, so we can update the corresponding div.

    Also, this is slightly off topic, but make sure you always use mysql_real_escape_string when putting values into a query like you are. If you do not use this, your queries will be vulnerable and susceptible to injection attacks, and they are not pretty. If a customer decided to enter a note value of ');DROP TABLE businesses; you'd really feel the pain. Preferably, switch to PDO or MySQLi and use prepared statements, as they are the 'correct' way of doing queries nowadays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启