Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts

    show a text from php variable

    I'd like to write a javascript code that shows a text included in php variable. This php variable has been taken from a mysql database.

    PHP Code:
    <?php 
    // conect to the database
    include("config.php"); 

    // Download words
    $video_number "4";
    $data mysql_query ("SET NAMES 'utf8'");    
    $data mysql_query("SELECT cz FROM videos WHERE id='$video_number' ") or die(mysql_error());
    $info mysql_fetch_array($data);
    $words_unedited $info['cz'];
    ?>
    Code:
    <p><button onclick="myFunction()">Show the text</button></p>
    <script>
    	function myFunction()
    		{
    			var unedited_text = '<?php echo "$words_unedited" ;?>';
    			document.write(unedited_text);
    		}
    
    </script>
    For some reason the javascript code is not printing anything

    May you please help me?
    Last edited by cast_no_shadow; 01-15-2013 at 10:00 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Are both PHP parts in the same PHP page or do you include() the first code segment into the page of the second code segment?

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Are both PHP parts in the same PHP page or do you include() the first code segment into the page of the second code segment?
    They are both in the same php file

  • #4
    New Coder
    Join Date
    Dec 2012
    Location
    England
    Posts
    20
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Does $words_unedited contains correct string to put into ' '?

    In google chrome open tools -> developer tools. New windows will appear, go to resources and find file that contains this JS code. It should say if there was an error. If there was no error then click that button of yours so it executes your JS function and then see if an error occurs.

    An easier way to check if this is a syntax error is to change
    PHP Code:
    $words_unedited $info['cz']; 
    to
    PHP Code:
    $words_unedited "If you can see this then there is a syntax error."

  • Users who have thanked linek98 for this post:

    cast_no_shadow (01-15-2013)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    You have a *MAJOR ERROR* in your code that has nothing to do with PHP interaction!!

    Let's take your code and remove the PHP, so it just looks like this:
    Code:
    <p><button onclick="myFunction()">Show the text</button></p>
    <script>
    	function myFunction()
    		{
    			var unedited_text = 'ANY TEXT AT ALL';
    			document.write(unedited_text);
    		}
    
    </script>
    Yes, that code will work.

    *BUT*, when the user clicks on that button, the *ENTIRE HTML PAGE WILL DISAPPEAR*, to be replace with *ONLY*
    Code:
    ANY TEXT AT ALL
    Even the JavaScript that was used to do the document.write will be gone.

    Why? Because if you use document.write *OTHER* than during the CREATION of the HTML of the page, you *automatically* first do the equivalent of window.open( ) of the current page, which means you are WIPING OUT all content of the page and replacing it with whatever document.write creates.

    This is only *ONE* of the reasons that document.write is considered very very obsolete! There are a very few reasons for using it, but they are much more advanced than have anything to do with responding to a mouse click or similar. As a good rule: Until and unless you ever discover that there is NO OTHER WAY to accomplish what you want, do *NOT* use document.write. And of course the same applies to alert( ) and confirm( ).

    ****************
    On a completely different topic:

    There is no practical difference between
    Code:
        echo "$words_unedited";
    and
    Code:
        echo $words_unedited;
    The quote marks in the first case will simply disappear. So the end result in HTML will be identical.

    **********

    SO...

    A corrected version of your function might be something like this:
    Code:
    <p><button onclick="myFunction()">Show the text</button></p>
    <p id="theText"></p>
    <script type="text/javascript">
    	function myFunction()
    		{
    			var unedited_text = '<?php echo $words_unedited;?>';
    			document.getElementById("theText").innerHTML = unedited_text;
    		}
    </script>
    If you aren't sure that you PHP code is doing the right thing, then DEBUG DEBUG DEBUG.

    In this case, the FIRST thing to do is bring up the web page in your browser. Then:
    -- click on the VIEW menu of your browser
    -- click on the SOURCE or PAGE SOURCE menu item
    -- look at the HTML that appears, which is the HTML *as the browser sees it*.
    -- find your myFunction code in that HTML
    -- see if it looks like it is, indeed, getting the expected string from PHP.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by linek98 View Post
    Does $words_unedited contains correct string to put into ' '?

    In google chrome open tools -> developer tools. New windows will appear, go to resources and find file that contains this JS code. It should say if there was an error. If there was no error then click that button of yours so it executes your JS function and then see if an error occurs.

    An easier way to check if this is a syntax error is to change
    PHP Code:
    $words_unedited $info['cz']; 
    to
    PHP Code:
    $words_unedited "If you can see this then there is a syntax error."
    I can see the text:

    "If you can see this then there is a syntax error.";
    I check that before, the problem I have is that the variable $words_unedited is an array (I think) and javascript is not able to print it as a string (at least I don`t know how to do it).
    Last edited by cast_no_shadow; 01-15-2013 at 08:37 AM.

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    You have a *MAJOR ERROR* in your code that has nothing to do with PHP interaction!!

    Let's take your code and remove the PHP, so it just looks like this:
    Code:
    <p><button onclick="myFunction()">Show the text</button></p>
    <script>
    	function myFunction()
    		{
    			var unedited_text = 'ANY TEXT AT ALL';
    			document.write(unedited_text);
    		}
    
    </script>
    Yes, that code will work.

    *BUT*, when the user clicks on that button, the *ENTIRE HTML PAGE WILL DISAPPEAR*, to be replace with *ONLY*
    Code:
    ANY TEXT AT ALL
    Even the JavaScript that was used to do the document.write will be gone.

    Why? Because if you use document.write *OTHER* than during the CREATION of the HTML of the page, you *automatically* first do the equivalent of window.open( ) of the current page, which means you are WIPING OUT all content of the page and replacing it with whatever document.write creates.

    This is only *ONE* of the reasons that document.write is considered very very obsolete! There are a very few reasons for using it, but they are much more advanced than have anything to do with responding to a mouse click or similar. As a good rule: Until and unless you ever discover that there is NO OTHER WAY to accomplish what you want, do *NOT* use document.write. And of course the same applies to alert( ) and confirm( ).

    ****************
    On a completely different topic:

    There is no practical difference between
    Code:
        echo "$words_unedited";
    and
    Code:
        echo $words_unedited;
    The quote marks in the first case will simply disappear. So the end result in HTML will be identical.

    **********

    SO...

    A corrected version of your function might be something like this:
    Code:
    <p><button onclick="myFunction()">Show the text</button></p>
    <p id="theText"></p>
    <script type="text/javascript">
    	function myFunction()
    		{
    			var unedited_text = '<?php echo $words_unedited;?>';
    			document.getElementById("theText").innerHTML = unedited_text;
    		}
    </script>
    If you aren't sure that you PHP code is doing the right thing, then DEBUG DEBUG DEBUG.

    In this case, the FIRST thing to do is bring up the web page in your browser. Then:
    -- click on the VIEW menu of your browser
    -- click on the SOURCE or PAGE SOURCE menu item
    -- look at the HTML that appears, which is the HTML *as the browser sees it*.
    -- find your myFunction code in that HTML
    -- see if it looks like it is, indeed, getting the expected string from PHP.
    The problem is clearly in $words_unedited = $info['cz']; not in document.write

    Thanks anyway.

  • #8
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    After much tweaking, I sorted out the problem by including the following line:

    PHP Code:
    $words_unedited preg_replace("'\s+'"' '$words_unedited); // remove extra space 
    In the php part. Thanks for your time.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •