0

How to get value of a text box in a dialog box using jquery in php. I am opening a dialog box on click of button in which there is a text box as comments. I want this value to submit to the database. But the value i get is undefined or blank when i alert it. I am unable to get the message value of the textbox. My Code is as:

<script type="text/javascript" language="javascript"> $(document).ready(function(){ $(".QTPopup").css('display','none'); $(".lnchPopop").click(function(){ $(".QTPopup").animate({width: 'show'}, 'slow');}); $(".closeBtn").click(function(){ $(".QTPopup").css('display', 'none'); }); $(document).on('click','.submit_comment',function(){ alert("Hello"); var comments=$('#comment_by_user').val(); alert(comments); var comments1= document.getElementById("comment_by_user").value; alert(comments1); }); }); </script> 

The Dialog box opens on the click of the Comment Hyperlink.

a href="#" class="lnchPopop">Comments</a> <div class="QTPopup" style="display: none"> <div class="QTPopupCntnr"> <div class="gpBdrLeftTop"></div> <div class="gpBdrRightTop"></div> <div class="gpBdrTop"></div> <div class="gpBdrLeft"> <div class="gpBdrRight"> <div class="caption"> Send Your Messages </div> <a href="#" class="closeBtn" title="Close"></a> <div class="content"> <br /> <table> <tr> <td style="height:5px;"></td> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td> <textarea class="textareagradiant" id="comment_by_user" name="comment_by_user" style="width:428px; height:116px; border:1px solid #CFCECE;"> </textarea> </td> </tr> <tr> <td style="height:5px;"></td> </tr> <tr> <td style="height:10px;"></td> </tr> <tr> <td> <input type="button" value="Submit" class="gbtn_s submit_comment" /> <input type="button" value="Reset" class="gbtn_s" /> </td> </tr> </table> </div> </div> </div> <div class="gpBdrLeftBottom"></div> <div class="gpBdrRightBottom"></div> <div class="gpBdrBottom"></div </div> </div> 
5
  • 1
    too many codes. please post codes only relative to the problem Commented Jul 4, 2014 at 7:03
  • just check your console errors if any and tells us the error...@Aaron Commented Jul 4, 2014 at 7:06
  • @SmartKiller There are no errors in the console log. Commented Jul 4, 2014 at 7:10
  • just add any class in textbox and try to get value using class like $('.your_class').val(); @Aaron17a Commented Jul 4, 2014 at 7:16
  • @SmartKiller I have tried it adding class and get the value but still it doesnt work. Commented Jul 4, 2014 at 7:28

2 Answers 2

2

remove the alert("Hello");

$(document).ready(function(){ $(".QTPopup").css('display','none'); $(".lnchPopop").click(function(){ $(".QTPopup").animate({width: 'show'}, 'slow');}); $(".closeBtn").click(function(){ $(".QTPopup").css('display', 'none'); }); $(document).on('click','.submit_comment',function(){ var comments=$('#comment_by_user').val(); alert(comments); var comments1= document.getElementById("comment_by_user").value; alert(comments1); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Your code seems to be working fine. Make sure you correct the first line

<a href="#" class="lnchPopop">Comments</a>

A working copy can be found here: http://jsfiddle.net/6LXSn/1/.

To get the value to php I usualy use a jQuery AJAX call.

$.ajax({ type: "POST", url: 'your php script', data: { comments: comments }, dataType: 'html', async: false, error: function(obj, text, error) { }, success: function(response) { } }); 

then retrieve your comments value in php as

$_POST['comments'] 

hope this helps

2 Comments

Still the same problem, Can there be a problem of jquery version i am using, which version should i use.
Try getting the value as you can see in my example var comments=$('#comment_by_user').val();. I tried with older versions of jQuery and seems to be working fine with 1.7.2 and later.