I'm using web2pdf to print a few values to a pdf document. In the html, these values show but when I'm converting the webpage to pdf, these php/js values disappear. What can I do to fix this?
<html>
<body>
<?php session_start();
$variable = $_SESSION['getSessionVar'];
?>
<span id = "name"><?php echo $variable ?></span>
<button id="cmd" > generate PDF </button>
..tags closed.
When I run this script, the variable value is shown but the PDF converter is not picking it up. Is there anything wrong with my code? Else, what other web-to-pdf generator can I use that can pick up Php or JS values written to HTML?
Thanks!
Update -
I can't seem to convert any GET/POST or SESSION variables in the php script to PDF even though I assign them to a html division. Also tried assigning php variable to js and using
document.getElementById('ID').innerHTML = "<?php echo #var_from_prev_page"?>";
However, I can print and convert php variables that I create on the page.
SESSION,POST and GET can't be show in your pdf.
the reason is pretty simple actually.
The site you are using is using the reference of the request to "Crawl" your page
So the session id they get is diferent than you therefore they can't have any $_SESSION loaded since they never browsed your site before, and they don't POST anything to the page since you are clicking on a link and not sending them any data to send back to your site.
The only way you could acheive what you want to do would be to have some way to change your ref of your request and add some GET by clicking the a link and have something to load variables in your php script.
If you want to test this actually, What you could do is to compare echo session_id(); output from your browser and the pdf you get.
Related
I am running a php code that handles a form and sends you to another page. I am currently using this code as an example once my handling has been done:
echo "Success, redirecting to index.html";
//I then do some stuff with the session here
echo "<script>window.open('index.html', '_self')</script>";
I feel like this is not the most efficient way of running things. Is it possible to open the new page with plain php? (header location whatever doesn’t work because the first echo line defines the page). Or, is there a way to do the same thing with header location whatever and displaying the earlier echo line differently, but still having the same effect?
You can't do it with PHP after data has been sent back to the browser.
I would personally just add a setTimeout() in JavaScript and then run the window.open() after 5000ms.
This is an odd situation and my current thought is that it doesn't work this way, but I need to some other eyes on this.
A different website I don't have control over has a form with a hidden field in it. The form action is a POST and to send it to a url on my website and I need to be able to get the value of that hidden field using javascript.
As a GET that would be included in the url and I think I would just be parsing that apart. But since it's a POST being sent to me I'm not entirely sure how to get the value of that hidden field out.
Is this doable? If so, where should I be looking to do it?
Thank you!
If your server that is receiving the sended form data uses PHP, you can get all form values using:
<?php
print_r($_POST);
?>
If the page in your server is a static html page, then you cannot get the POST data. Or you can, but then you have to make html pages to be executed as php pages (not recommended however).
You talk about that you need this value be accessible by javascript. Simply do something like:
<script>
<?php
echo 'var input_field_value="'.htmlspecialchars($_POST['name_of_input_field']).'";';
?>
</script>
The question doesn't provide information what server software is used, so I assume that is PHP.
EDIT: after Saturnix's comment I added a call to htmlspecialchars() to make it safe to execute in javascript.
Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax? Do I have to return html data like this: Your post is <?php echo htmlspecialchars($_POST["email"]); ?>.<br> If so how do I do that and still keep all the other elements on the page the same way they are currently formatted. Related to this, how do I load the HTML page with some elements already bound to values extracted from the database using an external PHP file?
What is best practice for doing this kind of thing. The objective is to allow a user to input text in a textbox, click submit to have the text stored in a database, and then at the same time update an element that displays the latest sumbmission, kind of like a forum post. I know you can use ajax, javascript, jquery to do this kind of thing on the client side, but I have seen html pages with the <?php echo htmlspecialchars($_POST["email"]); ?> type syntax and wanted to experiment with that for simplicity.
"Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax?"
Answer: No, you must send the data to the same page, ( action="" )
Then include the processing file at the top/beginning of the script. Please feel free to ask for more help.
IF YOU WANT TO SEND THE DATA TO THE DATABASE AND WANT TO DISPLAY THE DATA: Just retrieve the data from your database on the page you want to display it on.
Intro: I've been wandering the internet trying to find a solution to no avail. I'm a PHP noob and my html and css are marginal.
Problem:
I want to create a simple? text box on a website that can be edited and saved and displayed on the website.
If I could avoid PHP or other languages that would be fine but I think the save part is going to require some server site processing. Because I don't expect a large amount of data, storing the text boxes in a xml file or similar should be fine. (no need for a db)
I'm looking for the most simple and hopefully complete solution as I am a rookie with this stuff.
Notes:
I should be able to copy and paste the html code anywhere to create more text/comment boxes. (maybe i need to change a ID or something that's fine)
I hope the comment box has no real text/character limits and any characters or quotes or html or other language is displayed as was written.
It cam be ugly, the focus is functional and simple.
I found this was closest to what I wanted but I could not get it working at all. Maybe its a language my computer or server was not happy with. LINK
Let's start by acknowledging that you will need PHP, and unless you have a proper login system or alternatively a CAPTCHA, this project can be prone to a bot that floods a bunch of messages - I know from experience. All you need to make is an HTML form which will be used to type and submit the text to this public "bulletin board":
<form method='post' action='http://yoursite/newmessage.php' type='submit'>
<input type='text' name='text' placeholder="Type message here"/>
<input type='submit' value='Submit' />
</form>
This form will send whatever the user types in the text input to a PHP file, which will then save it. Now, for something very simple like this you can or cannot use a database.
Benefits of a database:
More secure
If two people try to add a message simultaneously, less issues there
Benefits of a text file
Much simpler to make
Since you are not storing any personal data, a text file will be fine. You can name this whatever you want on the server - it doesn't matter. This will be the PHP file:
<?php
$newText = $_REQUEST['text']; //data submitted from HTML form
$currentText = file_get_contents('data.txt'); //txt file in same folder as this PHP file_get_contents
$concatText = $currentText . '<div><p>' . $newText . '</p></div>';
file_put_contents('data.txt',$concatText);
?>
Lastly, displaying the data on the HTML page is easy. First make sure you are running this on a server that executes PHP, and it'd help to rename index.html to index.php:
<?php
$all_data = file_get_contents('data.txt');
echo $all_data;
?>
Edit I just saw that you want it password-protected. Simply create another text input in the form and in PHP verify that it matches a string.
Client goes to example.com/form.html where a html POST form is displayed
Client fills the form with specific information and submit it to example.com/form.html
When example.com/form.html receives the POST request redirects the Client on example.com/redirected.html
Is possible to retrieve the variables that the client filled and POSTed to example.com/form using javascript ? The javaScript being deployed on example.com/redirected.html only . I presume that can be some "back" controls iframes and ajax involved but I couldn't find a reliable solution yet.
The operation will take place on the same domain so no cross domain issue is involved
Nope, I don't think this is possible.
You have to either
Store the posted value in a cookie
Store the posted value in a session variable on server side
Add the posted value as a GET parameter to the new URL
Use GET to post the original form, and painfully extract the posted value from document.referer on the new page
With HTML5, use localstorage. (The answer describes how to store object in localstorage- you could store an object of your form fields in there).
But you have to store the data on posting with js at example.com/form.html and then can get it on example.com/redirected.html. Without js at form.html, this is not possible with this method.
This works if you plan to use html5 and do not store too much data in it, iirc it has a limit of 5-10mb depending on the browser.
I don't think there is a way to do this by using plain html. With some server-side language (like PHP) it can be done with no problem.
I have been in a similar situation before, and the way I managed to give the data to JS is by including the data in a tag while preparing the output using PHP.
Assuming the redirected to php script receives the POST data from the script it's being redirected in. I would include this in the php code:
<?php
echo '<script type="text/javascript">';
echo 'var postData = '.json_encode($_POST).';';
echo '</script>'
?>
This will have the javascript know what the POST values contained.
To access the values from js:
//assuming you need the value for $_POST['foo']
var foo = postData.foo;
// or if json is encoded as just an associative array
var foo = postData['foo'];
If the POST data is not being passed to the redirected to script (haven't checked if this happens automatically), you could write the $_POST data in a $_SESSION variable, in the first php script:
<?php
$_SESSION['postdata']=$_POST;
?>
and then get it back from SESSION from the redirected to script.
<?php
$postdata = $_SESSION['postdata']; //use this to create the inline script in the html
unset($_SESSION['postdata']; //empty this from the SESSION variables
?>