2012-01-03 127 views
2

一直在尋找幾個小時如何獲得這種排序,我正處於哭泣的邊緣,所以計算它的時間來獲得一些幫助。這應該很容易,但我明顯錯過了一些東西。使用PHP/jQuery獲取textarea的價值

我想從文本區域內獲取文本,然後使用textarea文本執行MySQL更新。我使用PHP和jQuery。

通常,我會在php中使用$ _post,但是,我正在使用jQuery來顯示模式彈出窗口。我想運行MySQL更新代碼,然後模態彈出窗口會顯示一些文字說「已保存」。簡單。

所有的代碼工作正常,除了我無法獲得文本。顯然這很關鍵。

那麼如何從textarea中獲取文本?

表:

<form name = "editProfile"> 
<textarea rows="2" cols="20" name="bio" id="bio"> 
Some text. 
</textarea> 
</form> 

PHP代碼

<?php 
$bio = TEXT FROM TEXTAREA; 

$sql="Update members SET bio = '$bio' WHERE memberID='$memberID'"; 
$result=mysql_query($sql); 
echo ("Profile Update Sucesful"); 
?> 

這bio.text位是你將怎樣做它在asp.net ....

上面的表格並沒有使用方法/動作,實際上可能不需要「表格」標籤,因爲我只需要textarea。該模式是通過鏈接啓動的。

請幫忙(請耐心等待我)。如果您需要更多信息,請告訴我。別喊。我是新來的。

+0

當通過使用jQuery的阿賈克斯,你可以指定它是POST或一個GET。你可以請張貼你的ajax代碼嗎? – Derek 2012-01-03 13:52:44

+0

你如何將數據傳輸到PHP腳本?也包括你的JavaScript代碼。 – 2012-01-03 13:53:13

+0

你如何發送textarea的值?你在用阿賈克斯嗎?這是什麼代碼?另外,最後一行代碼沒有意義,因爲它總是回顯「配置文件更新成功」。更好的選擇是使用'if($ result){echo「Profile update successful」; } else {echo mysql_error(); ''如有錯誤。 – 2012-01-03 13:53:58

回答

0
$bio = $_GET['bio']; /* or $_POST if your send data with jQuery $.POST */ 

是你需要加載textarea的

注意,你應該在你的形式無論如何指定發送方法,應用安全起見的價值是什麼,發送某種隱藏的令牌針對CSRF請求和任何數據操作(如數據庫插入)之前,你應該認真清理每輸入您收到

+0

你的意思是$ _POST ['bio'],因爲他發佈了它 – max4ever 2012-01-03 13:54:20

+0

他沒有在form元素中指定方法,get是默認方法:http://stackoverflow.com/questions/2314401/what-is- -default-form-posting-method並且沒有提供javascript代碼。 – fcalderan 2012-01-03 13:57:13

+1

儘管使用GET來表示一個帶有textarea的表單是一個非常糟糕的主意。 – ThiefMaster 2012-01-03 14:01:17

0

讓您形式的AJAX調用:

<!-- #dialog is the id of a DIV defined in the code below --> 
<a href="#dialog" name="modal">Simple Modal Window</a> 

<div id="boxes"> 

    <!-- #customize your modal window here --> 
    <div id="dialog" class="window"> 
     <textarea rows="2" cols="20" name="bio" id="bio"> 
      Some text. 
     </textarea> 
     <input id="saveTxt" type="submit" value="Save" /> 

     <!-- close button is defined as close class --> 
     <a href="#" class="close">Close it</a> 

    </div> 

    <!-- Do not remove div#mask, because you'll need it to fill the whole screen --> 
    <div id="mask"></div> 
</div> 

隨着你的Javascript:

<script> 

$(document).ready(function() { 

    //select all the a tag with name equal to modal 
    $('a[name=modal]').click(function(e) { 
     //Cancel the link behavior 
     e.preventDefault(); 
     //Get the A tag 
     var id = $(this).attr('href'); 

     //Get the screen height and width 
     var maskHeight = $(document).height(); 
     var maskWidth = $(window).width(); 

     //Set height and width to mask to fill up the whole screen 
     $('#mask').css({'width':maskWidth,'height':maskHeight}); 

     //transition effect  
     $('#mask').fadeIn(1000);  
     $('#mask').fadeTo("slow",0.8); 

     //Get the window height and width 
     var winH = $(window).height(); 
     var winW = $(window).width(); 

     //Set the popup window to center 
     $(id).css('top', winH/2-$(id).height()/2); 
     $(id).css('left', winW/2-$(id).width()/2); 

     //transition effect 
     $(id).fadeIn(2000); 

    }); 

    //if close button is clicked 
    $('.window .close').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 
     $('#mask, .window').hide(); 
    });  

    //if mask is clicked 
    $('#mask').click(function() { 
     $(this).hide(); 
     $('.window').hide(); 
    }); 

    //if submit is clicked 
    $('input#saveTxt').click(function(event) { 
     event.preventDefault(); 

     //Get the text from your textarea 
     var text = $('textarea#bio').val(); 

     //Post it to your PHP page to insert it into your database 
     $.ajax({ 
      url: "yourpage.php",     //The URL you are posting to 
      type: "POST",       //The way you post your data 
      dataType: "html",      //The type of data you expect to get back 
      data: {text : text},     //Your data (thus the value of your textarea) 
      cache: false, 
      success: function(html) {    //After the AJAX call was done, update the textarea with the HTML you got back 
      $('textarea#bio').val(html); 
      } 
     }); 
    });  

}); 

</script> 

yourpage.php(這是你可以發佈您的數據發送到網頁),那麼你可以簡單地把它拿來:

<?php 
    $bio = mysql_real_escape_string($_POST['text']); 

    //do your query 
    $sql = "Update members SET bio = '$bio' WHERE memberID='$memberID'"; 
    $result = mysql_query($sql); 
    echo ("Profile Update Sucesful"); 
?> 
+0

正在使用這個:http://www.queness.com/post/77/simple-jquery -modal-window-tutorial – Will 2012-01-03 16:07:28

+0

因此,把我的表單放在'

...
'內,並在$(document).ready(function()中加入我的$('input [type =「submit」]' ){...}然後,簡單地將ajax調用爲我發佈到** yourpage.php **,其中包含您在上面發佈的PHP代碼,然後在我的答案中告訴您,獲取'$ bio'。更新我的答案,現在基本上可以複製粘貼。 – Jules 2012-01-03 19:22:24

2

要想從文本jQuery中的textarea的我總是使用以下命令:

<textarea id="textareaOfInterest">some value</textarea> 
<script> 
    $('#textareaOfInterest').val(); 
</script> 

你的代碼的其餘部分應該是這個樣子:

<textarea id="textareaOfInterest">some value</textarea> 
<input type='button' id="doStuff" value="do" /> 
<script> 
    $('#doStuff').live('click',function(){ 
     show modal window 
     var val = $('#textareaOfInterest').val(); 
     $.ajax({ 
     .... 
     data: "&textArea="+val, 
     success: function(result) 
       { 
      ... do stuff with the result.... 

       hide modal window 
       } 
     }); 

}); 
</script> 

用jquery ajax搜索示例,因爲我不太瞭解語法。

希望它能幫助, 問候, 亞歷

+0

正在使用這個:http://www.queness.com/post/77/simple-jquery-modal-window-tutorial – Will 2012-01-03 16:07:36