running php script to convert html to pdf - php

I have a form which I want to convert to pdf. I want to use fpdf to do that
My html code is as follows:
page.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<h1 align='center'>Welcome </h1>
</header>
<section align = 'center'>
<h2> Register </h2>
<form action = "form.php" method = "post"
<p>
<label>First Name :</label>
<input type="text" name = "first_name" />
</p>
<p>
<label>Last Name :</label>
<input type="text" name="last_name" />
</p>
<p>
<label>Gender :</label>
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</p>
<p>
<label>Date of Birth :</label>
<input type="date" name="dob" />
</p>
<label>Mobile :</label>
<input type="text" name="mobile" />
</p>
<p>
<label>Email :</label>
<input type="text" name="email" />
</p>
<input type="submit" value="Register" name="submit" />
</form>
</section>
</body>
</html>
I have created a php script to convert the form to pdf once the register button is clicked the php is :
form.php:
<?php
if(!empty($_POST['submit']))
{
$f_name=$_POST['first_name'];
$l_name=$_POST['last_name'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",16);
$pdf->Cell(10,10,"welcome {$f_name}",1,0,C);
$pdf->Cell(50,10,"Name :",1,0);
$pdf->Cell(50,10,"$l_name",1,0);
$pdf->Cell(50,10,"gender :",1,0);
$pdf->Cell(50,10,"$gender",1,1);
$pdf->Cell(50,10,"Mobile :",1,0);
$pdf->Cell(50,10,"$mobile",1,1);
$pdf->Cell(50,10,"Email :",1,0);
$pdf->Cell(50,10,"$email",1,1);
$pdf->output();
}
?>
I am running this on XAMPP and have created a folder called demo in htdocs(where the html file, php file and fpdf folder is there), however when I run html file on browser and click register, it simply displays the php code in the php file rather than generating the pdf
The files are in htdoc of XAMPP files in folder demo like this:
When I run page.html(click on register):
It shows me this:
Why is it not generating a pdf?

You need to output to a file:
string Output([string dest [, string name [, boolean isUTF8]]])
Description
Send the document to a given destination: browser, file or string. In the case of a browser, the PDF viewer may be used or a download may be forced.
The method first calls Close() if necessary to terminate the document.
see http://www.fpdf.org/
Look for the Manual menu
Description
Send the document to a given destination: browser, file or string. In the case of a browser, the PDF viewer may be used or a download may be forced.
The method first calls Close() if necessary to terminate the document.
Parameters
dest
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
name
The name of the file. It is ignored in case of destination S.
The default value is doc.pdf.

Related

Unable to write to txt file in php

I'm trying to simply overwrite a txt file with a number entered in a text box in PHP. Both the PHP and text file are in the html directory of my apache2 server. Every time it executes it just displays the die() string. I also tried using fwrite() with the same results. Any help would be much appreciated.
<HTML>
<TITLE>Home Automation Interface</TITLE>
<BODY>
<H1>Air Conditioning Power(Relay 1)</H1>
<H2>Turn AC On/Off</H2>
<H3>
<p>
<form action="relayToggle.php" method="get">
<input type="submit" value="AC Power">
</form>
</p>
<form method= "post" action = "homeInter.php">
<p>
<label for="acTemp">AC temperature (F):</label><br/>
<input type="text" id="temp" name="temp">
</p>
<button type="submit" name="submit" value="send">Change Temperature</button>
</form>
<p><strong>
<?php
$temp= $_POST['temp'];
echo $temp;
file_put_contents("/var/www/html/temp.txt", (string)$temp) or die("Unable to open file!");
?>
}
}
?>
</strong>
</p>
</H3>
</BODY>
</HTML>
Wrap your PHP code Ina valid checker to see if form was submitted
Like this:
if ($_POST):
//All functions go here
endif;
If the file is in the same directory as your PHP file just change the directory to the file name.file Extension in your file_put_content()

CSS How to keep bodies of html file and included files from merging?

