I'm accessing my script throw an URL like that
http://www.anydomain.com/code.php?a=ií
and code.php is basically this
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<html><head>
<meta charset="UTF-8">
</head><body>
<script>
console.log('<?php echo $_SERVER['QUERY_STRING'];?>');
console.log(unescape('<?php echo $_SERVER['QUERY_STRING'];?>'));
console.log('<?php echo $_GET['a'];?>');
</script>
</body></html>
the logs I'm getting are:
a=i%C3%AD
a=iÃ
ií
In real life I'm expecting a lot of parameters and need to save the full query, so $_SERVER['QUERY_STRING'] would be very handy. But the charset seems not to be working properly, as it is with the $_GET function.
Is there a way to get the query using $_SERVER['QUERY_STRING'] but using the charset of $_GET?
The problem was not at PHP at all but in the javascript function unescape. When I changed to decodeURIComponent it worked just fine.
$query = http_build_query($_GET);
Related
I am trying to use php serialize to reserve an array and use the html textarea as a transit to post the data and download the array as csv file, the download function is working. However, when an array returns a great number of rows, it kept hitting the error as below:
Bad Request
Your browser sent a request that this server could not understand.
Size of a request header field exceeds server limit.
Referer
/n .
My guess is the textarea does not have enough rows or columns allowed?
Can anybody help? Thanks a lot!
<?php
$user_arr[] = array($project,$dept,$community,$corte,$pmb,$pme,$type,$area,$officesupervisor,$status);
$serailze_user_arr = serialize($user_arr);
//echo $serailze_user_arr;
?>
<textarea name='export_data'><?php echo $serailze_user_arr;?></textarea>
<?php
$filename = "user.csv";
$serailze_user_arr = unserialize($_POST['export_data']);
// download
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment; filename=$filename");
$file = fopen($filename,"w");
fputcsv($file, array('ID', 'Name', 'Address', 'PMB', 'PME', 'YEAR'));
foreach($serailze_user_arr as $line){
fputcsv($file,$line);
}
fclose($file);
readfile($filename);
?>
We are here to help if you are more clear with question we can move further.
Sorry if it hurts you.
Thanks for this oppurtunity to connect with you
I have the following piece of code which I partly got from another thread in SO:
<?php
header('Content-Type: text/html; charset=ISO-8859-1');
$origadd = $_SESSION["OriginAdd"] // $_SESSION["OriginAdd"] has a value "rueFrédéricMistral";
echo $origadd; // This displays rueFrédéricMistral
?>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
var source = <?php echo json_encode($origadd); ?>;
alert(source); // This displays null
</script>
</head>
....
....
</html>
As I have mentioned in the comments, the echo statement gives me the original string. However, the alert box in JS gives me a null value because of which the subsequent code is failing.
The problem here is an encoding issue and the accents played a major role here. Had the string been just "abc" for example, it would have alerted it properly. Everything needs to be UTF-8 if you want both contents to show up properly.
Therefore, you need to change the file's encoding to UTF-8 and also set the header as UTF-8.
This was tested with successful results.
<?php
header('Content-Type: text/html; charset=UTF-8');
$origadd = "rueFrédéricMistral";
echo $origadd; // This displays rueFrédéricMistral
?>
<script type="text/javascript">
var source = <?php echo json_encode($origadd); ?>;
alert(source);
</script>
Note: $origadd = "rueFrédéricMistral"; was used from the original post holding that value.
https://stackoverflow.com/revisions/46956093/1
where I stated that the string needed to be wrapped in quotes, otherwise it would have been treated as a constant. (To which someone deleted that comment I placed as being the first comment).
Note:
Using header('Content-Type: text/html; charset=ISO-8859-1'); instead of header('Content-Type: text/html; charset=UTF-8');, your initial HTML would have shown you rueFrédéricMistral instead of the intended rueFrédéricMistral.
Edit:
As said by the OP, my mentioning to quote it would have also worked.
var source = '<?php echo $origadd; ?>';
As per my comment above.
If you use var source = '<?php echo $origadd; ?>'; as my example and quoting it (something you didn't do), you will see your data is there.
I have read the following posts, but they are a little over the top for me. I think I'm trying to do something fairly simple, and would like some guidance.
How to pass variables and data from PHP to JavaScript?
passing PHP variables across javascript windows.open to another PHP page
Posting a php variable to a new window
Here is the case:
I have a php script which is very simple, it calls another script and passes 2 variables:
<?php
echo '<script type="text/javascript" language="javascript">
window.open("http://callpage.com/utils/cdr.php?callernum=123456789&calltime=2017-02-22 16:24:12");
</script>';
?>
Note: This is just a "hardcoded" example.
The next script, takes those numbers and builds file/url variable.
Lets say
$file = /var/www/html/file.wav
What I'm trying to do open a new window to the effect of :
http://newpage.com/$file
I have read and found that I think the best use is Javascript, but I can't seem to get my variable into the Javascript.
Here is what I would like to get working:
<?php
$file = /var/www/html/file.wav
echo '<script type="text/javascript" language="javascript">
window.open("http://newpage.com/$file");
</script>';
?>
A few notes:
I don't want to "redirect" the old page, I want it to stay open, and the remote page isn't on the same domain
(one is a.domain.com and the other is b.domain.com).
I don't care about window sizes, etc, its a wav file that I'm expecting the browser to just play with a simple Browser default interface for Wav.
if I understood correctly what you want, you have to concatenate the string with the variable in order to be replaceed
<?php
$file = '/var/www/html/file.wav';
echo '<script type="text/javascript" language="javascript">
window.open("http://newpage.com/'.$file.'");
</script>';
?>
Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript:
echo "<script type='text/javascript' language='javascript'>
window.open('http://newpage.com/$file');
</script>";
The interpolated PHP variable $file should be correctly interpreted as a string and the value it holds should be displayed in the URI of your javascript.
Check out this easy to understand info about variable interpolation http://phppot.com/php/variable-interpolation-in-php/
Here is my "final" code snippet:
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$uid = $row['uniqueid'];
foreach(glob($path. "*". $uid. "*") as $file) {
$link = "http://newpage.com$file";
echo "<script type='text/javascript' language='javascript'>
window.open('$link');
</script>";
}
}
I am developing a web that has to show an sql view, so I did the query with PHP, but I have to show the result in charts, and I am trying to use the google geochart. So basically what I want to do is:
Select the data from an SQL view with PHP.
Get the data from PHP into a javascript variable so I can use it in the chart.
Get the value of the javascript variable to put in the google chart api, so it show what I want.
So far, I've got the point 1 and the point 2 (I think) done. But when I am trying to use the javascript variable again in another part of the code it has no value, so no data is showing, I am getting undefinedon the explorer.
Relevant Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
//connections and stuff
$tsql="SELECT count(*) as Qty, ShipCountry FROM [Orders Qry] group by ShipCountry"; // yes, is the Northwind database example
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt === false)
{
FatalError("Failed to query table: ".$tsql);
}
else
{
$json=array();
$i=0;
echo "<script type='text/javascript'> var countries = []; </script>";
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
echo "<script> countries[".$i."]= ". $row['ShipCountry'] .";</script>";
$i=$i+1;
}
//echo "<h1>". $json["ShipCountry"] ."</h1>"; //I was testing, so the problem is not in retrieving the data from the database.
sqlsrv_free_stmt($stmt);
}
?>
<p>
<script type="text/javascript">
document.write(countries[0]);
</script>
</p>
</body>
</html>
You forgot to quote $row['ShipCountry'] (seems to be a string);
echo "<script> countries[".$i."]= '". $row['ShipCountry'] ."';</script>";
Note the new quotes.
You might want to consider using AJAX to query a different file from within your javascript, cf. http://www.w3schools.com/ajax/ajax_php.asp.
If you have your PHP file return JSON to the AJAX request, javascript will have an object that it understands and you can use it there. This way you can have all your javascript in one place. E.g. this pseudo code:
Javascript.js
function gimmeACountry(i){
var countries = AJAX.get('countries.php');
return countries[i];
}
PHP.php
<?php
$result = mysql_stuff(...);
print json_encode(result);
?>
HTML
<html>
<head>
<script src='Javascript.js'>
</head>
<body onload="document.write(gimmeACountry(0));">
</body>
</html>
If you really want to use just one file, a few thoughts:
You don't need to open and close a statement every time you write javascript. All of your PHP could be embedded in one.
You can output most of your javascript outside of the block, instead of echoing everything. I think the PHP is clearer then. E.G.
<script>
<?php $foo = 'bar'; ?>
var foo = <?php echo $foo ?>;
document.write(foo); // writes 'bar'
</script>
If you are still have scope issues, you can try adding your variable to the window object, e.g.
window.countries = []
This might be problematic if you end up doing more stuff with javascript later. I really do recommend you use AJAX.
You should use push method and Array() Construct
var countries = [];//nope
var countries = new Array();//yep
echo "<script> countries[".$i."]= ". $row['ShipCountry'] .";</script>";//nope
echo "<script> countries.push(".$row['ShipCountry'].");</script>";//yep
push method documentation
Distributing javascript over two blocks works fine:
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
<script>
countries = ['foo'];
</script>
<script type="text/javascript">
document.write(countries[0]);
</script>
</body>
</html>
But the problem is that your PHP isn't generating valid java script. Have a look at your browser's JS console, and you'll see ReferenceErrors because you didn't quote the country names.
I need to send result from my PHP file to the JavaScript function/file.
I found some of answers like: var x='<?php echo $pathinfo; ?>';
or this:
myHtmlFile.html:
<script type="text/javascript" src="jquery-1.8.1.js"></script>
<script type="text/javascript" src="newEmptyPHP.php"></script>
<script language="javascript">
function test()
{
alert(result);
}
</script>
newEmptyPHP.php:
<?php
$res="it is result";
echo "var result = ".json_encode($res).";";
?>
My question is whether there is way return value from php function and not from php file.
something like this:
<?php
function phpFunction(){
$res="it is result";
return $res;
}
?>
I need simple PHP/JavaScript code without something complex.
I want keep many function in one php file and not many php files.
<?php
function phpFunction(){
$res="it is result";
return $res;
}
?>
<script>
var result = "<?php echo phpFunction() ?>";
</script>
The above method will work. However, if you want more PHP/JS combined scripts, learn AJAX.
The best way to get your data to JavaScript from PHP is to use AJAX using jQuery you have declared.
You can use $.get()
You can call you php script which contains the function with parameters in you url request.
And when a specific parameter is specified in the url, then you can call you fonction and echo whatever you want to the javascript.
Have a good day!
You can open as many php tags as you wish inside your one php document. You can then either make this php document separate from your main document (however this would require making the main document in php and using a "php include" or "php require") or you can simply just open and close php tags as necessary. You do not even have to close the line at the end of the php, for example.
<?php if (a==a) { ?>
<div>
<script>
<?php echo 'do something here'; ?>
</script>
</div>
<?php } ?>
Using the above example can provide some unique results. Just remember that anything you do with php you can just echo out the result and slot it into the middle of your javascript.