javascript
  • php
  • mysql
  • ajax
  • mysqli
  • 2015-05-24 59 views 0 likes 
    0

    對於一些奇怪的原因,這行代碼是不工作:新變量ajaxObj不起作用

    var ajax = ajaxObj("POST", "php_parsers/status_system.php"); 
    

    什麼可能呢? 我覺得它必須是使用window.alert之後的那一行,因爲那行window.alert沒有運行。

    全碼:

    的函數被調用:

    $status_ui = '<textarea id="statustext" onkeyup="statusMax(this,250)" placeholder="What&#39;s new with you '.$u.'?"></textarea>'; 
    $status_ui .= '<button id="statusBtn" onclick="postToStatus(\'status_post\',\'a\',\''.$u.'\',\'statustext\')">Post</button>'; 
    

    功能:

    function postToStatus(action,type,user,ta){ 
        window.alert("status passed 1"); 
        var data = _(ta).value; 
        if(data == ""){ 
         alert("Type something first weenis"); 
         return false; 
        } 
        window.alert("status passed 2"); 
        _("statusBtn").disabled = true; 
        var ajax = ajaxObj("POST", "php_parsers/newsfeed_system.php"); 
        window.alert("status passed 3"); 
        ajax.onreadystatechange = function() { 
         if(ajaxReturn(ajax) == true) { 
          var datArray = ajax.responseText.split("|"); 
          if(datArray[0] == "post_ok"){ 
           var sid = datArray[1]; 
           data = data.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g,"<br />").replace(/\r/g,"<br />"); 
           var currentHTML = _("statusarea").innerHTML; 
           _("statusarea").innerHTML = '<div id="status_'+sid+'" class="status_boxes"><div><b>Posted by you just now:</b> <span id="sdb_'+sid+'"><a href="#" onclick="return false;" onmousedown="deleteStatus(\''+sid+'\',\'status_'+sid+'\');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a></span><br />'+data+'</div></div><textarea id="replytext_'+sid+'" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_'+sid+'" onclick="replyToStatus('+sid+',\'<?php echo $u; ?>\',\'replytext_'+sid+'\',this)">Reply</button>'+currentHTML; 
           _("statusBtn").disabled = false; 
           _(ta).value = ""; 
          } else { 
           alert(ajax.responseText); 
          } 
         } 
        } 
        ajax.send("action="+action+"&type="+type+"&user="+user+"&data="+data); 
        window.alert("status passed 4"); 
    } 
    

    newsfeed_system.php

    if (isset($_POST['action']) && $_POST['action'] == "status_post"){ 
        // Make sure post data is not empty 
        if(strlen($_POST['data']) < 1){ 
         mysqli_close($db_conx); 
         echo "data_empty"; 
         exit(); 
        } 
        // Make sure type is a 
        if($_POST['type'] != "a"){ 
         mysqli_close($db_conx); 
         echo "type_unknown"; 
         exit(); 
        } 
        // Clean all of the $_POST vars that will interact with the database 
        $type = preg_replace('#[^a-z]#', '', $_POST['type']); 
        $data = htmlentities($_POST['data']); 
        $data = mysqli_real_escape_string($db_conx, $data); 
    
        // Insert the status post into the database now 
        $sql = "INSERT INTO newsfeed(author, type, data, postdate) 
          VALUES('$log_username','$type','$data',now())"; 
        $query = mysqli_query($db_conx, $sql); 
        $id = mysqli_insert_id($db_conx); 
        mysqli_query($db_conx, "UPDATE newsfeed SET osid='$id' WHERE id='$id' LIMIT 1"); 
    
        mysqli_close($db_conx); 
        echo "post_ok|$id"; 
        exit(); 
    } 
    

    的Ajax方法:

    function ajaxObj(meth, url) { 
        var x = new XMLHttpRequest(); 
        x.open(meth, url, true); 
        x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        return x; 
    } 
    function ajaxReturn(x){ 
        if(x.readyState == 4 && x.status == 200){ 
         return true;  
        } 
    } 
    

    請幫忙!

    +1

    什麼是'ajaxObj'?它是否是js庫的一部分?如果是這樣,你應該列出它,以便我們知道你在做什麼。 –

    +0

    @PatrickEvans編輯後顯示ajax方法... – MasudM

    +1

    檢查您的javascript控制檯的錯誤 –

    回答

    0

    ajax沒有被引用!您需要包含庫或將代碼調用爲'ajaxObj'。

    相關問題