2014-10-07 97 views
0

我插入MySQL中的textarea的內容和我獲取與PHP。 提交表格havnt問題,我可以插入新的數據。 我有不同的div和TextArea這就是爲什麼我不能使用輸入提交。 我使用Ajax,但比以前阿賈克斯負載process.php單擊這就是爲什麼獲得價值較早,插入舊數據。 我怎麼能在阿賈克斯加載process.php點擊或在無論如何textarea的與點擊進入新文本之後獲得的變量。 謝謝阿賈克斯負載PHP

<script type="text/javascript"> 
function useradd() 
{ 
    var data=$("#user,#user2,#user3,#user4").serialize(); 
    $.ajax({ 
     type: "POST", 
     url: "process.php", 
     data: data, 
     dataType: "html", 
    }); 
} 
</script> 

process.php

<?php 
    include'db.php'; 
    $text1=$_POST['text1']; 
    $text2=$_POST['text2']; 
    $text3=$_POST['text3']; 
    $text4=$_POST['text4']; 
    $n=new db(); 
    $n->connect(); 
    $n->delete(); 
    $n->insert($text1,$text2,$text3,$text4); 
?> 

插入

public function insert($text1,$text2,$text3,$text4) 
    {  
     $sql=mysql_query("REPLACE INTO strak(text1,text2,text3,text4) VALUES('$text1','$text2','$text3','$text4')"); 
     if(!$sql) 
     { 
      echo mysql_error(); 
     } 
    } 

獲取代碼的textarea

<textarea id="t1" name="text1" style="height:100%;"> 
<?php 
$conn=mysql_connect("localhost","fach","hektar12","p-d"); 
if($conn) 
{ $seldb=mysql_select_db("p-d",$conn); 
    if($seldb) 
    { $retrive=mysql_query("select text1 from strak",$conn); 
     if($retrive) 
     { $result=mysql_fetch_row($retrive); 

       echo ($result[0]); 

     } 
     } 
    } 
    mysql_close($conn); 
?> 
           </textarea> 
+0

你有多少種方法要問這個問題? – RiggsFolly 2014-10-07 10:15:40

+0

爲什麼不能使用簡單的提交按鈕? – RiggsFolly 2014-10-07 10:16:08

+0

是什麼導致你的javascript'useradd()'函數運行? – RiggsFolly 2014-10-07 10:17:01

回答

0

我認爲這將解決你的問題,如果你的Ajax開玩笑在獲取 數據的值之前,ction會調用您的PHP。並添加一個按鈕,其中包含ajax調用的函數的「onclick」事件。

在這裏你可以添加,「異步:假」到你的Ajax功能

function useradd() 
{ 
    var data=$("#user,#user2,#user3,#user4").serialize(); 
    $.ajax({ 
     type: "POST", 
     url: "process.php", 
     async: false, // add this line ,it will make process synchronous 
     data: data, 
     dataType: "html", 
    }); 
} 
+0

在process.php – 2014-10-07 10:55:04

+0

沒有我最提出了新的價值不同的div ??????????????? – user3662273 2014-10-07 12:23:54

+0

問題提出了新的價值,我想異步但dosent工作,謝謝 – 2014-10-07 14:27:37