
Originally Posted by
keyboard1333
The code you posted wouldn't work, but you said it did work so there must be more code that you're not showing us...
Could you please provide us with the full script?
Or if not, an example of what $a and $b are and the contents of your js function.
This little test script works fine...
PHP Code:
<?php
$a = 'Fatber Christmas';
$b = '27 Sunshine Street /n America';
$c = nl2br($b);
?>
<html>
<head>
<script type="text/javascript">
function test(a,b) {
alert(a);
alert(b);
}
</script>
</head>
<body>
<a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
</body>
</html>
Thanks for your reply!
Let me explain to you what I'm trying to do. I want to take input from users in a textarea which could be a number of paragraphs and save it in MySQL db. Now, that's working fine as I've tried it myself.
Now I want to display that information (which i've fetched from MySQL db through queries and saved them in php variables, eg, $a) through a javascript (passing php variables) which is called through 'onClick'.
When I print $a without passing it to JS function it display entire string without any linebreaks, but when I try to pass $a to JS function and print it through an OnClikck event, nothing gets printed. (referring to the below code)
Code:
<?php
$a = very long string
$b = "Hello People!!!!";
?>
<html>
<head>
<script type="text/javascript">
function test(a,b) {
document.getElementById('div1').innerHTML=a;
document.getElementById('div2').innerHTML=b;
}
</script>
</head>
<body>
<a onClick="test('<?php echo $a ?>', '<?php echo $b ?>');">LINK</a>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
Is there a way to ensure that I can print that long string through <div id="div1"></div>.
Also I want to pass $a in a way that all the linebreaks are retained in it (like the way it is present in MySQL db).
I also tried using below code, but this code prints $a when I call onClick event rather than passing it to test() function.
Code:
<?php
$a = very long string
$b = "Hello People!!!!";
$a=nl2br($a);
?>
<html>
<head>
<script type="text/javascript">
function test(a,b) {
document.getElementById('div1').innerHTML=a;
document.getElementById('div2').innerHTML=b;
}
</script>
</head>
<body>
<a onClick="test('<?php echo $a ?>', '<?php echo $b ?>');">LINK</a>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
Can you please help. I'd really appreciate any method or suggestion!!!
Bookmarks