Jump to content

Recommended Posts

I have a comment section. There is three fields: name, email, and comment. You can then hit post. The value that is typed into those html fields are stored in the following variables below: theCom, theName, theMail. The ajax is then run with those values and comment is posted. 

 

I also want to make the option to login. This way a your username and email can be pulled from the database of users and negating the need for the user to type thier name and email every time they want to comment. Using an if-statement to detect if a user is logged in, how can I then replace the value of the javascript varibles with the contents of the php variables. 

 

Assuming the user name of a registered user will be stored in $username and their email will be in $useremail. Thanks all!

       $('.bt-add-com').click(function(){
            var theCom = $('.the-new-com');
            var theName = $('#name-com');
            var theMail = $('#mail-com');

			
            if( !theCom.val()){ 
                $('.the-new-com').css('border' , '1px solid red'); 
            }else{ 
            	$('.the-new-com').css('border' , '#d3d7dc 1px solid');
                $.ajax({
                    type: "POST",
                    url: "ajax/add-comment.php",
                    data: 'act=add-com&id_post='+<?php echo $id_post; ?>+'&name='+theName.val()+'&email='+theMail.val()+'&comment='+theCom.val(),
                    success: function(html){
                        theCom.val('');
                        theMail.val('');
                        theName.val('');
                        $('.new-com-cnt').hide('fast', function(){
                            $('.new-com-bt').show('fast');
                            $('.new-com-bt').before(html);  
                        })
                    }  
                });
            }
        });
Edited by ryanmetzler3

Just pre-fill the name and email fields when you print the comment form rather than trying to figure out how to replace the variables. If you want to prevent a logged in user from modifying their name/email then just remove those fields when they are logged in and only show a comment field, then when you post the comment check if they are logged in and if so, use their name/email

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.