How to use PHP variables on HTML page? - php

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...

Related

output_add_rewrite_var doesn't rewrite URL

output_add_rewrite_var()
As documented here
This function adds another name/value pair to the URL rewrite mechanism. The name and value will be added to URLs (as GET parameter) and forms (as hidden input fields).
So the following code is supposed to do the trick
<?php
output_add_rewrite_var('var', 'value');
echo 'link';
echo '<form action="test.php" method="post">
<input type="text" name="var2" />
</form>';
?>
On form it works fine, and add a hidden field. But the URL(a tag) is still the same.
http://localhost/test.php
I'm expecting
http://localhost/test.php?var=value
How to fix this?
It seems that if you're using PHP >= 7.1.0 the default tag in url_rewriter.tags is form.
So what you're gonna have to do is change this line in php.ini file:
;url_rewriter.tags
To:
url_rewriter.tags = "a=href,area=href,frame=src,form=,fieldset="
Note: The semicolon at the beginning of the line must be removed

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?

populate form fields with php

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

Referring php variable in html file

Following code is saved as html file:
<?php
$IP = 4;
?>
<textarea name="comments">$IP</textarea>
I want to access the value of php variable in html tags.
Example I want the value of IP to be displayed in textarea.
You can execute .html files as .php files by adding a line to your .htaccess file..
AddType application/x-httpd-php .html
And obviously use the <?php echo $variableName; ?> style syntax
use this:
<textarea name="comments"><?php echo $IP; ?></textarea>
Just put it with PHP
<textarea name="comments"><?php echo $IP; ?></textarea>
it is not possible to access in html, simply save the page in '.php' and if you want to change url, means .html instead of .php then use .htaccess
Here is a short tutorial on using PHP in HTML pages. Note that in order to use PHP in an HTML page, you need to save the page with a .php extension.
to answer your question above, you need to do the following:
<?php
$IP =4;
?>
<textarea name="comments"><?php echo $IP;?></textarea>
<?php $IP =4; ?>
<textarea name="comments"><?php echo $IP;?></textarea>
<textarea name="comments"><?php echo $IP ?></textarea>
Extra cool stuff (for free!!!):
http://php.net/manual/en/install.php
http://www.php.net/manual/en/tutorial.firstpage.php
http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/
etc, etc...

Inserting PHP inside HTML code, ie. inside < ... value="PHP CODE HERE" /> - workaround?

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.

Categories