向mysql数据库发送多个参数

时间:2014-12-11 10:51:15

标签: php mysql autosave

此代码是在mysql数据库中自动保存表单 我无法发送多个参数

主要问题在这里

var saved_text = document.getElementById("saved_text").value;

        var content = document.getElementByID("test").value;

        var params = "saved_text="+saved_text+"content="+content;

php code

<?php
$user_id = 1;                                             
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    $saved_text = mysql_real_escape_string($_POST["saved_text"]); 
    $sql = "UPDATE asave SET saved_text = '".$saved_text."' ";    
    $sql.= "WHERE user_id = $user_id";                            

    mysql_query($sql) or die(mysql_error().$sql);           
    echo "Your data has been saved ".date("h:m:s A");   
    exit;                                                   
}
$sql = "SELECT saved_text FROM asave WHERE user_id = $user_id";
$rs = mysql_query($sql) or die(mysql_error().$sql);       
$arr = mysql_fetch_array($rs);
$saved_text = $arr["saved_text"];
?>

html代码

<html>
<head>
<script type="text/javascript">
    function init(){
        window.setInterval(autoSave,10000);                  // 10 seconds
    }
    function autoSave(){

        var saved_text = document.getElementById("saved_text").value;

        var content = document.getElementByID("test").value;

        var params = "saved_text="+saved_text+"content="+content;

        var http = getHTTPObject();
        http.onreadystatechange = function(){
            if(http.readyState==4 && http.status==200){
                msg = document.getElementById("msg");
                msg.innerHTML = "<span onclick='this.style.display=\"none\";'>"+http.responseText+" (<u>close</u>)</span>";
            }
        };
        http.open("POST", window.location.href, true);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.send(params);
    }

    //cross-browser xmlHTTP getter
    function getHTTPObject() { 
        var xmlhttp; 
        /*@cc_on 
        @if (@_jscript_version >= 5) 
            try { 
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
            } 
            catch (e) { 
                try { 
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
                } 
                catch (E) { 
                    xmlhttp = false; 
                } 
            } 
        @else 
            xmlhttp = false; 
        @end @*/  

        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
            try {   
                xmlhttp = new XMLHttpRequest(); 
            } catch (e) { 
                xmlhttp = false; 
            } 
        } 

        return xmlhttp;
    }
</script>
</head>
<body onload="init();">
    <span id="msg" style="cursor:pointer;"></span>
    <form method="POST">
        <textarea id="saved_text" name="saved_text" rows="10" cols="100"><?PHP echo $saved_text;?></textarea>
        <br/>
<input id="test" type="text" value="<?php echo $content;?>">
        <input type="submit" value="save now" />
    </form>
</body>

1 个答案:

答案 0 :(得分:0)

您没有在javascript中发布saved_text。您正在发布params(缺少分隔符)。

要发布多个参数,请查看此帖子:Posting parameters to a url using the POST method without using a form