Using php variable in HTML - php

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

Related

How to save value in html input text

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

Wrap php variable in quotes before echoing

I need to use a php variable to echo the id into an input field:
<input type="text" id={{ strtolower($val) . "Field" }}/>
But with the code above the output is
<input type="text" id=waterField/>
I want the output to wrapped in double quotes like:
<input type="text" id="waterField" />
I am using laravel but am totally willing to accomplish it without.
Is there a simple way to accomplish this? Thank you.
That is pure php, considering you have short tags enabled.
<input type="text" id="<? echo strtolower($val) ?>Field" />
use
echo '<input type="text" id="'.strtolower(htmlspecialchars($val)). 'Field"/>';
OR idle way to use html and php is
<input type="text" id="<?php echo strtolower(htmlspecialchars($val)); ?>Field"/>
If $val can have quotes in it, then need to clear them by using htmlspecialchars.
Why not just:
<input type="text" id="{{ strtolower($val) }}Field"/>
You can simply add the double quotes into your html text. Escape the double quotes like this \" so php won't parse them and you're fine.

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.

Changing a variable in php file

How to add values from <input> to $var in php file?
html
<input type="text" id="align" name="align"/>
php file
<?php
$align="center";
?>
You mean you want to post it and put it in a PHP variable?
Try this:
<form action="somephpfile.php" method="POST">
<input type="text" name="align" value="center" />
<input type="submit" value="Send!" />
</form>
somephpfile.php
$align = $_POST['align'];
echo $align;
It depends on the form action.
If your form that is holding your fields has an action="post" attribute then from the php side you have to use $_POST['align']. If you have set the action to action="get" then you have to use the $_GET['align'].
<?php
$align = $_POST['align'];
// OR
$align = $_GET['align'];
?>
Just output it into the regular HTML value attribute:
<input type="text" id="align" name="align" value="<?php echo htmlspecialchars($align); ?>" />
The htmlspecialchars() call is there to escape things like quotes, to avoid problems if your variable contains quotes, and to make your mark-up more standards-compliant.

Input value based on query string

How could I pass a query string value such as domain.com/register?invite=MR5OMxTyjYmTjcwNTyQjTZMyY5YY into a input box on a page?
For example say I had this: <input type="text" name="invite" value="" />
I'm using PHP
To clarify what I mean, if a person loaded that URL, then the value would be automatically filled in with the query string of invite.
Simple:
<?php $invite = (array_key_exists('invite', $_GET)) ? htmlspecialchars($_GET['invite']) : ''; ?>
<input type="text" name="invite" value="<?php echo $invite; ?>" />
Try this:
<input type="text" name="invite" value='<?php echo $_GET["invite"]; ?>' />
<input type="text" name="invite" value="<?php if(isset($_GET['register'])) echo $_GET['register']; ?>" />
This isn't secure at all, but it gives you a start.
Try fetching the invite key with $_GET['invite'] from the address bar (validate it first of course to prevent XSS attacks ;) ) and then place it in your input field within that value part as $invite for example so you end up with value="$invite"
Hope that helps!
<input type="text" name="invite" value="<?php echo $_REQUEST['invite']; ?>" />

Categories