2012-01-03 115 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