0

Im trying to show GET variable data in a textarea. I tried may solutions in StackOverflow but none of them worked. I have no idea why. Can you please tell me if I'm missing something.

My Story

I have a link on a page and href is /index.php?page=contact-us&performance=Lamborghini

and it takes me to the Contact Us page and the url is still /index.php?page=contact-us&performance=Lamborghini which is what I want.

So, Im tyring to grab performance variables's content which is Lamborghini and print it in a textarea which is

<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32"></textarea>

So what I did is added <?php $performanceInfo= $_GET['performance']; ?> to the top of my page. Then added $("#fbrp__32").val(<?php echo $performanceInfo; ?>); in my script.js file. But it never prints Lamborghini in the textarea.

When I try $("#fbrp__32").val('<?php echo $performanceInfo; ?>'); (Notice the ' marks) it prints <?php echo $performanceInfo; ?> in the textarea.

When I <?php echo $_GET["performance"]; ?> it prints Lamborghini fine too.

Can you guys tell me why its not working for me?

NOTE : I cant edit the textarea manually as it is generated using a plugin Thanks a lot.

user1889007
  • 319
  • 2
  • 4
  • 13

6 Answers6

1

PHP Parse will not parse .js files, you will need to do this outside of the .js file. If not you will need to in your webserver config file set to parse .js files as PHP files.

EDIT

You mentioned that you added <?php $performanceInfo= $_GET['performance']; ?> to the top of the page and then $("#fbrp__32").val(<?php echo $performanceInfo; ?>); to your script.js file. This will not work as per default configuration script.js or any .js file will not be interpreted by the PHP parser. so your statement $("#fbrp__32").val(<?php echo $performanceInfo; ?>); will cause a syntax error.

The best way to handle it without changing server configuration would be to do this at the top of your page.

 <script type='text/javascript'>
  var performance = "<?php echo $_GET['performance']; ?>";
  </script>

and in your script.js file do $("#fbrp__32").val(performance);

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    @francisco.preller read it again, he says `Then added $("#fbrp__32").val(); in my script.js file. But it never prints Lamborghini in the textarea.` – DevZer0 Jun 13 '13 at 06:32
  • yes, I did re-read, I suspect you are correct and OP is using two files. My apologies! :) – francisco.preller Jun 13 '13 at 06:33
  • @DevZer0 sorry can you please clarify what I have to do? I was looking this answer (http://stackoverflow.com/a/5895840/1889007) and it says you can assign a 'php' value to `js`? Im confused – user1889007 Jun 13 '13 at 06:39
  • @DevZer0 thanks. But it still prints `` in my textarea – user1889007 Jun 13 '13 at 06:47
  • You say when you do `When I it prints Lamborghini fine too.` Then there should be nothing preventing it from setting the word Lamborghini to the java script vaiable – DevZer0 Jun 13 '13 at 06:48
  • @DevZer0 yeha its weird. This never happened to me before. Anyway see below for my solution that fixed it – user1889007 Jun 13 '13 at 06:59
0

Try this

<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32">

<?php echo $_REQUEST['performance']; ?></textarea>

OR in JS

 $(document).ready(function(){
 var performance='<?php echo $_REQUEST["performance"]; ?>';
 $("#fbrp__32").val(performance);
 });
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

Just add the following right before the textarea (in your PHP/HTML-file, not JS-File!):

var performance = <?php echo '"' . htmlspecialchars( $_GET['performance'] ) . '"'; ?>
$("#fbrp__32").val(performance);
webmonkey
  • 1,083
  • 1
  • 15
  • 33
0

Just set value directly using PHP

<?php
 $performanceInfo= $_GET['performance']; 
?>

JQuery is a bit tricky.Instead try simple javascript for setting value:

document.getElementById("Enter Here Textarea Id").value = <?PHP echo $_GET['performance']; ?>
user2360906
  • 107
  • 2
  • 3
  • 12
0

Ok. Finally this is the solution which worked for me.

In the php file I put

<?php

$performanceInfo= $_GET['performance'];
echo "<script>";
echo "$('#fbrp__32').val('$performanceInfo');";
echo "</script>";

?>

Its weird other solutions didnt work for me for somereason.

user1889007
  • 319
  • 2
  • 4
  • 13
-1

Try this

var value = <?php $performanceInfo= $_GET['performance']; ?>
    $("#fbrp__32").val(value);

NB: php tags will not not render in js files

Afsal
  • 349
  • 3
  • 10
  • @user1889007 try this. It will work if the code is in a html file. Hope you are not using a js file – Afsal Jun 13 '13 at 06:20
  • iirc, val will input into textarea anyway – francisco.preller Jun 13 '13 at 06:30
  • The `val()` method sets the `value` property, not the `value` attribute. It is the correct way to change the value of a textarea programatically. Also: Never inject unsanitized, unescaped data into the page! – Quentin Jun 13 '13 at 06:31