Jump to content

Changing Variables Value Dependent on If statement


ryanmetzler3

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);  
                        })
                    }  
                });
            }
        });

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.