Jump to content

PHP variables inside of Javascript


ryanmetzler3

Recommended Posts

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>
Link to comment
Share on other sites

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 by BrodaNoel
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.