From what my trial and error has shown me is that when I (using php) include another php file, the bodies basically merge. I have a CSS file that effects the main files body with a class of "main". The CSS file also effects the included files body with a class of "nav". It appears that both the bodies of each file are effected by both of the styling that targets each class (even though the classes on each of the body tags are different). Am I mistaken? If not, is there a way I can get around this problem? Thanks!
Included nav file (which includes the css file (I only show the code relating to the body):
<body class="nav">
<ul class="navbar">
<li>
<form action="login.php" method="post">
<input id="email" type="text" name="email" placeholder="Email">
<input id="password" type="password" name="password" placeholder="Password">
<input type="submit" value="Login">
</form>
</li>
Main index file that includes the nav file :
<body class='index'>
<div class="block-signUp">
<h1>Sign Up</h1>
<form action="" method="post">
<input class="firstname" type="text" name="firstname" placeholder="First Name"
value="<?php echo($_POST['firstname']) ?>">
<input class="lastname" type="text" name="lastname" placeholder="Last Name"
value="<?php echo($_POST['lastname']) ?>"> <br>
<input class="email" type="text" name="email" placeholder="Email"
value="<?php echo($_POST['email']) ?>"><br>
<input class="password" type="password" name="password" placeholder="Password"
value="<?php echo($_POST['password']) ?>"><br>
<div class="error-message hidden"></div>
<input type="submit" value="Sign Up"><br>
</form>
<div>Already Have An Account?</div>
</div>
</body>
The only CSS File in the whole website, which the nav file contains :
body.nav { /* This code effects the body in the index file as well for some reason */
margin-top: 150px; /* Make nav bar at top of screen */
}
incorrect. First of, include and require is injection a script from one file to __FILE__(Master file, the file opened in .htaccess/URL) for example:
file1.php
<?php
$first_script_var = true;
include 'file2.php';
?>
file2.php
<?php
if(isset($first_script_var) && $first_script_var === true){
echo 'True';
}
?>
Now file1.php would look like this:
<?php
$first_script_var = true;
include 'file2.php';
?>
<?php
if(isset($first_script_var) && $first_script_var === true){
echo 'True';
}
?>
But if you would be using AJAX requests to the HTML DOM Element you would be requesting a file to the `html` tag it would load in the body tags.
Example: <
file1.php
<html>
<head>
<title>Hello world</title>
<script src="jquery.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$.ajaxSetup({cache:false});
});
function load(){
$("html").load("file2.php?string=" + document.getElementById("str").value); // $("html") is a selected, it basicly means we have selected the html tag. and the .load(); is a function from JQuery.
}
</script>
<div>
<input type="text" id="str" placeholder="Echo 'text'" />
<br />
<input type="button" onclick="load();" />
</div>
</body>
</html>
file2.php
<head>
<title>New title</title>
</head>
<body>
<?php
if(isset($_GET['string'])){ echo $_GET['string']; }
?>
</body>
this will result in replacing old header and old body tag with new head and new body tag, remember! Old javascript functions will still be remembered.
If you have CSS declared in both include files and the styles are not scoped specifically to the elements on that page they you can get conflicts. This can especially happen if you are styling base tags like body, span, div, table, etc.
When the page is compiled in the browser everything gets thrown together. Long story short. Depending on order the CSS falls in the code it may get overwritten. Also depending on the specificity (real word) of the style it again may get overwritten.
This may help with that.
CSS Specificity Calculator
Be sure that your main and nav class are not on both or especially being styled on both pages
Another thing I see some people do that are new to MVC (model view controller) and calling partial pages is that they include the HTML and BODY tags in the page that is being called in when it's not needed.

Server Won't Display Body of HTML or PHP

