Well.... I want to know how to make a script to create a html page. So when the script is executed, it will create a new page. Something like this: mydomain.com/test.html . The script should create the test.html page!
Here is my work:
<form action="index.php" method="post">
<input type="text" name="nick" value="Enter NIck" />
<input type="submit" value="Create" />
</form>
<?php
$nick=$_POST['nick']; // get the users input
$fh=fopen('$nick' , 'W') // Create the page
$contents= include 'sys.php';
fwrite($fh, $contents);
fclose($fh);
echo "Redridectring to your page....";
header('Location: $nick');
?>
Now, what this code should do is create a page, which is called
$nick. So if the input is "bleugh", the $nick should be $nick="bleugh".
Then it will create the page "bleugh", include sys.php, then header to the
page. Is this wright? Will it work?
You cannot use PHP variables that way inside single quotes
Is OK:
"{$nick}"
Won't Work:
'$nick'
But am not sure what you're trying to do with "$contents= include 'sys.php';"? Perhaps you want to capture the output of sys.php into a string? Either way, you need to be careful with vulnerabilities you are opening yourself to.
I think your method by creating a file and then redirect to the file is basically okay. Additional comments:
Make sure the PHP process has write permission to the folder you write into.
Do you really have to include sys.php? You need to write proper html tags into $contents when you really want to create a HTML file.
You should perform redirect before outputting any character, so your redirect would not work because it is preceded by an echo operation.
try this
header('Location: '.$nick.'');
Related
I have a form on my website. I wanted to save to input to txt but when I submit data, the .txt file get larger (bytes increase) but no text shows up,
Here is the code
<div>
<form action="controller.php">
<input name="card" id="card" type="email">
<button type="submit" name="submit" >Submit</button>
</form>
</div>
Here is the .php code
<?php
$card = $_POST['card'];
$file = fopen ('file.txt', "a");
fwrite($file, $card . "\n");
fclose($file);
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
I have a sub domain with the same code and it works perfectly fine.
why is it not working and how do I fix?
I tried to change the id and the type
You have a very simple issue here. Let's see why by trying to do this on your file:
print_r($_POST);
You will see that after posting your form, you have no POST data. Your form is, by default (on your server or PHP configuration) sending the data not using the POST method, but the GET one (your other server probably is setup the opposite way).
To fix this, you can either change your $card variable to:
$card = $_GET['card'];
Or rather, and that would be a better option to make it more clear and avoid problems if you migrate your website on another server/PHP version, you could simply specify the method on your tag:
<form action="controller.php" method="post">
Please, don't forget to secure the data that will be written in this file, malicious users exist and if you keep your code that simple, it might be a serious security issue.
I currently use wkhtmltopdf where I am attempting to generate a .pdf file after a user submits a form on our website. The form data gets submitted to post.php, where it displays nicely as a formatted web page. I want to generate a .pdf file of this exact page.
But the problem begins when trying to execute wkhtmltopdf. I get a continuous loop because I'm trying to generate the .pdf from inside of this post.php file, which is also the target.
I have also tried to include() a separate PHP file to handle the exec() function, but I still get a continous loop.
Maybe a visual below helps:
form.php which contains something like below...
<form id="ecard" action="post.php" method="post">
<input type="text" name="ecard_message" />
<input type="submit" />
</form>
post.php which holds the posted data and contains HTML like so...
<div class="content">
<?php echo $_POST['ecard_message']; ?>
</div>
<?php exec("wkhtmltopdf http://example.com/post.php ecard.pdf"); ?>
My code DOES work when the exec() function is runs separately from these files, but how would I accomplish the exec() within this same process, automatically?
Any insight is very much appreciated.
calling wkhtmltopdf http://example.com/post.php ecard.pdf will lose the post data, so even if it worked, the pdf will be empty.
Rather generate the pdf as html and then pass it to wkhtmltopdf. Eg: (untested)
<?php
$html = '<div class="content">' . $_POST['ecard_message'] . '</div>';
exec("echo $html | wkhtmltopdf - ecard.pdf");
?>
See Is there any wkhtmltopdf option to convert html text rather than file? for explanation of using text instead of files.
I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document
I have the following script which takes data from the users and writes into a file using PHP.
The issue that I am facing is when i direct the page using "form action" the page gets redirected but the rest of the script is not executed meaning no new data is written to the file, but when I leave the Form action blank the data gets written.
Below is the code:
<html>
<body>
<form action="page.php" method="post">
<input type="text" name="text_box"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) { //only do file operations when appropriate
$a = $_POST['text_box'];
$myFile = "t.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $a);
fclose($fh);
}
?>
You're submitting your form to page.php, but your form handling logic is found on index.php. That's what's causing your problems.
Either submit to index.php rather than page.php, or simply move the form handling logic to page.php.
PHP won't save it automatically. You need to submit the form for it to then execute with given data.
Also, your fopen is using 'w' which truncates the file, then writes to it. So every time you save, the new data isn't appended onto the end, it replaces the current data.
Also, is this file called 'page.php'? The only other solution I can think of with your code is to move the PHP code to the top of the file, before the <html>.
When somebody submits the form, it will take them to 'page.php'. Whatever PHP is in that file will be executed. Any PHP in the file above (i.e. not in page.php) won't be executed - it will never be read.
Just an aside: I would check out a PHP framework like Yii or CodeIgniter to help you here. Handles a lot of these problems for you.
<?php
(E_ALL & ~E_NOTICE);
session_start();
// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
// not logged in, move to login page
header('Location: login.php');
exit;
}
else {
echo "Welcome To The Test Page:";
echo $_SESSION['logname'];
}
if (isset($_POST['submit'])) {
test();
}
function test()
{
$var = rand(1, 5);
header("Location:{$var}.html");
exit;
}
<html>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="submit" value="Take Test">
</form>
</body>
</html>
When the "take test " button is clicked i need 1 of the 5 html pages i.e (Question papers in this case ) to be displayed to the user.
for that i have created the random number from 1 to 5.
The pages have been names 1.html , 2.html and so on...
Could anyone debug this code ??
Other comments have addressed two problems: output before changing headers, and the invalid formatting around the quotes.
Another problem is that $_POST['submit'] is never specified from the HTML. You have:
<input type="submit" value="Take Test" />
But nowhere is a name for the field specified. This should read:
<input type="submit" name="submit" value="Take Test" />
You have extra quotes in
header('"Location:".$var.".html"');
It should be
header('Location:'.$var.'.html');
or if you prefer double quotes as mentioned in the comment to this answer you can use the php double quote variable interpolation:
header("Location: {$var}.html");
The string your would set the header to would be:
"Location:".$var.".html"
instead of what you actually wanted
You need to exit; after you send Location header. Otherwise the redirect will not happen.
header("Location:".$var.".html");
exit;
You can't call header() after your wrote something in your page.
Here, when you're successfully logged on, you echo a message and then try to change the header. But it's too late.
From the php doc :
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.
If you want instead to include some page content, then include it, don't change the Location in the header.
Plus, as others pointed out, your string for the header is wrong, "Location:".$var.".html" will be enough.
It appears you declared test function after the call, so you must it's better practice to move it before the call, I would put it before session_start();
You also have to avoid output, in order to get header to do what you want. You can do this by putting an exit(); after the header instruction.
Also you have extra quotes as GWW said in his answer.
Update
Well, I didn't notice this before, in PHP you can call functions before the declaration, although this is currently confusing me a little.
You can check if form is sent by
if($_SERVER['REQUEST_METHOD'] == 'POST') {
test();
}
try pointing the form to the page manually instead of using the server var.
from
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
to
<form action="test.php" method="post">
or even
<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">