CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Resolved show a text from php variable (http://www.codingforums.com/showthread.php?t=285764)

cast_no_shadow 01-14-2013 02:52 PM

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?

devnull69 01-14-2013 06:48 PM

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?

cast_no_shadow 01-14-2013 08:17 PM

Quote:

Originally Posted by devnull69 (Post 1306195)
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

linek98 01-14-2013 09:23 PM

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."


Old Pedant 01-14-2013 11:55 PM

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.

cast_no_shadow 01-15-2013 08:31 AM

Quote:

Originally Posted by linek98 (Post 1306237)
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:

Quote:

"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).

cast_no_shadow 01-15-2013 08:36 AM

Quote:

Originally Posted by Old Pedant (Post 1306265)
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.

cast_no_shadow 01-15-2013 09:53 AM

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.


All times are GMT +1. The time now is 05:07 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.