0

How do I make the java script read the value of $info ?

        $(document).ready( function() {
            $("#anyvalue").focus( function() {
                if ( $(this).val()=="<?php echo $info['anyvalue'];?> ") {
                    $(this).val('');
                } 
            });
        });

This code clears the default value of the text when the user clicks on the textbox.

HTML code

<input id="anyvalue" type="text" value="<?php echo $detail ['firstname'];?>">
4
  • Remove the trailing extra space character after <?php echo $info['anyvalue'];?>. Also ask a clear questin. what do u want to do ? Commented Dec 22, 2015 at 1:54
  • The JS won't find that because that PHP code is on the server. The JavaScript is looking at the code on the front-end and when the the server code get's sent to the front-end from PHP, it's just regular HTML (no PHP code). Commented Dec 22, 2015 at 1:58
  • @Trix What I wanted to do is to clear the value of $info using javascript. Commented Dec 22, 2015 at 2:10
  • Using '$(this)' you are referencing to the input '#anyvalue' so when the input has focus does just what you wrote 'empty the value of this input'. Commented Dec 22, 2015 at 2:17

4 Answers 4

2

i think you can create a meta tag on your head tag like this

<meta id ="anyvalue" data-content="<?php echo $info['anyvalue'];?>">

then accessing it using jquery

<script>
      $(function() {
         var anyvalue = $('#anyvalue').data('content');
      })
</script>

or using vanilla javascript

<script>
   var anyvalue = document.getElementById('anyvalue').getAttribute('data-content');
</script>

but this is more appropriate rather than echoing data directly to javascript. .

to answer your problem

   $(document).ready( function() {
        $("#anyvalueTextField").focus( function() {
            if ( $(this).val() == $('#anyvalue').data('content')) {
                $(this).val('');
            } 
        });
    });
Sign up to request clarification or add additional context in comments.

4 Comments

Very nice. Much more organized/semantic than what I've usually seen
So by using the 'anyvalue' I can now change the value whenever I wanted ?
it depends on how you will you use it. . my answer is just show how you should pass data from the server to html then accessing it using javascript .. if you want to change the value of a field you can use $(element).val(newValue) of jquery
Up vote for both answers. I think this question needs an upvote just sayin :D
0

You can insert PHP code inside Javascript, but PHP won't be able to read the Javascript variables.

To my understanding, $info['anyvalue'] must be blank or undefined.

In other words, in your code, the server communicates with the client but the client doesn't communicate with the server.

You have to find another way to exchange data between the client and the server (POST, Ajax, or some trick with PHP nested into Javascript nested into PHP).

3 Comments

Do you any simpler examples of POST,ajax ?
Simple but efficient: w3schools.com/ajax . It will be useful if you want to dynamically change $info['anyvalue'] from Javascript to PHP.
Is there anything I need to download to use ajax ?
0

Disclaimer: This is really bad practice

You can use PHP to initialize a javascript variable if the Javascript code is in your PHP file. So the following would work:

page.php

<?php $info['anyvalue'] = "foo"; ?>
$(document).ready( function() {
     $("#anyvalue").focus( function() {
         if ( $(this).val()=="<?php echo $info['anyvalue'];?> ") {
            $(this).val('');
         } 
    });
});

Since the PHP code will be pre-process before going to the Browser <?php echo $info['anyvalue'];?> will become "foo".

But don't do this its messy and not very nice to future developers or your future self, when you have to switch between PHP and JS to track down bugs.

2 Comments

Why is it a bad practice ? Sorry I am very new learning at javascript.
Its messy, use noobHere's answer its much cleaner.
0

To parse php inside javascript use single quotes instead of double like:

<script>
    $(document).ready( function() {
        $("#anyvalue").focus( function() {
            if ( $(this).val()=='<?php echo $info['anyvalue'];?>') {
                $(this).val('');
            } 
        });
    });
</script>

1 Comment

Can you explain on how does it affect by using single quote please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.