I have a form for users to fill out information and am wondering how to retain the previously typed information if the user redirects back to the page.
So, I have two files,
form.html
validate.php
Under form.html:
<form id="regForm" action="index.php?validate" method="post" onsubmit="return regValidation();" >
<tr>
<td width="150px" >First Name: <font color="red">*</font> </td>
<td><input type="text" name="firstNameField" id="firstNameField" value/ ></td>
</tr>
and under validate.php, I have stored the info with $_SESSION:
<?php
session_start();
$_SESSION['first'] = $_POST['firstNameField'];
?>
but when I tried to populate the firstNameField with $_SESSION['first'] as follow
<td><input type="text" name="firstNameField" id="firstNameField" value="<?php echo isset($_SESSION['first']) ? $_SESSION['first'] : NULL; ?>" /></td>
The field will literally be replaced with <?php echo isset($_SESSION['first']) ? $_SESSION['first'] : NULL; ?>
Can someone tell me why and how to properly fix it?
Thanks.
Change the file name to form.php instead of form.html so Apache knows to parse PHP inside that file.
Alternatively you can modify your .htaccess file and add a line like this:
AddType application/x-httpd-php .html
This tells Apache to to render all files that end in HTML using PHP.
Why make this complicated by having two files?
Just have the one.
The first time around nowt is filled in as the $_POST has nothing in it. Subsequent times that variable will have the appropriate value.
This will save you a lot of heartache in the future as you do not need to keep two files in step.
Also it removes the complexity of using sessions.
By default, any file with the file extension of .html will not be parsed, therefore exposing <?php $yourCode->exposed = true; ?>
If you rename the same file, changing its file extension from .html to .php, then
<?php echo 'HelloWorld'; ?> will be parsed, and the end result is HelloWorld
This default behavior can be overridden as described above by Tim
Change the file name to form.php instead of form.html as is the php tag
or add .html handler
Related
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.
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?
I've been trying to use PHP to allow a user to put in articles onto a certain site via a form.
The site,my code and my skills as a programmer are primitive in nature.
The user enters the input here:
<form action="articlepro.php" method="post">
Title:<input type="text" name="title"><br>
Sub-Heading:<input type="text" name="subhead"><br>
Intro:<textarea name="intro" rows="10" cols="40"></textarea><br>
Heading-1:<input type="text" name="head1"><br>
Content-1:<textarea name="cont1" rows="10" cols="40"></textarea><br>
<input type="submit">
</form>
The date is processed as such by the file articepro.php:
<html>
<body>
<?php
$title=$_POST["title"] . ".php";
$createFile=fopen($title,"x+");
$content=file_get_contents("articletemplate.php");
echo $createFile;
echo file_put_contents($title,$content);
echo $createFile;
?>
</body>
</html>
The aim of the code above is to first create a variable that stores the title from the form with the .php extension.Then the code creates a file with the same name as the $title variable.$content stores the html and php code of the file "articletemplate.php" as a string and "file put contents" puts that template into the file created earlier.
It may be of of value to know that artictemplate.php contains the template I wish for all new articles to have with bits of php that put in data as such:
<p><h1><?php echo $_POST["head1"];?></h1>
<?php echo $_POST["cont1"];?>
</p>
Now the problem I face is the fact that my code both creates the required files with the required title(Permisson:644),it also contains the intended template.BUT,I am unable to display anything when I open the created files(.php's) via URL.I either see a blank page or "k????????????????????????????????????????????????????????????????????????????????????????ä???????????????ä??"
I can open them if I download the created files off the server and change the extension to .html(cant use PHP then) though but no luck with .php or .phtml.
Webserver:Apache/2.2.24 (Unix
I am designing one HTML form with index.html page. Default values are on PHP page, like name, for form. Now my query is how can I take these values on to the HTML page?
For example :
default.php
$name="poorna";
...
...
in index.html
<form>
<input type="text" value="<?php echo $name; ?>" >
</form>
How can this be possible ?
quick and dirty:
change index.html to index.php
<?php include 'default.php'; ?>
<form>
<input type="text" value="<?php echo $name; ?>" >
</form>
hope it helps
for a shorter version of echo you can use <?= $name ?>;.
Notice:
the short tags only work if you set the correct setting in the php.ini
$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = On //Default Off
If i am not wrong since PHP 5.4 this option is set to On by default
Without .php extension server dont accept php tag. Save the file as php is better...
I have custom fields defined on wordpress in which I have entered an episode number for each post I have written. I want to use this value in a form, but I need to keep the form code all the same and just call it using shortcode in each post since there are way too many posts to edit manually.
I have tried
<input type="hidden" name="Episode" value="<?php the_meta(); ?>"/>
to set the value of Episode inside the form as the value for that specific post, but the php code is still able to be seen on the page source code so it is not going through. Is there a workaround?
You have to echo the response.
<input type="hidden" name="Episode" value="<?php echo the_meta(); ?>"/>
Of course you can also use print functions.
Although the above answer(by jjs9534) is true, you typically need to echo the_meta, it is not the problem, the problem is that your files are not being parsed by PHP, you either need to rename your extensions to .php or mess around with .htaccess(on Unix) server web.config (on Windows) to add the extension you are using for your pages to be parsed as a php file... so in .htaccess, to parse .htm or .html files as PHP you would add:
AddHandler application/x-httpd-php .html .htm
You need to echo the value of the_meta() for it to appear on your page.
Try this
<input type="hidden" name="Episode" value=<?php echo "'" . the_meta() ."'"; ?>/>
Edit:
I changed the code slightly. Try that.