![]() |
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:
Code:
<p><button onclick="myFunction()">Show the text</button></p>May you please help me? |
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?
|
Quote:
|
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:
PHP Code:
|
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>*BUT*, when the user clicks on that button, the *ENTIRE HTML PAGE WILL DISAPPEAR*, to be replace with *ONLY* Code:
ANY TEXT AT ALLWhy? 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";Code:
echo $words_unedited;********** SO... A corrected version of your function might be something like this: Code:
<p><button onclick="myFunction()">Show the text</button></p>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. |
Quote:
Quote:
|
Quote:
Thanks anyway. |
After much tweaking, I sorted out the problem by including the following line:
PHP Code:
|
| All times are GMT +1. The time now is 05:07 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.