php array to javascript [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
I have a PHP array that I want to use in a javascript script. I suppose that I will have to convert it to a string that can be understood by javascript, and embed the string inside the script. What is the best way to accomplish this.
EDIT - This is for initial load of the page, not subsequent AJAX call
$textMessages = array();

You could use JSON.
json_encode and json_decode on the server side and JSON.stringify and JSON.parse on the client side. Both client side functions are natively built-into modern browsers but if you need to support legacy browsers you could include the json2.js script to your page.

json_encode(). Put in a PHP array, receive a string representation of a JS array.

JSON is perfect for this.
echo json_encode($textMessages);

Related

how to pass variables in url with an ID section using PHP? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I can do this:
home
But when I have a link with an Id, I can't pass variables:
home <--!this doesn't work of course -->
Thanks a lot!
When using PHP you can not use '#' in the URL, it will not be passed to the server.
You can use urlencode in order to encode the non-alphanumeric characters.
Use window.location.hash in javascript
<script>alert(window.location.hash);</script>
And the parse_url() function in PHP
<?php echo parse_url("home.php?var=home#sectionID",PHP_URL_FRAGMENT);?>
your HTML will be..
home
Declare a variable with $home dollar sign, i think you getting confused with JavaScript variable and PHP. To retrieve the variable values on a new page use $_GET['ID'].
Hope this helps.

How to print address url including # [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I want to echo exact url : http://domain.com/mystuff.html#123456
but it only print http://domain.com/mystuff.html how to include the #123456 ? so become http://domain.com/mystuff.html#123456
my code so far
$urladdress = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING'];
You cannot. The fragment identifier is handled entirely client side so the browser will not send it to the server.
(The nearest you could come would be to (after the page has loaded) use JavaScript to sent it to the server in another request.)
you cannot get it using php, you can get it done by javascritp. use this test script to check your result.
<script>
alert(document.URL);
var urlDetail = document.URL;//you can use this variable to print in your web page.
</script>
Found the answer:
text

passing a php array to a jquery function [duplicate]

This question already has answers here:
Passing PHP variable into JavaScript [duplicate]
(5 answers)
passing PHP objects to javascript [duplicate]
(4 answers)
Closed 9 years ago.
I need to pass a php variable to a jquery function. the php variable is an array of unknown size. is there a way to pass each member of the array to be processed individually by the function?
I Guess you should use JSON to pass the php variable to jquery. its the easiest way to do that.
You can use JSON to print the array and parse that to receive a JS object.
I assume you use AJAX (directly printing into the javascript source is possible but ill-advised).
Your php script needs to print output directly:
print_r(json_encode($data_array));
And on the receiving end use $.parseJSON:
var data_array = $.parseJSON(ajax_result);

403 FORBIDDEN ajax calling script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript URL encode
some scripts working fine started to give me this error.
It is probably due to some special chars inside the string passed.
Ajax is calling this:
.../ControllerAjaxSpecifiche.php?Material=100%%20poliester&product=Maglia (trikot)
this way it says 403 forbidden error
removing the % and () it works
.../ControllerAjaxSpecifiche.php?Material=100%20poliester&product=Maglia trikot
How I can solve this problem?
I tried passing those variables as post, buit it did not works, and I continue having same problem.
Try urlencode() (php) or encodeURI() encodeURIComponent() (js) before passing the values. This should escape any harmful characters
You need to use urlencode to format your links correctly

Using a Javascript variable inside a php block [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP
I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?
The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.
Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.
try with it
<script>
var whatever = "<?php echo $phpVar ?>";
</script>

Categories