I have A VERY OLD SCRIPT that I need to modify. You will notice from the code it is using mysql_query() which is outdataed however that is not my issue.
I have a text link in a file named surveycommentslist.php. The link opens a jquery model window that I need to capture a user inputed text in and then save it to a mysql along with the value of the links data-id which tells me which unique user ID to connect the comment to. My issue is I am not able to get the value of data-id using the code I have below.
Here is my code
<html> <head> <!-- common scripts --> <!-- jQuery library --> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.js"></script> <!-- jQuery migrate --> <script src="js/jquery-migrate-1.2.1.min.js"></script> <!-- bootstrap framework plugins --> <script src="bootstrap/js/bootstrap.min.js"></script> </head> <body> <script> $(document).on('click', '.reply', function() { //get acommentuid var val = $(this).attr('data-id'); $.post('surveycommentslist.php', {acommentuid: val}, function(data) { console.log(data); }); }); </script> <?php //lets list comments $scommentsql = mysql_query("SELECT * FROM survey_comments WHERE surveyid=$surveyid ORDER BY commentdate asc"); while($scrows = mysql_fetch_array($scommentsql)) { extract($scrows); $the_comment = stripslashes($the_comment); $commentdate = date("m/d/Y h:ia", strtotime($commentdate)); if($touid > 0) { $indent = "margin-left:35px"; } //get name of person making the comment $nsql = mysql_query("SELECT fname, lname, userlevel, id AS scommentid FROM users WHERE id=$uid LIMIT 1"); $namerow = mysql_fetch_array($nsql); $commenters_fname = stripslashes($namerow['fname']); $commenters_lname = stripslashes($namerow['lname']); if($namerow['userlevel'] > 19) { $adminicon = "<img src='./img/admin_smicon.png' alt='admin'>"; } echo "<div class='ch-message-item clearfix' style='$indent'> <div class='ch-content'> $the_comment </div> <p class='ch-name'> <strong>$adminicon $commenters_fname $commenters_lname</strong> <span class='muted'>$commentdate</span>"; if($touid == 0) { echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>"; } echo "</p> </div>"; }
5 Answers
Answers 1
$(document).on('click', '.reply', function(e) { e.preventDefault(); //get acommentuid var val = $(this).data('id'); $.post('surveycommentslist.php', {acommentuid: val}, function(data) { console.log(data); }); });
Answers 2
When I copy-paste your code in a file & execute that in a browser, it shows the "data-id" attribute as blank: http://screencast.com/t/pasXI14dp0x . That is due to the incorrect HTML pointed out in earlier messages.
Even after the corrections, there is still the 'btn-small' text which is not in any attribute.
The incorrect HTML is:
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
The correct HTML should be:
echo " <a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
Another alternative you could try is hardcode some value in the "data-id" attribute & find out if it's being passed. If that works, then the issue is with the HTML & should be fixed with above corrected HTML code.
Answers 3
Your HTML markup is wrong
You can check that by doing a view source
Change the following code
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
with
echo "<a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
Answers 4
Class attribute is not closed properly. Quotes issue.
Change this
echo " <a href='#switch_modal' class='reply btn btn-default id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
to
echo "<a href='#switch_modal' class='reply btn btn-default' id='$scommentid' btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>";
Answers 5
Maybe there's just an issue with the single and double quote in the REPLY
. You said $scommentid
has a value, so I assume there's just an echo problem. Try these:
//codes . . //RE-POSITION class values from id value echo "<a href='#switch_modal' class='reply btn btn-default btn-small' id='$scommentid' data-toggle='modal' data-id='$scommentid'>REPLY</a>"; //or by concatenation if I am wrong with the above solution echo "<a href='#switch_modal' class='reply btn btn-default id=".'"$scommentid"'." btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>"; //or by using the " if I am wrong with the above solutions echo "<a href='#switch_modal' class='reply btn btn-default id="$scommentid" btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>"; . . //codes
0 comments:
Post a Comment