0

I am trying to insert values from an input field into a database with ajax as part of a conversation system.I am using an input form as follows.

<input data-statusid="' .$statuscommentid. '" id="reply_'.$statusreplyid.'" class="inputReply" placeholder="Write a comment..."/>

with the following jquery I carry out a function when the enter key is pressed by the user.

  $(document).ready(function(){ 
$('.inputReply').keyup(function (e) {
    if (e.keyCode === 13) {
       replyToStatus($(this).attr('data-statusid'), '1',$(this).attr("id"));
    }
  });

  }); 

within this function is where I am having the problem ,I have no problems calling the function with jquery but I have done something wrong with the ajax and I don't know what?

$.ajax({ type: "POST", url: $(location).attr('href');, data: dataString, cache: false, success: function(){ $('#'+ta).val(""); } });

Additionally this is the php I am using to insert into the database

    <?php //status reply input/insert
//action=status_reply&osid="+osid+"&user="+user+"&data="+data
if (isset($_POST['action']) && $_POST['action'] == "status_reply"){
    // Make sure data is not empty
    if(strlen(trim($_POST['data'])) < 1){
        mysqli_close($db_conx);
        echo "data_empty";
        exit();
    }
    // Clean the posted variables
    $osid = preg_replace('#[^0-9]#', '', $_POST['sid']);
    $account_name = preg_replace('#[^a-z0-9]#i', '', $_POST['user']);
    $data = htmlentities($_POST['data']);
    $data = mysqli_real_escape_string($db_conx, $data);
    // Make sure account name exists (the profile being posted on)
    $sql = "SELECT COUNT(userid) FROM user WHERE userid='$userid' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    if($row[0] < 1){
        mysqli_close($db_conx);
        echo "$account_no_exist";
        exit();
    }
    // Insert the status reply post into the database now
    $sql = "INSERT INTO conversation(osid, userid, postuserid, type, pagetext, postdate)
            VALUES('$osid','$userid','$postuserid','b','$pagetext',now())";
    $query = mysqli_query($db_conx, $sql);
    $id = mysqli_insert_id($db_conx);
    // Insert notifications for everybody in the conversation except this author
    $sql = "SELECT authorid FROM conversation WHERE osid='$osid' AND postuserid!='$log_username' GROUP BY postuserid";///change log_username
    $query = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $participant = $row["postuserid"];
        $app = "Status Reply";
        $note = $log_username.' commented here:<br /><a href="user.php?u='.$account_name.'#status_'.$osid.'">Click here to view the conversation</a>';
        mysqli_query($db_conx, "INSERT INTO notifications(username, initiator, app, note, date_time) 
                     VALUES('$participant','$log_username','$app','$note',now())");
    }
    mysqli_close($db_conx);
    echo "reply_ok|$id";
    exit();
}

?>

Thanks in advance for any help it will be much appreciated

James
  • 1,260
  • 13
  • 25
  • What kind of problems are you experiencing, does the PHP get called at all? – Johannes Jander Jan 10 '16 at 13:47
  • If you're already using jQuery, why don't you switch over to using $.ajax()? It's simple to use. Also, you can use jquery selectors as well. http://api.jquery.com/jquery.ajax/ You should also use PDO instead of mysqli: http://php.net/manual/en/book.pdo.php and http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 – Jacques ジャック Jan 10 '16 at 13:50
  • the php does not get called.... i think it might have something to do with the "url:" attribute – James Jan 10 '16 at 15:18

2 Answers2

1

Why didn't you set the proper URL for Ajax calls instead of using location.href?

var ajax = ajaxObj("POST", location.href);

In additional, I guess ajaxObj is not defined or well coded. You are using, jQuery, why don't you try jQuery ajax?

http://api.jquery.com/jquery.ajax/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mathew B.
  • 199
  • 8
0
var ajax = ajaxObj("POST", location.href);
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            var datArray = ajax.responseText.split("|");
            if(datArray[0] == "reply_ok"){
                var rid = datArray[1];
                data = data.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g,"<br />").replace(/\r/g,"<br />");
                _("status_"+sid).innerHTML += '<div id="reply_'+rid+'" class="reply_boxes"><div><b>Reply by you just now:</b><span id="srdb_'+rid+'"><a href="#" onclick="return false;" onmousedown="deleteReply(\''+rid+'\',\'reply_'+rid+'\');" title="DELETE THIS COMMENT">remove</a></span><br />'+data+'</div></div>';
                _("replyBtn_"+sid).disabled = false;
                _(ta).value = "";
                alert("reply ok!");
            } else {
                alert(ajax.responseText);
            }
ajax.send("action=status_reply_ok&sid="+sid+"&user="+user+"&data="+data);
        }
    }