I have a html form with a textbox in which i want to pull the name of the person signed in to automatically be filled in the text box when the page loads. below is the code i am using.
<input type="text" autocomplete="off" name="createdby" id="createdby" value= <?= $first_name.' '.$last_name ?> readonly>
the issue is with the php code
<?= $first_name.' '.$last_name ?>
Because of the space between .' ' . the last name does not show. however when i remove the space both the first name and the last name show but without any spaces. How do i pull both names from the database and have them separated
I believe it needs to look as follows:
<...value="<?= $first_name.' '.$last_name ?>" readonly>
You should use double quotes around your php tags.
implode() looks better then such concatenation:
implode(' ', array($first_name, $last_name));
Convert this:
<?= $first_name.' '.$last_name ?>
Into this:
<?php echo $first_name.' '.$last_name ?>
Or this if you want to simplify variables concatenation:
<?php echo “$first_name $last_name” ?>
Use the standard php tags and explicitly echo your values
Related
I have made a page in HTML/PHP and I would like a function to be able to edit some files though the web-page. I have a <textarea> tag to do this.
However, I cannot seem to enter any default value to the <textarea> through PHP variables.
Here is my code
<?php
$text = file_get_contents("file.txt");
?>
<textarea name="input">
<?php echo $text; >?
</textarea>
But the text that appears is "<?php echo $text; ?>"
Please help, I couldn't find help anywhere else.
You misplaced your ending PHP tag (>? should be ?>) when echoing the result into the textarea.
<?php
$text = file_get_contents("file.txt");
?>
<textarea name="input">
<?php echo $text; ?>
</textarea>
You should check your php knowledge:
line 5 should be ?> not >?. Then it will be valid.
Is it possible to insert an active link to an input textbox?
I tried using an <a> tag inside the value of html but its not working.
<?php $email = "example#link.com "; ?>
<input type="text" id="email" name="email" value="<?php echo $email; ?>">
It only returns the text without the hyperlink value.
A couple things are wrong here...
You're not escaping your quotes. Therefore the PHP is invalid.
You're trying to put HTML inside a attribute, which is also invalid.
The only alternative I could see being used here is an HTML element with contenteditable="true" applied. This makes it so an element (per say a <div>) can have it's content be modified.
<?php $email = "example#link.com "; ?>
<div id="fake-email" contenteditable="true"><?php echo $email; ?></div>
Then see this related question if you're doing a form.
Edit:
If you're trying to do a form, then this is one example:
document.getElementById("form").onsubmit = function(){
document.getElementById("email").value =
document.getElementById("fake-email").innerText || document.getElementById("fake-email").textContent;
}
While your form is:
<form action="..." method="..." id="form">
<div id="fake-email" contenteditable="true"></div>
<input type="hidden" id="email" name="email" />
</form>
No, it isn't possible. Input values will always be rendered as plain text. If the user doesn't need to edit the link I would just put it beside the input.
Otherwise you might want to look into WYSIWYG Editors. Links to two of the most popular below.
TinyMCE
CKEditor
You need to escape quotes when including it in your php variable.
<?php $email = "example#link.com "; ?>
You need to use a backslash when you're using double quotes.
Alternatively, you can write it as such:
<?php $email = 'example#link.com '; ?>
If you start with single quotes, then you don't need to escape the double quotes. \
I strongly suggest you read up on escaping characters when need be.
This is my code for texarea that i have.
<textarea class="text" id="post_content_auto" name="post_content_auto"><?php echo (!empty($car_info['post_content_auto']) ? $car_info['post_content_auto'] : ''); ?></textarea>
I want to echo that on some other page and for that i use this code :
<?php echo strip_tags($car_info['post_content_auto']); ?>
But it won't echo text. I tried to change id and name but still no result.
You need to wrap that form element in a form, give it the post method, and an action that points to your PHP file. In your PHP file, access that variable as $_POST['post_content_auto']
Hello i'm currently using a table to input values into a custom meta field. I have a text box called episode title. My problem here is that if the characters ' " are added in the field then everything goes in to chaos. I want to use the htmlspecialchars to input the values as " and ' instead of ' ". the below code does not work to covert the characters. Can anyone please help?
<p>
<input type="text" name="episode_title[]" id="episode_title[]" value="<?php echo ($_POST['episode_title']); ?>" class="title regular-text" style="width: 98%" />
<span class="description"><?php _e('Title of The Episode.'); ?></span>
</p>
add this to the htmlspecialchars call: ENT_QUOTES like so:
<?php echo htmlspecialchars($_POST['episode_title'], ENT_QUOTES); ?>
This will enable changing of both the " and the ' quotes
$_POST['episode_title'] is an array, so you need to get the right value from the array and use htmlspecialchars() on that value.
Something like:
value="<?php echo htmlspecialchars($_POST['episode_title'][$some_key]); ?>"
Edit: I am assuming that the $_POST array contains the results of the form when it is submitted.
i have the following code :
<input type="text" value="<?php echo $_GET['msg']; ?>">
This input is automatically filled with the name that is writen in the previous page.
So, if the user wrote : i like "apples" and banana
The input will be broken because it will close the tag after the double quotes.
I know i can avoid that by html entiting the value, but i don't want this, is there another solution or is there an <<< EOD in html ?
Thanks
htmlentities() / htmlspecialchars() is the standard way for this. You should use it.
You can always decode the entities before you send them by E-Mail, or do something else with them using html_entity_decode().
You should use the htmlspecialchars function, to escape the output for HTML :
<input type="text" value="<?php echo htmlspecialchars($_GET['msg']); ?>">
Note : you might have to add some additionnal parameters, if you are not using ISO-8859-1 as charset ; for example, with UTF-8 :
<input type="text" value="<?php echo htmlspecialchars($_GET['msg'], ENT_COMPAT, 'UTF-8'); ?>">
One function or another will cause some kind of trouble.
I came up with the following to keep the ampersand:
<input type="text" value="<?php echo parseString($_GET['msg']); ?>">
<?php
function parseString($str) {
$result=str_replace('"','"',$str);
$result=str_replace("'","'",$result);
return $result;
}