Setting HTML Attribute via PHP File - php

I'm new to PHP and seem to have ran into a problem I can't seem to get around.
I have a form on a secure page that creates a PHP file to store a text value. I named this variable $text.
The Form HTML Code:
<form action="upload_title.php" method="post">
<label for="text">Title 1:</label>
<input type="text" name="text" id="text"><br>
<input type="submit" name="submit" value="Submit">
</form>
The upload_title.php then seems to store the text input as $text in filename.php:
<?php
$var_str = var_export($_POST['text'], true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
?>
This seems to be functional as the form will generate filename.php, below is an example if I typed Store into the form input and submitted on the webpage.
<?php
$text = 'Store';
?>
Now the issue I'm encountering is not being able to retrieve this stored as a attribute in separate html document, the index.html in my case.
This was my best approach to changing the title attribute of an image:
<a href="upload/1.jpg">
<img src="upload/thumb.jpg" title="<?php include 'filename.php'; echo htmlspecialchars($text); ?>" alt="" class="image0">
</a>
This does not work, but I can see my JQuery detects that this is trying to be populated but does not extract the data from filename.php on the `index.htm' page.
Thank those in advance for your advice and insight, it is sincerely appreciated.

Your issue is probably the fact that you are using an html file instead of a php file, in this case index.html.
Your server is likely not set up by default to process .html files as php so the php does not get executed.
Apart from that it is not a very good way to store your value as when the php does get executed, you introduce a security risk and you use a lot more storage than necessary. You'd better store the value in a database or text file.

Related

saving form to txt. text does not show, why?

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.

How to use wkhtmltopdf with POST data

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.

Form handling in php

I'm trying to set the value in my input text using the get method. However, when I try to render the page, it keep showing my code in the text field instead. Can someone explain what I did wrong?
<form name="quotepage" action="search.php" method="get">
<b>Stock Symbol:</b>
<input type="text" size="8" name="sb" value="<?php echo $_GET["sb"]; ?>" />
<input type="submit" value="Quote" onClick="quotepage.action='search.php';"/>
</form>
When I try to render the page, it will show my code in the value tag in my text field.
If you are seeing an error/warning/notice in your text box, try changing:
<?php echo $_GET["sb"]; ?>
into:
<?php echo isset($_GET["sb"])?$_GET["sb"]:""; ?>
to avoid showing the content if there was no content yet.
And better yet, change it to:
<?php echo isset($_GET["sb"])?htmlspecialchars($_GET["sb"]):""; ?>
to also escape nasty characters such as " that otherwise will break your HTML.
If you are actually seeing <?php echo $_GET["sb"]; ?> inside your text box, then you are having problems running PHP. Check that your script file name ends with .php and check PHP is working on your system.
Do you have a LAMP stack or similar set up?
You need Apache running with PHP installed at the least for this. Also what is this 'onClick="quotepage.action='search.php';"' attribute for?

PHP loading pre-saved form

I have a php page with a form in it.
The main idea here is to have a list of picture that you can select with a checkbox. Client will select the picture he want and submit the form.
I want the information of this form to be save in a file.(a .txt file would do it. I don't want a single table in mysql for each client if possible)
Then when the admin will log in the same page(or client let's say because he is not happy with his choice), I want the form to be filled exactly as it was saved previously.
Honestly, I don't know how to proceed to do that and the way it's gonna be loaded.
I could easily write in a .txt file with php variable but how to load these data and not getting any error if(only example) I remove a file in the folder?
Here's my form with the php code:
<form method="post" action="Exec.php">
<?php
$counter = 0;
foreach (glob("preview/*.jpg") as $file)
{
?>
<div class="photoscontainer">
<a class="big" href="<?php echo $file; ?>"><img src="<?php echo $file; ?>" width="100%" height="auto" alt="Photo <?php echo $file; ?>" /></a>
<br />
<input class="checkbox" type="checkbox" name="Photo <?php echo $counter; ?>" id="<?php echo pathinfo($file,PATHINFO_BASENAME) ; ?>" />
</div>
<?php
$counter++;
}
?>
</form>
You can save users answers as a custom field in the database (by JSONing the form inputs, for example), or even several new fields (one for each form field)... so definitely not using a separate table for each user. That's what databases are for.
If you still wish to use plain text, you can use a CSV (comma-separated values) file, .ini file, or whatever custom structure you want.
So, worthwhile Google searches:
JavaScript serialize form into JSON object
PHP CSV operations (like this and this)
PHP .ini file operations (parse_ini_file should work fine)
PHP file read (file_get_contents will do)
And to avoid file missing errors - simply check if file_exists.
It really doesn't change much either way using a file to store data or a database. A database is generally the preferred method, but you'd only need 1 table with 1 field for each client.
You will want to store the form data in an array/object that you hold in your database or file using serialize or json_encode.
For example:
<?php
if(!empty($_GET)){
$client_form_data = json_encode($_GET);
echo $client_form_data;
}
?>
Then build your form using your stored values.
As far as errors go - you'll need to develop a method to catch potential errors using conditional statements.

Storing and echoing php code in db

I have been trying to test this out. I put in the code below in the database.
<?php
$test = "Whats up";
print $test;
?>
And then on a page i do select and then i try to echo but it won't do it unless i rid the entry of the start and end tags. So if i wanted to execute this php by calling it from the db, how would i do it? Thanks.
Use eval to treat a string as runnable PHP code, for example:
<?php
$string = 'echo "hello";';
eval($string);
?>
As Pekka suggests, this is bad practice and you should probably rethink your solution. There's hardly ever motivated to store code in the database (the database is for data).
you could use serialize - unserialize function to save object or variable. and you can use eval function to run code
Hi This is exactly what I was looking for. We have CMS deployed accross multiple servers and in order to maintain the base functions (without the need to make a url based include of our main functions file) it was easier to store and call these functions from our group=wide single database. We use medium blob and store a serialized base_64 encoded text file there.
So to serialize the code we use this script without the php tags:
<form name="serialise_script" action="" method="post" >
<p>Put Script in here (without php tags???)</p>
<textarea cols="100" rows="25" name="serializeme" ></textarea>
<input type="submit" name="go" value="go" />
</form>
<?php if(isset($_POST['serializeme'])){
echo "<p>Your Code</p><textarea style='border:3px solid black;padding:0.5em;' cols='100' rows='25' >" . base64_encode(serialize($_POST['serializeme'])) . "</textarea>";
} ?>
Then copy the script and paste the output code into a text file.
Upload that to the medium blob table
Then to output again from the database and set to a session variable... I just did this:
eval(unserialize(base64_decode($_SESSION['functions'])));
But it was the eval bit I needed to make it work. By serializing and encoding means we shouldn't have any problems.
Now all I need to do is manage 1 base code for many sites across certain servers. AKA One bug fix would solve all issues across all deployments.
Thanks
Andi

Categories