0

this code is to autosave form in mysql database i am unable to send multiple parameters

the main issue is here

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 code

<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>

Arvind
  • 25
  • 6
  • possible duplicate of [Posting parameters to a url using the POST method without using a form](http://stackoverflow.com/questions/1244308/posting-parameters-to-a-url-using-the-post-method-without-using-a-form) – idstam Dec 12 '14 at 19:18

1 Answers1

0

You're not posting saved_text in your javascript. You're posting params (that is missing a delimiter).

To post multiple parameters check this post: Posting parameters to a url using the POST method without using a form

Community
  • 1
  • 1
idstam
  • 2,848
  • 1
  • 21
  • 30