Inserting PHP variable into Html5 "Placeholder" attribute - php

I tried the following, and it shows an empty text area, the value isn't displayed:
<input type="textarea" class="class1" name="name1" placeholder="<?= $val1?>" />
Any thoughts on what should be done so it will work?
Update: It is now working, the value was empty, therefore text area didn't show anything. The code can be used safely.

<input type="textarea" class="class1" name="name1" placeholder="<?php echo $val1; ?>" />
There is no such input type attribute as textarea.
Do you mean:
<textarea class="class1" name="name1" placeholder="<?php echo $val1; ?>"></textarea>

if $val1 is not null, i think the problem is short tag (<?= ?>).
You can override this config, at the top of file, put this line to enable short tag:
ini_set('short_open_tag',1);

Make sure that $val1 actually contains a non-empty value. If it does, your code should create a non-empty placeholder attribute.

Check whether the variable is empty (as #Asaph suggested in his comment).
Entered a value and now it is working just fine.

Related

PHP keep input type=date value after form submit

i want to keep my input type=date value after onchange form submit
Current HTML code
<input type="date" id="Date" name="Date" placeholder="Date" value=”<?php echo $_POST['Date'];?>” onchange="this.form.submit();">
So after onchange, I want the value of the date remain same. I tried many ways but it didn't work. Thanks for helping
Please check with this, I am seeing issue with quote as well in your HTML, so copy below code to see.
<input type="date" id="Date" name="Date" placeholder="Date" value="<?php echo isset($_REQUEST['Date'])?date("Y-m-d",strtotime($_REQUEST['Date'])):'';?>" onchange="this.form.submit();">
You're not using standard double quotes ", but a curly variant, which are not part of the HTML standard:
value=”<?php echo $_POST['Date'];?>”
Replacing those curly quotes by straight ones will fix your problem.

urlencode() not encoding inside value attribute

I know that urlencoding is used for url's but in the case I need to use it for a post value.
The problem that I am having is when I try encoding it inside the value attribute it doesn't do the encoding.
But when I encode the variable outside the value attribute and then just use the variable it works.
I am really confused on why that's working but when I use urlencoding in the value attribute its not working.
Here is my code. Any help would be really appreciated.
Not encoding
<input type="hidden" name="proj" value="<?php echo urlencode($encrypted_params); ?>">
Encoding
urlencoding($encrypted_params);
<input type="hidden" name="proj" value="<?php echo $encrypted_params; ?>">

Textarea removes data from db

i want to create a textarea, which will store information already inserted into the database and can be updated by a user but everytime i add elements to the query it removes the already inserted data. But when i add elements to the query, it keeps the data intact.
here is the code that keeps the data
<input name="mobile" type="text" class="field span6" rows="6" placeholder="Mobile" value="<?php echo !empty($mobile)?$mobile:'';?>"/>
and here is the code that removes it
<textarea name="mobile" type="text" class="field span6" rows="6" placeholder="Mobile" value="<?php echo !empty($mobile)?$mobile:'';?>"></textarea>
i am really puzzled as to why the textarea elements removes the data, keeping the textarea elements with the data still in tact will be a great help.
Thank you.
You are mis-using the textarea tag. Unlike inputs it requires an opening and closing tag with the value of the textarea defined between the tags rather than through a value attribute.
Try:
<textarea
name="mobile"
class="field span6"
rows="6"
placeholder="Mobile"><?php echo !empty($mobile)?$mobile:'';?></textarea>
Also, unless you're checking the value of $mobile elsewhere, I'd recommend outputting it with htmlentities() to prevent HTML injection.

Extra Tabs in Input Fields after retrieving from Db

I am trying to retrieve data from the db in an input field through PHP. Instead of the placeholder, I want to display this retrieved value in the input field if the data exists. I am able to retrieve the data from the db, but it is adding few extra tabs before the actual input.
<input type="text" name="title" id="title" value="
<?php
$title='title';
$clean = trim(retrieve_project_info($email,$title));
echo $clean;
?>">
So, if the input in the db is 'coldplay'. the retrieved data looks like '[tab][tab][tab]coldplay' and the url has many %09s in it.
I have removed whitespaces by using the trim method. But, trim does not remove tabs.
Any idea how to remove these tabs?
Yes, removing tabs is easy, but you better find out why they are there instead of removing them.
But your code:
<input type="text" name="title" id="title" value="
<?php
$title='title';
$clean = trim(retrieve_project_info($email,$title));
echo $clean;
?>">
would be better written like this:
<?php
$title='title';
$clean = trim(retrieve_project_info($email,$title));
?>
<input type="text" name="title" id="title" value="<?php echo $clean; ?>">
Does that help removing your tabs?
The reason I suggest this is because retrieve_project_info could output the tabs.
One more observation: If $clean contains " will your code fail?
EDIT: #palmi
This is how I do it:
function doFormSafeHTMLEncode($someStr){
return htmlspecialchars($someStr,ENT_QUOTES,'UTF-8');
}
And I call that function everywhere where I output raw code to a formfield.
Take out the tabs in your code and put it to the chrome of the screen. I did and it works for me. Even though it is great to have readable code, however this method works for now until i find another solution.

Updating sql data through PHP

Good day!
I am having a problem in showing the data that I need to Update.
For example: I have a field - NAME. And user input "John Travolta" on it and enter to save it in SQL. Then to update it, he will click the update and a textbox will appear with the name "John Travolta" with it.
The problem is I am only getting "John" and the "Travolta" does not appear on the text box.
My code is as follows:
<input name="event" type="text" id="event" size="69" value=<?php print stripslashes($row_array['event'])?>>
What can i do in order for the whole name to appear in the textbox?
Thank you in advance.
You need to put quotes before and after the PHP line so HTML can parse it as a string.
otherwise you will end with value=John Travolta when you want value="John Travolta"
Use that code:
<input name="event" type="text" id="event" size="69" value="<?php print stripslashes($row_array['event']);?>">

Categories