Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 02:52 PM   PM User | #1
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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..
cast_no_shadow is offline   Reply With Quote
Old 01-14-2013, 06:48 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 01-14-2013, 08:17 PM   PM User | #3
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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
cast_no_shadow is offline   Reply With Quote
Old 01-14-2013, 09:23 PM   PM User | #4
linek98
New Coder

 
Join Date: Dec 2012
Location: England
Posts: 18
Thanks: 0
Thanked 4 Times in 4 Posts
linek98 is an unknown quantity at this point
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."
linek98 is offline   Reply With Quote
Users who have thanked linek98 for this post:
cast_no_shadow (01-15-2013)
Old 01-14-2013, 11:55 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-15-2013, 08:31 AM   PM User | #6
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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:

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

Last edited by cast_no_shadow; 01-15-2013 at 08:37 AM..
cast_no_shadow is offline   Reply With Quote
Old 01-15-2013, 08:36 AM   PM User | #7
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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.
cast_no_shadow is offline   Reply With Quote
Old 01-15-2013, 09:53 AM   PM User | #8
cast_no_shadow
New Coder

 
Join Date: Aug 2012
Posts: 44
Thanks: 26
Thanked 0 Times in 0 Posts
cast_no_shadow is an unknown quantity at this point
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.
cast_no_shadow is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:23 AM.


Advertisement
Log in to turn off these ads.