ryanmetzler3 Posted December 13, 2013 Share Posted December 13, 2013 (edited) 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 December 13, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/284738-changing-variables-value-dependent-on-if-statement/ Share on other sites More sharing options...
kicken Posted December 13, 2013 Share Posted December 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284738-changing-variables-value-dependent-on-if-statement/#findComment-1462243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.