I trying to put user data from PHP in HTML form, like name, email, address, etc. But i don't know what i have to do to work it.
Already tried:
<?php
session_start();
$test='some name';
$_SESSION['name']='some name';
?>
<a href="<?php $_SESSION['name']; ?>"></a>
<!-- AND -->
<a href="<?php $test; ?>"></a>
and nothing happened.
SOLVED CODE:
<?php
session_start();
$test='some name';
?>
<a><?= $test ?></a>
CodePudding user response:
The PHP manual has a page introducing the PHP tags:
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them.
Note that "interpreting the code" is not the same as displaying the result of that code. For instance, you could write <?php $test = 'hello'; ?> to assign a new value to a variable, and it won't cause anything to be output.
To output a string, you can use the echo and print keywords. Note that these are not functions, so do not have parentheses around their arguments; as noted in the manual, including redundant parentheses will sometimes work, but can be misleading. So the following output the variable $test into the page:
<?php echo $test; ?>
<?php print $test; ?>
As the PHP tags page I linked to earlier says:
PHP includes a short echo tag
<?=which is a short-hand to the more verbose<?php echo.
So you can also use:
<?= $test ?>
However, you also have a second problem, which is nothing to do with PHP, and will happen if you hard-code a URL into your HTML like this:
<a href="https://example.com"></a>
Nothing will appear in the browser! Why? Because you've defined the destination of a link, but haven't defined any text or content to click on; you need it to look like this:
<a href="https://example.com">Click me if you dare!</a>
So put that together, you might write this in PHP:
<a href="<?= $linkUrl ?>"><?= $linkDescription ?></a>
CodePudding user response:
Change this
<a href="<?php $test; ?>"></a>
to this
<a href="<?php echo($test); ?>"></a>
or to this if you want to see the data on the html page
<a href="<?php $test; ?>"><?php $test; ?></a>
<a href="<?php echo($test); ?>"><?php echo($test); ?></a>
Do note that there are better ways to display such data. Check this out: https://www.w3schools.com/php/php_echo_print.asp
