ryanmetzler3 Posted December 13, 2013 Share Posted December 13, 2013 I have an html form with a field for name, email, and comment. If you are not logged in you have to type these things in manually. They are then stored in javascript variables and run through ajax (which I did not bother to include). The PHP if statement below is supposed to detect if you are logged in, then replace the name and email variables with your username and email automatically (which are stored in PHP variables). The way I have it now does not work at all. How can I replace the value of the javascript variables with the contents of a PHP variable using an if-statement? <script type='text/javascript'> $('.bt-add-com').click(function(){ var theCom = $('.the-new-com'); var theName = $('#name-com'); var theMail = $('#mail-com'); <?php if($username && $userid) : ?> var theName = "<?php $username; ?>"; var theMail = "<?php $email; ?>"; <?php endif; ?> </script> Quote Link to comment Share on other sites More sharing options...
BrodaNoel Posted December 13, 2013 Share Posted December 13, 2013 (edited) I cant understand wich is you question, because my english is soo bad. If you can explaint you again, and more quickly and "neutral", will be good. Check you have an error... You forget a "});" <script type='text/javascript'> $('.bt-add-com').click(function(){ var theCom = $('.the-new-com'); var theName = $('#name-com'); var theMail = $('#mail-com'); <?php if($username && $userid) : ?> theName = "<?php $username; ?>"; theMail = "<?php $email; ?>"; <?php endif; ?> }); // This you forget </script> Edited December 13, 2013 by BrodaNoel Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 13, 2013 Share Posted December 13, 2013 You need to echo the variables so their values are outputted into the javascript <?php if($username && $userid) : ?> var theName = "<?php echo $username; ?>"; var theMail = "<?php echo $email; ?>"; <?php endif; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 13, 2013 Share Posted December 13, 2013 (edited) Any PHP is executed on the server before the page is sent to the user. If $username or $email are not defined at the time the page is first requested, then there will be no values put into the JavaScript. But, I am confused by what you are trying to do. If the user is already logged in, and you have these values in PHP, then why are you populating the values into JavaScript to then use JavaScript to populate them into the fields. Why not use PHP to directly populate the values into the fields when the page is created. In any event, when trying to debug PHP / JavaScript issues you need to do some simple debugging to see where the problem lies. Otherwise you are just stumbling in the dark. View source on the page to see if the values are even getting populated in the page. If not, then you have a PHP problem and most likely the variables are not getting defined. If the values are in the page, the there is an error in the JavaScript. [EDIT: Good catch Ch0cu3r - I missed that. But, my previous response stands, the logic doesn't make sense] Edited December 13, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
ryanmetzler3 Posted December 14, 2013 Author Share Posted December 14, 2013 Any PHP is executed on the server before the page is sent to the user. If $username or $email are not defined at the time the page is first requested, then there will be no values put into the JavaScript. But, I am confused by what you are trying to do. If the user is already logged in, and you have these values in PHP, then why are you populating the values into JavaScript to then use JavaScript to populate them into the fields. Why not use PHP to directly populate the values into the fields when the page is created. In any event, when trying to debug PHP / JavaScript issues you need to do some simple debugging to see where the problem lies. Otherwise you are just stumbling in the dark. View source on the page to see if the values are even getting populated in the page. If not, then you have a PHP problem and most likely the variables are not getting defined. If the values are in the page, the there is an error in the JavaScript. [EDIT: Good catch Ch0cu3r - I missed that. But, my previous response stands, the logic doesn't make sense] The reason I am trying to fill the java script variables is because it is a comment section that runs on ajax. I have the entire ajax script written to run off of these variables and it seems easier to me to change their value rather than somehow incorporating php throughout the ajax script. Quote Link to comment 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.