Benim ajax veri her yazı ile boş bir yazı göndererek

0 Cevap php

*EDIT***this problem is solved by OP, see response to question I have an ajax function that sends a user string back to server in json format using POST method. Its working fine, the problem is it sends an empty post after each data post. Here is my function:

function ADDLISITEM(form)
{ 
var options = form.txtInput.value;
options = JSON.stringify(options);
//var url = "conn_sql.php?options=" + options;
var url = "conn_mysql.php"
var request = null;
request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
    if (request.readyState == 4) {
            if (request.status == 200) {
                alert('POST');
        } else {
            alert(request.status); // fail 
        }
    }
}
 request.send("options=" + encodeURIComponent(options).replace(/%20/g, '+'));
}

bu ben php POST veri almak nasıl:

<?php  
      $json = $_POST['options'];
      $options = json_decode($json);
      $username = "user";  
      $password = "********";  
      $hostname = "localhost";  
      $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect   
      to MySQL");  
      $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");

      $query = "INSERT INTO user_spec (options) VALUES ('$options')";
      mysql_query($query);

      $query1 = "SELECT * FROM user_spec";  
      $result=mysql_query($query1);  

      $outArray = array(); 
        if ($result) { 
               while ($row = mysql_fetch_assoc($result)) $outArray[] = $row; 
                   } 
               echo json_encode($outArray);
?

Bu benim php ajax tarafından gönderilen, veri çıkışı olduğunu.

{"options":"09876"},{"options":""},{"options":"qq"},{"options":""},{"options":"43444"},{"options":""}]  

0 Cevap