2015-09-30 89 views
0

使用Ajax和PHP將數據插入到MySQL中時,從rich textarea中獲取有限的數據,是否有任何問題?使用Ajax和PHP將數據插入到MySQL時,使用有限的數據?

jQuery腳本

$('#adddesc').click(function(e) 
    { 
     e.preventDefault(); 

    var txtcategoryname=tinyMCE.get('txtcategoryname').getContent(); 
    var txttitle=$('#txttitle').val(); 
    var selectError1=$("#selectError1").val(); 
    var selectError2=$("#selectError2").val(); 
    var selectError3=$("#selectError3").val(); 
    //var fimage=$("#fimage").val(); 
    //alert($("#selectError2").val().length); 
var dataString; 
var err; 
err=(txttitle!='' && txtcategoryname!='' && $("#selectError1").val()!='0' && $("#selectError2").val()!='0' && $("#selectError3").val()!='0')?'0':'1'; 
// var dataString1="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2; 
//dataString1="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2+"& subcat="+selectError3; 
// alert(dataString1); 

    if(err=='0') 
    { 
    dataString="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2+"& subcat="+selectError3; 
    //alert(dataString); 
       $.ajax({ 
           type: "POST", 
           url: "aAddDescription.php", 
           data: dataString, 
           cache: true, 
           beforeSend: function(){ $("#adddesc").val('Adding Des.....');}, 
      success: function(html){ 

    //$("#txtcategoryname").val(''); 
    tinyMCE.get('txtcategoryname').setContent(''); 
    $("#txttitle").val(''); 
     //$("#selectError1").get(0).selectedIndex = 0; 
     //$("#error").removeClass("alert alert-error"); 
    $("#error").addClass("alert alert-success"); 
$("#error").html("<span style='color:#cc0000'>Success:</span> Description Added Successfully. ").fadeIn().delay(3000).fadeOut(); 

      } 
      }); 

HTML表單腳本

     <form class="form-horizontal" method="POST" action="" enctype="multipart/form-data" autocomplete="off"> 
          <fieldset> 
           <div class="control-group"> 
           <label class="control-label" for="selectError">Select Cateogry</label> 
           <div class="controls"> 
                <?php 
$result=mysqli_query($db,"SELECT * FROM categories "); 
//$count=mysqli_num_rows($result); 
$op="<option value='0'>Select Category</option>"; 
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) 
{ 
$op.="<option value='".$row['id']."'>".$row['title']."</option>"; 
} 
?> 
            <select id="selectError1" > 
            <?php echo $op; ?> 
            </select> 
           </div> 
           </div> 


      <div class="control-group"> 
           <label class="control-label" for="selectError">Select Sub Cateogry</label> 
           <div class="controls"> 

            <select id="selectError3"> 
           <option selected="selected" value="0">--Select Sub--</option> 
            </select> 
           </div> 
           </div>     











           <div class="control-group"> 
              <label class="control-label" for="focusedInput">Enter Title:: </label> 
    <div class="controls">           
<input class="form-control" type="text" id="txttitle" name="txttitle" value="" placeholder="Enter Title"><span id="user-availability-status"></span> <img src="LoaderIcon.gif" id="loaderIcon" style="display:none;width:20px;height:20px;" /> 
             </div></div> 

              <div class="control-group"> 


    <textarea rows="10" cols="20" name="content" style="width:100%; height:150px" id="txtcategoryname"></textarea>          

             </div> 

             <div class="control-group"> 
           <label class="control-label" for="selectError1">Tags(select All with Press Ctrl)</label> 
           <div class="controls"> 
            <?php 
$result=mysqli_query($db,"SELECT * FROM tags "); 
//$count=mysqli_num_rows($result); 
$op1=''; 
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) 
{ 
$op1.="<option value='".$row['title']."'>".$row['title']."</option>"; 
} 
?> 
            <select id="selectError2" multiple > 
            <?php //echo $op1; ?> 
            </select> 
           </div> 
           </div> 









           <div class="form-actions"> 

           <button type="submit" class="btn btn-primary" id="adddesc">Save changes</button> 
           <button class="btn">Cancel</button> 
           </div> 
          </fieldset> 
         </form> 

aAddDescription.php

<?php 
include("common/db.php"); 
session_start(); 
if(isSet($_POST['description']) && isSet($_POST['txttitle'])) 
{ 
// username and password sent from Form 
$description=$_POST['description']; 
$txttitle=mysqli_real_escape_string($db,$_POST['txttitle']); 
$catname=mysqli_real_escape_string($db,$_POST['catname']); 
$subcat=mysqli_real_escape_string($db,$_POST['subcat']); 
$tags=mysqli_real_escape_string($db,$_POST['tags']); 
//$fimage=$_FILES['fimage']['name'] ; 
$cby=$_SESSION['login_user']; 
//$result=mysqli_query($db,"SELECT * FROM categories WHERE title='$categoryname'"); 
//$count=mysqli_num_rows($result); 
//$target_dir = "uploads/"; 
//$target_file = $target_dir.$_FILES['fimage']['name']; 
//move_uploaded_file($_FILES['fimage']['tmp_name'],$target_file); 


//$row=mysqli_fetch_array($result,MYSQLI_ASSOC); 
// If result matched $myusername and $mypassword, table row must be 1 row 
/*if($count>0) 
{ 
echo "0"; 
} 
else 
{ 
mysqli_query($db,"INSERT INTO categories(title) VALUES('".$categoryname."')"); 
echo "1"; 
}*/ 
//date_default_timezone_set('Asia/Delhi'); 

mysqli_query($db,"INSERT INTO description(title,description,cat_id,tags_id,created,modified,createdby,modifiedby,subcat_id) VALUES('".$txttitle."','".$description."','".$catname."','".$tags."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','".$cby."','".$cby."','".$subcat."')"); 
$cid=mysqli_insert_id($db); 
$cresult=mysqli_query($db,"SELECT * FROM counter WHERE cont_id='$cid'"); 
$ccount=mysqli_num_rows($cresult); 

if($ccount==0) 
{ 
mysqli_query($db,"INSERT INTO counter(cont_id) VALUES(".$cid.")"); 
} 
echo "1"; 
} 
?> 

請幫我任何事情錯在這個代碼?......即使我改變緩存爲虛假也得到同樣的問題。如果我輸入1000 /更少或更多的行數據,它總是採取有限的數據。請幫幫我。在此先感謝

+0

請檢查兩件事情:你應該encodeURIComponent()這樣的編碼文本 1.到底有多少個字符在MySQL中輸入 2.當您使用TinyMCE的,請谷歌的豐富TinyMCE的默認行爲文本。 – satroy

+0

是的,我使用TinyMCE – Arjun

+0

您嘗試在Alert框中彈出textarea值嗎?警報(txtcategoryname)。結果是什麼 –

回答

0

我願意打賭,你的文字中有字符與該網址干擾,如「&」,「?」,「/」,「=」等

var txtcategoryname=encodeURIComponent(tinyMCE.get('txtcategoryname').getContent()); 
var txttitle=encodeURIComponent($('#txttitle').val()); 
var selectError1=encodeURIComponent($("#selectError1").val()); 
var selectError2=encodeURIComponent($("#selectError2").val()); 
var selectError3=encodeURIComponent($("#selectError3").val()); 
+1

是的,你是對的。現在它工作得很好。非常感謝。 – Arjun

+0

@Arjun高興地幫忙,幾年前在類似的問題上,我在牆上打了3天的頭......這些課程中的一個,你只需要學習一次:) – DelightedD0D

相關問題