dongxiatuo9827 2012-07-16 20:37
浏览 88
已采纳

进行AJAX调用以更新数据库onChange of textarea

I am trying to create a feature on my website that automatically updates the database onchange of the textarea below (which is going to be act as a 'post-it note for reminders'). I am new to ajax, and I was wondering if someone can show me a basic example of how I would make an AJAX call to update my database onChange of the textarea below?

<?php
//Create mysql connect variable
$conn = mysql_connect('samplesource.com', 'example', 'pass');

//kill connection if error occurs
if(!$conn){
    die('Error: Unable to connect.' . '<br>' . mysql_error());
}
//connect to mysql database
mysql_select_db("mydb", $conn);

session_start();
$userid = $_SESSION['id'];

$results = ("SELECT * FROM notes WHERE userid='$userid'");
?>

<html>
<head>
<title>practice</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {     
    $(".sometext").change(function(){   
        //make ajax call to update database onChange    
    }); 
}); 
</script>
</head>
<body>

<textarea class="note" style="resize:none; width:300px; height:200px;"> </textarea>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongyihang3575 2012-07-16 20:44
    关注

    First, you'd need to move your database save script into a new file e.g save.php

    On your <textarea> i'd add

    <textarea onchange="saveChanges(this);"></textarea>
    

    For the javascript save function that's called when a change is made:

    function saveChanges(object){   
        $.ajax({
            url: 'save.php',
            data: 'content=' + object.value,
            cache: false,
            error: function(e){
                alert(e);
            },
            success: function(response){
                // A response to say if it's updated or not
                alert(response);
            }
        });   
    }
    

    This is a very quick and dirty way of doing it.

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

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入