How to save value in html input text - php

I have form with input text and this form action direct to the same page,
now i insert string into input text like "air garden" then submit but after that string in input text become one word that mean it show only "air" not "air garden".
<input type="text" id="sfo_keywords" <?php if($sfo_array['sfo_keywords']) echo "value=".$sfo_array['sfo_keywords'];?> />

This is because you're appending directly to the value= without extra quotes, then in your html code you have something like
<input type="text" value=air garden />
instead of:
<input type="text" value="air garden" />
You can to that to fix it :
<input type="text" id="sfo_keywords" <?php if($sfo_array['sfo_keywords']) echo "value=\"".$sfo_array['sfo_keywords']."\"";?> />

You've missed the quotes:
<input type="text" id="sfo_keywords"
<?php if($sfo_array['sfo_keywords']) {
echo "value='".$sfo_array['sfo_keywords'];."'";
}
?>
/>
If you don't wrap the values of the attributes into quotes only the first occurrence will be rendered, and next will be considered attributes.
Example:
<input class="one two">
Or
<input class=one two> <!-- here you are the "two" attribute-->

Related

Using php variable in HTML

I am using a php variable in html. $userProfile['institute'] is displaying correct value whenever $userProfile['institute'] is non-empty. When $userProfile['institute'] is empty , it display '/'. What may be the issue here ?
Institute: <input type="text" name="institute" value=<?php echo $userProfile['institute']?> /><br />
You are missing a set of quotes for your value attribute
value="<?php echo $userProfile['institute']?>"
You should add double quotes around it like this (like you have wrapped value of type and institute attributes )
Institute: <input type="text" name="institute" value=" <?php echo $userProfile['institute']?>" /><br />

PHP: How to post hidden field with style='display:none'

This doesn't get posted if it's hidden like this, how do i get around it?
<input name="Pid" style="display:none"/>
and PHP.
$Pid = preg_replace ('#[^0-9 ]#i', '', $_POST['Pid']);
Kindly use it as:
<input name="Pid" type="hidden" />
You can use hidden field simply setting type as hidden
<input type="hidden" value="" name="pid"/>
If you really need your input type to remain, like text or checkbox, use a <div style='display:none;'></div> tag to hide your input object.
Sometimes, this is required if your submit code does not include checks like
if (isset($_POST['Your_Object_name'])) {...}
Sometimes you don't want to show them, but neither do you want your code to issue error messages.
Your code will then be as follows
<div style='display:none;'>
<input name="Pid" type="text" />
</div>
Et voila

Auto fill insert form with $_GET data

I have a search form, where the user can insert in the same field the name of one or more authors.
I have this code:
Author<br /><input type="text" name="autore" value=<?php echo $_GET['autore'] ?> ><br/>
To automatically fill the Author field on the next page.
My problem is that if the user writes for example:
san, gli, tro
In the Author field I'll get only 'san,' , while I want 'san, gli, tro' .
How can I solve this?
What happens if you do:
Author<br /><input type="text" name="autore" value="<?php echo $_GET['autore'] ?>" ><br/>
(Notice quotes around the value attribute)
You need to put quotes around value from php. Like this:
Author<br /><input type="text" name="autore" value="<?php echo $_GET['autore'] ?>" ><br/>
In your case the final html will be
Author<br /><input type="text" name="autore" value=san gli tro ><br/>
so the value of attribute value is san and the other to are empty attribute names.
With quotes, the final html will be
Author<br /><input type="text" name="autore" value="san, gli, tro" ><br/>
Which is what you need.

HTML close tag print in my page

Data or value in $description from database is
<div>Henry</div>
My HTML Code
<input type="textbox" id="textbox" value=""/>
<input type="hidden" id="hidden" value="<?php echo $description; ?>"/>
Output :
If the code be
<input type="hidden" id="hidden" value='<?php echo $description; ?>'/>
Its working fine !..Any one please tell me the issue ?
You are trying to have HTML code inside the value of a hidden input in a form? That doesn't sound right.
If you need to keep it as it is, you should at least use htmlentities to make it a string:
<input type="hidden" id="hidden" value="<?php echo htmlentities($description); ?>"/>
An example:
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
?>
Your code becomes like this...
<input type="hidden" id="hidden" value = "<div><a href="www.google.com" > Henry </a></div> "/>
You can divide this as...
<input type="hidden" id="hidden" value = "<div><a href="www.google.com" >
Henry
</a></div>
"/>
That's how you get Henry"/>
Here comes 2 issues,
First of all use htmlentities to convert all applicable characters to HTML entities.
htmlentities($description);
And Its fair to use Single quote instead of double quotes. Ref link
By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa
look at the different between " and ':
if your code is :
$description = <div>Henry</div>
and
<input type="hidden" id="hidden" value="<?php echo $description; ?>"/>
so it's actually means
<input type="hidden" id="hidden" value="<div>Henry</div>"/>
and the " that before the url closes the "value", so the value is actually -
value="<div><a href="
so try to use ' instead of " on the url (google) OR in the value (not both).
This is how your browser sees the code:
<input type="textbox" id="textbox" value="<div>Henry</div>
"/>
See how the double-quotes don't make sense?
If you want to keep double-quotes you need to go with htmlentities.
$description = htmlentities($description);
<input type="hidden" id="hidden" value="<?php echo $description; ?>"/>
Also, your link "www.google.com" will point to a page called www.google.com RELATIVE to your directory. Be sure to use ABSOLUTE path: http://www.google.com
A simple replace in here like this
<div><a href='www.google.com'>Henry</a></div>
or like this
<input type="textbox" id="textbox" value=""/>
<input type="hidden" id="hidden" value='<?php echo $description; ?>'/>
but not both should do the trick.
http://php.net/manual/en/function.strip-tags.php
Try this:
div><a href='www.google.com'>Henry</a></div>
<input type="textbox" id="textbox" value=""/>
<input type="hidden" id="hidden" value='<?php echo strip_tags($description); ?>'/>

display contents in input

I have anchor tag and input tag
Go to Example.com
and
<a href="example.com">Go to Example.com</a>
Now I need to display above text in input box
<input type="text" value="Go to Example.com">
But it's not working, How can I do this?
Change quotes to html entities:
"
<input type="text" value="<a href="example.com">Go to Example.com</a>">
<input type="text" value="<a href="example.com">Go to Example.com</a>">

Categories