【问题标题】:Get value of textarea using PHP/jQuery使用 PHP/jQuery 获取 textarea 的值
【发布时间】:2012-01-03 13:48:57
【问题描述】:

找了好几个小时如何解决这个问题,我快哭了,所以想是时候寻求帮助了。这应该很容易,但我显然遗漏了一些东西。

我想从 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 中的操作方式......

上面的表单没有使用方法/动作,实际上可能不需要“表单”标签,因为我只需要文本区域。模态是通过链接启动的。

请帮助(请耐心等待我)。如果您需要更多信息,请告诉我。不要大喊大叫。我是新手。

【问题讨论】:

  • 通过 jQuery 使用 ajax 时,您可以指定它是 POST 还是 GET。你能发布你的ajax代码吗?
  • 如何将数据传输到 PHP 脚本?包括你的 JavaScript 代码。
  • 你是如何发送 textarea 的值的?你在用ajax吗?那个代码是什么?另外,最后一行代码没有意义,因为它总是会回显“配置文件更新成功”。如果出现错误,更好的选择是使用if($result) { echo "Profile update successful"; } else { echo mysql_error(); }
  • 还有很大的sql注入漏洞:)
  • 那么,如何在 jQuery 中创建模态窗口。如果我们不知道这一点,我们将无法帮助您。如果窗口的内容是通过 AJAX 加载的,那么使用 modal_content.php?bio=text 等传递的参数加载 php 文件是没有问题的……文本很容易用 jQuery 检索……

标签: php jquery html textarea modal-dialog


【解决方案1】:

要从 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 搜索示例,因为我不知道语法。

希望对你有帮助, 问候, 亚历克斯

【讨论】:

【解决方案2】:
 $bio = $_GET['bio']; /* or $_POST if your send data with jQuery $.POST */

是你需要加载textarea的值

请注意,无论如何,您都应该在表单中指定一个发送方法,并且为了应用程序的安全性,针对 CSRF 请求发送某种隐藏令牌,并且在任何数据操作(如 db 插入)之前,您应该认真清理每个您收到的输入

【讨论】:

【解决方案3】:

从您的表单进行 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");
?>

【讨论】:

  • 所以将我的表单放入&lt;div id="dialog" class="window"&gt;...&lt;/div&gt; 并将我的$('input[type="submit"]').click... 放入$(document).ready(function() { ... }。然后像我发布到 yourpage.php 那样简单地进行 ajax 调用,其中包含您在上面发布的 PHP 代码,并在那里获取 $bio,正如我在回答中告诉您的那样。我更新了我的答案,现在基本上可以复制粘贴了。
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 2013-03-22
  • 2023-03-09
  • 2011-11-05
  • 1970-01-01
  • 2018-01-12
  • 2011-12-15
  • 1970-01-01
相关资源
最近更新 更多