display contents in input - php

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

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

Need to pass a variable in anchor tag

I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
I want to pass value from searchbutton to anchor tag
Hello"
If you are using jquery in your project and want to do this on the front end, you can do:
<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>
$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());
With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)
i.e.
Hello"
IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.
Be aware though that the link would need to be on the page set in your forms action attribute to populate the link
You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
On your fetchvalues.php page
<?php
$val = $_GET['q'];
?>
then
Hello
You can do like this
<?php
if(isset($_REQUEST['searchbutton']))
{
?>
Hello
<?php
}
?>

Appending checkbox selections to URL

I want to be able to search through my WordPress Woocommerce store, by selecting multiple product tags. By default, if you add tags to a product, they are visible on the product page, and if you click on a tag, it searches for other products which are also tagged the same. The results of this search look url like this:
http://localhost/ccloud/?product_tag=option1
If you manually add other tags to this URL, it searches for products which are tagged with both selections, like this:
http://localhost/ccloud/?product_tag=option1+option2
This works, but obviously I want users to be able to do this using checkboxes.
I’ve created this form (which doesn’t work)
<form name="search_by_tag" action=/product_tag.php method="get">
<input type=checkbox name="option1">Option 1<br>
<input type=checkbox name="option2">Option 2<br>
<input type=checkbox name="option3">Option 3<br>
<input type=checkbox name="option4">Option 4<p>
<input type=submit value="Search">
</form>
I think it doesn’t work because it’s not sending the action correctly. The result of selecting multiple checkboxes and searching looks like this:
http://localhost/product_tag.php?option1=on&option2=on
How can I correct the url (the first part is missing the directory) and remove the .php part etc? It doesn't work at all if I remove the .php extension
You’ll have to use a little JavaScript to make this work, but you could do it like this.
HTML:
<input type="checkbox" class="tags" value="red">Red<br>
<input type="checkbox" class="tags" value="blue">Blue<br>
<form name="search_by_tag" action="/ccloud/" method="get">
<input type="hidden" id="tags" name="product_tag" />
<input type="submit" value="Search"/>
</form>
jQuery:
$(function() {
$('form').on('submit', function() {
var tags = [];
$('.tags:checked').each(function() {
tags.push($(this).val());
});
tags = tags.join(' ');
if (tags) $('#tags').val(tags);
else $('#tags').remove();
});
});
So, what we are doing is using a hidden field to store the selected tags, and moving the checkboxes out of the form. Then, when they submit, we populate the hidden field so that it gets included in the query string. Spaces turn into +'s in the URL.
product_tag needs to be the name of the hidden field, and the action is /ccloud/, so that you end up with a URL like you want. Here is a jsFiddle of it in action.
A few issues I see in your code:
<form name="search_by_tag" action=/product_tag.php method="get">
<input type=checkbox name="option1">Option 1<br>
<input type=checkbox name="option2">Option 2<br>
<input type=checkbox name="option3">Option 3<br>
<input type=checkbox name="option4">Option 4<p>
<input type=submit value="Search">
</form>
The type property should have quotes around it. Also, you are not setting a value property. And to get the URL to be /product_tag you need to lop off that .php and mayeb set the form action to the full site URL So this should be closer to what you want:
<form name="search_by_tag" action="http://my.great.site/product_tag" method="get">
<input type="checkbox" name="option1" value="option1">Option 1<br>
<input type="checkbox" name="option2" value="option2">Option 2<br>
<input type="checkbox" name="option3" value="option3">Option 3<br>
<input type="checkbox" name="option4" value="option4">Option 4<p>
<input type=submit value="Search">
</form>
And that said, you need to add more logic—either via PHP or JavaScript like the user dave points out—to get this set. A simple get wouldn’t work.

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

Check type of an input field with php

I would like to check the type of an input field text is. Cause i only want to make thos fields read only. I know i can do this in javascript, but is there any way that this is possible in php?
best regrads.
same as javascript use this
<input name="address" type="text" readonly="readonly">
this make field read only you can't change it.
<input name="address" type="text" value="<?php echo "your value"; ?>" readonly="readonly">
try this code
To disable an input field add disabled attribute:
disabled="disabled"
This has to be done when generating the html or with javascript after generation.
This is a clean way of doing it and the user sees directly that it is just readable but not editable.
When you submit a form, in php you take the name attribute to get the posted value. Say you have
<input name="address" type="text">
In php:
if($_POST['address']){
//don't process address cause you know this is of type text
}
This is how you can do it in php,if $field is false then set input disabled
<input type="text" name="" <?php echo ($field == false)? 'disabled="disabled"' : '';?> value="" />

Categories