Select file name and redirect..
index.php
<?php
$book_name = ["Jenkins_Essentials","Asterisk","phalcon"];
echo "<select><option selected>Book Name</option>";
foreach ($book_name as $key => $value) {
echo "<option name='$key'>$value</option>";
}
echo "</select>";
?>
<script type="text/javascript">
$(document).ready(function(){
$("select").on("change",function(){
location.href = "reading.php?title="+$(this).val();
});
});
</script>
reading.php
$title = $_GET["title"];
header("Content-type: application/pdf");
header('Content-Disposition: inline; filename="$title.pdf"');
#readfile('D:\Learning\$title.pdf');//this is my issue
When I redirected it show Failed to load PDF document..
My running script file location is as we know C:\xampp\htdocs But pdf file place is as shown in above D: drive !How to give path to it?
On your last two lines, PHP isn't including the $title variable, as you are using single quotes and you are using backslashes. Try one of these:
header('Content-Disposition: inline; filename="'.$title.'.pdf"');
#readfile('D:/Learning/'.$title.'.pdf');
or:
readfile("D:/Learning/$title.pdf");
Backslashes are used for escaping characters, so use forward slashes as much as you can. On Windows, you can use both in file paths. Also, for outputting the file, try using this instead of #readfile:
$pdf = file_get_contents('D:/Learning/'.$title.'.pdf');
echo $pdf;
Another note - you should check if the file exists before accessing it. Place at the top of your script:
if(!file_exists('D:/Learning/'.$title.'.pdf')) {
echo "File doesn't exist.";
exit();
}
Hope this helps. Wish you the best.
Is the file name 'D:\Learning\$title.pdf' literally $title.pdf with the dollar ($) sign.
PHP variable interpolation works on the " double quote meaning that your variable is literally a string and not recognized as a variable by php.
You'll most likely want to change that to
readfile("D:\Learning\$title.pdf");
OR ( personally I would avoid this because of the back slash escaping ) However it's worth noting that windows will accept the forward slash ( Unix style )
readfile('D:\Learning\\'.$title.'.pdf');
readfile('D:/Learning/'.$title.'.pdf'); //this works fine on windows and avoids escaping the \
OR what I prefer.
readfile("D:\Learning\{$title.pdf}");
Otherwise it's looking for a file named $title.pdf literally
Related
I have a PHP page with some JavaScript code also, but this JavaScript code below doesn't seem to work, or maybe I'm way off!
I am trying something like this:
var areaOption=document.getElementById("<?php echo #$_POST['annonsera_name']?>");
areaOption.selected=true;
Also I have tried this, but it only alerts a BLANK alert-box:
alert (<?php echo $test;?>); // I have tried this with quotes, double-quotes, etc... no luck
Am I thinking completely wrong here?
UPDATE
Some PHP code:
<?php
$test = "Hello World!";
?>
In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).
Test this:
alert (<?php echo "'H'";?>);
OR
alert ('<?php echo "H";?>');
PHP runs on the server side and Javascript is running on the client side.
The process is that PHP generates the Javascript that will be executed on the client side.
You should be able to check the JS that is generated just looking at the code. Of course, if the JS relies on some PHP variables, they need to be instanciated before the JS is output.
<?php
$test = 'Hello world';
?>
<html>
<body>
<script>
alert('<?php echo $test; ?>');
</script>
</body>
</html>
will work but
<html>
<body>
<script>
alert('<?php echo $test; ?>');
</script>
</body>
</html>
<?php
$test = 'Hello world';
?>
will not
Use json_encode to convert some text (or any other datatype) to a JavaScript literal. Don't just put quotes around the echoed string — what if the string has a quote in it, or a newline, or backslash? Best case your code fails, worst case you've got a big old cross-site-scripting security hole.
So,
<?php
function js($o) {
echo json_encode($o, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
}
?>
<script type="text/javascript">
var areaOption= document.getElementById(<?php js($_POST['annonsera_name']); ?>);
areaOption.selected= true;
alert (<?php js('Hello World'); ?>);
</script>
Your using #$_POST indicates that you have received (or are expecting) errors - check your generated source to see if the value was output correctly. Otherwise document.getElementById will fail and you'd get no output.
alert("Delete entry <? echo $row['id']; ?> ")
If your extension is js, php will not work in that file.
The reason being, php parses on files that it is supposed to. The file types that php will parse are configured in httpd.conf using AddType commands (or directives, whatever they are called).
So you have 3 options:
add filetype js to the list of files php will parse (BAD, VERY BAD)
make the script inline to some php file
rename the file to script.js.php, and at the beginning of the file, specify the content type, like so:
<?php header( 'content-type: text/javascript' ); ?>
Cheers!
I have a Javascript function that takes a string and sends it via ajax to a PHP page. In PHP I would put this string inside the head tag (that already exist) of an html file. This change should be final. How could I do this? I don't know how to manipulate html code via PHP.
<?php
$htmlpage = file_get_contents('http://example.com/index.html');
echo str_replace("<head>", "<head>New text", $htmlpage);
//the result should be from <body></body> to <body>New Text</body>
?>
Also if you have text between body tags you can include it in
str_replace("<body>Old Text", "<body>New Text", $htmlpage)
If you have the string inside a varibale or you are using $_POST then instead of "<head>New text" put "<head>".$_POST['thestring']
Just use if/else statements and echo string from ajax to your tags if you can't use JS for this.
One way around this would be to use file_get_contents, then do a string replace on the tag with the amended code, and then write the changes with the ammended code.
Example
<?php
$file = "myhtmlfile.html";
$original = file_get_contents($file);
$needle = "<head>";
$new_content = "<head>\n".$_POST['header'];
$replacement = str_replace($needle, $new_content, $original);
file_put_contents($replacement, $file);
?>
I had to change my question to simple question
For example: my XML file name is: ABC.xml
inside of this xml file is:
<li class="thumb"><a class="Alibaba" href="google.com"></a></li>
So, here is my question:
How to use PHP to search string inside of ABC.xml and find Alibaba name and replace it to AlibabaColor name and save it to ABC.xml
Please guide me and give me an example for this. Thank you
You can do this using php str_replace among many other functions. Here is a couple examples ->
If the XML is in a string already you can do this
PHP:
<?php
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $myXmlString);
?>
If you need to get the XML data from another file there are a few ways to to do it depending on if you need to save it, just replace it and display it, or really what you are doing. Some of this comes down to preference.
Just need to display it?
<?php
$xml = file_get_contents(/path/to/file); // or http://path.to/file.xml
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $xml);
?>
Need to change the file itself?
<?php
$xml = file_get_contents(/path/to/file); // or http://path.to/file.xml
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $xml);
file_put_contents(/path/to/file, $myXmlString);
?>
--
To answer the below comment, here is working code from my server ->
xml:
<li>hello</li>
php:
$file = '/home/username/public_html/xml.xml';
$xml = file_get_contents($file);
$text = str_replace('hello','world',$xml);
file_put_contents($file, $text);
xml.xml after:
<li>world</li>
Keep in mind to change 'username' to the right one, or use an FQDN if you are more comfortable doing so. Make sure that the file has permission to be written to by a script as well.
I am using the same line of code to echo a link onto my page in two locations and getting different results. One is inside an < a > tag and the other is inside a < script > tag. Below are the two excerpts of code (from the same tpl file) and the results:
<?php echo $btn['text']; ?>
Results in:
Filter
However in the script tag I have
url = "<?php echo $links['current_path']['href']; ?>"
And my result is
url = "index.php?route=module/jw/list&token=a4c693a4e38916fc03af23ad4fe1"
Notice the '&' after the route parameter. It is displaying the html code when I echo it inside the script tag. I know I can convert it in the second instance, but I am curious as to why I would need to. Why is the same php command making the symbol echo differently in various parts of the source code?
& is a HTML entity - your browser parses and displays it. Some similar questions about decoding here and here. Have you tried html_entity_decode():
html_entity_decode — Convert all HTML entities to their applicable characters
So
url = "<?php echo html_entity_decode($links['current_path']['href']); ?>"
Use the php function html_entity_decode
Example:
<?php echo $btn['text']; ?>
I Have one registration and payment page. program flow is like registration > confirmation > payment. After validation and calculation in registration page need to go to confirmation page.
My code is
<?php
ob_start();
session_start();
include("header.php");
include("ini.php");
date_default_timezone_set('Asia/Brunei');
header('Cache-Control: max-age=900');
if (isset($_POST['submit'])) {
$error = 0;
//validations
if ($error <= 0) {
//do some calculation
header('location:confirm.php');
}
}
<?php
ob_flush();
?>
Control entered in to if ($error <= 0) clause, but stay on the registration page.
I tried many ways like
header('location:confirm.php', true, 302);
instead of
header('location:confirm.php');
removed
ob_start() and ob_flush()
add exit() after header()
then it goes to blank page.
Also try to use
echo '<script type="text/javascript">';
echo 'window.location.href="confirm.php";';
echo '</script>';
Then the control goes to confirmation page but url seems to be as registration.php
But header('location:confirm.php'); is working fine in my local server. Anybody please help me to resolve this issue.. Is there any alternate way for header(). Looking forward to you guys.. Thanks
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Also try to change header('Location:confirm.php');
(Note L is capital since its work in your local server may be problem with strict php type try this once)
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'confirm.php';
header("Location: http://$host$uri/$extra");
use ob_end_flush() at the end. This should work like charm.
i had the same issues for years but today i discovered that u don't have to have any html or blank space before the header word .Try it out