I'm using GoDaddy's CPanel hosting and just finished developing a submission website locally however when I upload it to the server, it won't display the body of the index.php file.
I don't think it has to do with the index.php file because verything works fine locally on localhost with Wamp however my CPanel server will refuse to work. http://submit.arhsj.com/ is where the index.php is. You can see the title of the page has changed and you can Inspect Element to see that the head contains stuff however the body is empty.
Here are the files located in the submit.arhsj.com folder.
I can provide more information if you think it's required. I'm just really confused as to why it's not working when it works perfectly on localhost. Even the HTML form in the middle of the 2 PHP sections won't display.
index.php code:
<!DOCTYPE html>
<html>
<head>
<title>Submission Website</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<?php
require('/recaptcha/src/autoload.php');
require_once "recaptchalib.php";
$siteKey = 'XXXXXXXXXXXXXX';
$secret = 'XXXXXXXXXXXXX';
$lang = 'en';
$response = null; // empty response
$reCaptcha = new ReCaptcha($secret); // check secret key
if (isset($_POST['g-recaptcha-response'])) { // if submitted check response
$response = $reCaptcha->verifyResponse(
$_SERVER['REMOTE_ADDR'],
$_POST['g-recaptcha-response']
);
}
if ($response != null && $response->success) {
echo "Thanks for your submission!";
} else {
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to submit*:
<input type="file" accept=".gif,.png,.jpg,.jpeg" name="submission" id="submission" required>
<br>Allowed file types: .png .jpg .jpeg .gif
<br><input type="text" name="first_name" id="first_name" placeholder="First Name*" maxlength="56" required>
<br><input type="text" name="last_name" id="last_name" placeholder="Last Name*" maxlength="56" required>
<br><input type="text" name="email" id="email" placeholder="Email Address*" maxlength="128" required>
<br><input type="checkbox" id="rights" value="rights" required>I created this meme or found this meme online but have not changed anything (such as removing watermarks).
<br><input type="checkbox" id="age" value="rights" required>I am 13 years of age or older.
<br><br>By clicking "Submit", you have read, accepted, and agreed to adhere to our Terms of Submission.
<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXX"></div>
<input type="submit" value="Submit" name="submit">
<br><br>* Required
</form>
<?php
}
?>
</body>
</html>
If who post give some answer maybe somebody can help him but here semms become a "
last chance" :)
The page is crashed this is sure how is sure that is crashed from the php code.
Maybe the code start with an invisible byte order mark (BOM). So try to open with your editor or notepad++ setting the encoding to UTF-8 (no BOM). Other problem could be the path so the require try in this way and if work you can check the reason.
require $_SERVER['DOCUMENT_ROOT'] . '/recaptcha/src/autoload.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/recaptchalib.php';
Anyway and repeat this point if you post try to follow the reply maybe somebody can help you.

Attempting to save user input via HTML form and then refer back to it later

I am currently in the process of designing a Computer Aided Learning package. The welcome screen simply requires the user to type in their name into a text field and click a button which directs them to 'subject selection' page. How would I go about storing the user input and how, if possible, could I refer to the stored value on a seperate html page?
<div id="inputbox">
<form>
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
HTML:
<div id="inputbox">
<form action="script.php">
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
That will send the form to script.php (if no 'action' defined, it will send the form to the very same page, if that html is inside script.php then it will work the same, and if no "method" is defined, if will use GET, you can set <form action="script.php" method="POST"> to change this):
script.php:
<?php
session_start();
$_SESSION['name'] = $_GET['name'];
?>
and then you reuse that variable in the same session, remember to always invoke session_start() before any output in your php.

edit text from/to an XML file : it works in localhost but not on internet? (php, xml)

i try to edit the text of an XML file from my page edit.php. It works fine if i test it in "localhost", the xml file is updated as expected.
But, when i put the files on internet, and i try to edit the text from there, it seems to be ok, but then i refresh the page, and the text in the edit.php is still the same as before, and nothing has been updated. Do you know where this comes from? is there a problem with my "form" ?
here is my code :
<body>
<?php
/* READ */
$dom = new DomDocument();
$dom->load('edition.xml');
$_haut = $dom->getElementsByTagName('haut')->item(0);
$haut = $_haut->firstChild->nodeValue;
/* WRITE */
if ( isset($_POST['cache']) ){
$haut = stripslashes($_POST['haut']);
$_haut->firstChild->nodeValue = $haut;
$dom->save('edition.xml');
}
?>
<div>
<h4 style="float:left;">Update the text</h4>
<div style="clear:both;"></div>
<form method="post" action="edition.php">
<p>
<label for="textarea1">the text : </label><br />
<textarea rows="14" cols="80" name="haut" id="textarea1"><?php echo $haut ?></textarea>
</p>
<input type="hidden" id="cache" name="cache"/>
<p><input type="submit" value="Envoyer" /></p>
</form>
</div>
</body>
Thanks for your help
My best guess is the user php is running as does not have the permissions to do so.

Categories