how to keep input text and value different in html - php

I am designing a form in which i have an input text field,
And my text is of the following type,
abc-112233
scenario for new record -
I want first 4 characters of text to be fixated/non-editable in the field, ie. "abc-" and user can type number coming after the hyphen, but when the form gets submitted, i want it to get submitted as abc- 112233, not just 112233.
Hence in short, i want the text to be shown as 112233 (abc- fixed) inside the input, but value after submission should be whole "abc-112233".
scenario for previous record to be updated -
and for when the input field is pre-filled according to records in database, ie. when the record needs to be updated, i want to show it as abc- 112233
my code is according to following,
<div class="">
<label for="inputError" class="">Name</label>
<input
type="text"
id="" class="form-control"
name="name"
value="<?php
if(isset($data->name)){
echo $data->name;
} else{
echo "";
}
?>"
required
>
</div>
right now, the value is,
$data->name
which is abc-112233.
and the text shown inside the input is also abc-112233,
I want the text to be 112233(with abc fixated/non-editable on left hand side) and the value to be abc-112233

I don't think you can do it within only 1 <input> element, maybe you can try separated into 2, one is fixed for "abc-" and the other is for user input. Then you can control whether to show / hide the fixed one.

You could consider using pseudo code to display the prefix.
label {
display: block;
}
.demo:before {
content:"abc-";
}
<label for="demo">Demo input</label>
<span class="demo"><input id="demo" type="text"></span>

You could use this approach:
body, input
{
font-family: arial;
font-size: 14px;
}
.editable
{
position: relative;
border: 1px solid gray;
padding: 3px;
background-color: white;
box-shadow: rgba(0,0,0,0.4) 2px 2px 2px inset;
}
.editable > input
{
position: relative;
z-index: 1;
border: none;
background-color: transparent;
box-shadow: none;
width: 100%;
padding-left: 40px;
}
.editable::before
{
position: absolute;
left: 4px;
top: 5px;
content: attr(data-placeholder);
pointer-events: none;
opacity: 1;
z-index: 1;
}
<div class="editable" data-placeholder="ABC- ">
<input type="text" value="" />
</div>
you will need to play with the CSS a bit to get it to look right. I presume you are using Bootstrap based on your ".form-control" class.
Basicly this is just forcing content to overlay on top of your input. not inside of it. otherwise you will need to prepend or append content to the input value using JavaScript

Related

Checkbox array (square brackets) - all have the same name and 'checked' is not working

I have a number of checkboxes that return an array of data to PHP. They are all named 'Plans[]' but with different IDs.
I have a CSS checkbox styler that replaces the usual HTML checkbox with something more fancy...
When the page is displayed, there is one (or several) that are 'checked', but they don't display as checked (I think one does but that is hidden usually).
I am assuming that the problem is caused by them all having the same name and so somehow they are the checked attribute is not getting acted on for all of them for this reason.
I have tried wrapping each of them in their own forms (as I saw this suggested elsewhere) but to no avail.
Here's an example of what it looks like:
<style type="text/css">
.checkOpt input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: -.3em;
right: 5%;
height: 25px;
width: 25px;
background-color: #fff;
border-radius: 20%;
border: 1px;
border-color: #1e62d0;
border-style: dashed;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
font-weight: 900;
color: blue;
margin-top: -11px;
margin-left: -3px;
font-family: Arial, Helvetica, sans-serif;
font-size: 28px;
content: "\2714";
position: absolute;
display: none;
}
/* Style the indicator (dot/circle) */
.checkOpt .checkmark:after {
top: 8px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
/* background: white; */
text-shadow: -1px -1px 0 #7f7f7f, 1px -1px 0 #7f7f7f, -1px 1px 0 #7f7f7f, 1px 1px 0 #7f7f7f;
}
/* Show the indicator (dot/circle) when checked */
.checkOpt input:checked~.checkmark:after {
display: block;
}
/* On mouse-over, add a grey background color */
.checkOpt:hover input ~ .checkmark {
background-color: #97c4fe;
}
/* When the radio button is checked, add a blue background */
.checkOpt input:checked ~ .checkmark {
background-color: #2196F3;
}
.checkOpt input:disabled ~ .checkmark {
background-color:#b0c7df; pointer:default;
}
.test {
position: relative;
height:60px;
}
</style>
<div class="test">
<div class="checkOpt" style="top:20px;">
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Transfer existing line - OFNL" id="12" value="0.00" checked="checked" >
<span class="checkmark"></span>
</label>
</div>
</div>
<div class="test">
<div class="checkOpt" >
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Paper Bill" id="35" value="2.00" >
<span class="checkmark"></span>
</label>
</div>
</div>
<div class="test">
<div class="checkOpt">
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Transfer existing line " id="12" value="0.00" checked="checked" disabled="disabled">
<span class="checkmark"></span>
</label>
</div>
</div>
...only one of these (the first) is showing as checked - the rest are not.
Does anyone have any ideas what I might do to get round this (without coding changes to the subsequent data collection if possible!)
tried everything I can think of and would welcome any suggestions....
UPDATE - I tried updating the page so that the checkboxes have different names ('Plans[0]', 'Plans[1]' etc.) and the checkmarks suddenly started to appear in the right places.
The issue is now that there is (legacy) JQuery code that doesn't work, i.e.
$("input[name='plans[]']:checked").each(function(){
var thisPlan= $(this).attr('id'); var thisVal = $(this).attr('value');
plans.push(thisPlan);
planValues.push(thisVal);
// ....
// ....
}
I guess this would work if the Plans list were just POSTed and picked up by PHP, but in fact it is intercepted by Javascript, processed and re-posted via Ajax to the next (PHP) page (where it is picked up quite simply by $_POST['plans']). It's a real mess, but Ihave inherited it...
Anyone have any idea how I could work round this without breaking all the legacy (Javascript/JQuery) code?
SOLVED. I have discovered that there is actually a bit of Javascript/JQuery code that is overwriting the 'checked' attribute and this is causing the problem. The 'name' duplication was a red herring - by changing the names (from 'plans[]') I just stopped the code working (it used $('input[name="plans[]"]').each(function ()).
Thanks for your suggestions and help - Sorry to waste anyone's time. It's a bit of a nightmare trying to unravel someone else's mucky code!
Now I just have to get the functionality to work without that code...
The issue is that you're absolutely positioning them as Quentin stated in the comments. There are a few other issues:
You have two elements with the same ID and this isn't permitted (12)
IDs should start with a letter
Your first element has the attributed disabled = "" but this will make the item disabled irrespective of the value of the attribute. It's not ideal, I know.
disabled element values are usually not sent with the form (see link in the bullet point above) which may explain why it's not getting to your server script. this might be the cause as the check box is ticked but also disabled and the browser may be just ignoring all disabled checkboxes when sending the form data.
It's the same for checked = "", if the checked attribute is present on in you input element it'll still be checked.
I've had a go at what I think you're trying to achieve and marked up any changes.
.checkOpt {
/* added this so the container has some size */
height: fit-content;
display: inline-block;
}
.checkOpt input {
/* added this, you're looking to make your checkbox invisible so just use display:none */
display:none;
/* removed this
position: absolute;
opacity: 0;
cursor: pointer;
*/
}
/* Create a custom radio button */
.checkmark {
/* removed this
position: absolute;
top: 0em;
/*
right: 5%;*/
/* added this so that the tick marks are positioned in the middle so you don't have to use
absolute positioning on them */
display: inline-flex;
align-items:center;
justify-content:center;
height: 25px;
width: 25px;
background-color: #fff;
border-radius: 20%;
border: 1px;
border-color: #1e62d0;
border-style: dashed;
cursor:pointer;
}
.checkb:checked ~ .checkmark:after {
/*if the checkbox is checked then make the tick appear by making it opaque */
opacity:1;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
font-weight: 900;
/*color: white;
margin-top: -11px;
margin-left: -3px;*/
font-family: Arial, Helvetica, sans-serif;
font-size: 28px;
content: "\2714";
/* added this - makes the tick mark transparent when it's not checked */
opacity: 0;
/*position: absolute;*/
/*display: none;*/
}
.checkb:disabled ~ .checkmark:after {
/* I noticed that you had the disabled attribute set in your HTML so I've styled this gray so you know it's
not click-able */
color:lightgray;
}
<div class="checkOpt">
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Transfer existing line - OFNL" id="a12" value="0.00" checked="checked" disabled="" autocomplete="off">
<span class="checkmark"></span>
</label>
</div>
<div class="checkOpt">
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Paper Bill" id="a35" value="2.00" autocomplete="off">
<span class="checkmark"></span>
</label>
</div>
<div class="checkOpt">
<label class="labelopt">
<input type="checkbox" name="plans[]" class="checkb " title="Transfer existing line " id="a12X" value="0.00" autocomplete="off" checked="checked" disabled="disabled">
<span class="checkmark"></span>
</label>
</div>

How a textarea can display an svg file

Trying to insert some svg's in my chat app. If I use them as PHP files, it's ok they will display in the chat#div but in the text area it's the full code of the svg's. When I'm trying to display them as svg the only thing I can see in the chat is this [object XMLDocument] both in text area and in #div
here's the code for the textarea
<textarea id="comment" style="width: 280px; margin-top: 10px; box-shadow:
inset 0 -15px 35px -5px rgba(0,0,0,0.3); height: 40px; overflow: auto;
pointer-events: all;">
</textarea>
and here is the javascript for the svg
function test(){
$.ajax({
url:"emoticons/cloudda3.svg",
success:function(result){
$("#comment").val(result);
}
})
}
A div Element has a contenteditable attribute, setting it to true will allow the user to enter and modify the data in the div. The best thing: the div can contain rich text including images.
.textarea {
font-family: monospace;
outline: none;
background: #efefef;
border: #888 1px solid;
display: block;
position: relative;
resize: both;
overflow: auto;
font-size: 9pt;
}
img {
height: 9pt;
}
<div class="textarea" contenteditable="true">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Perry-miniature-donkey-in-Palo-Alto-CA-2016.jpg/800px-Perry-miniature-donkey-in-Palo-Alto-CA-2016.jpg" />
</div>
Note: The <img> tag in the <div> can be replaced with any valid image or SVG.
As a sidenote, #emaillenin is correct in saying that textareas are only for text. Also I'm noticing you using the val function to insert, that would mean that the value of the element is updated, not it's innerHTML. This would result in a behaviour similar to what you are seeing now. If you want to get just the text from the div, use the text function.

How can i show an asterisk (to indicate field is required) inside a select2 dropdown?

I have a form where I need a multiselect. For that I use Select2.
Problem is: In this form all of my fields are required, and I inform my users about that by showing an asterisk when the field is empty. However, Select2 totally ignores the fact there is a required attribute on the created select.
So my question is: How can i show an asterisk image inside a select2 dropdown? Preferably with the same method i use for my other inputs?
EDIT: I already took care of the validation part. My question is purely about informing the user what fields are required.
Example Form
HTML
<input type="text" name="name" id="name" value="" placeholder="" maxlength="255" style="width: 300px" required="required"/>
<input type="text" name="label" id="label" value="" maxlength="8" style="width: 150px" required="required"/>
<?php
echo form_multiselect('client_types[]', $client_types, set_array('client_types', null), 'class="select2" style="width: 315px;" required="required"');
?>
CSS
input:required, textarea:required {
background: #fff url(../images/red_asterisk.png) no-repeat 98% center;
}
.required_notification {
color:#d45252;
margin:5px 0 0 0;
font-size: 12px;
font-weight: bold;
}
input:required:valid, textarea:required:valid {
background: #fff url(../images/valid.png) no-repeat 98% center;
box-shadow: 0 0 5px #5cd053;
border-color: #28921f;
}
input:focus:invalid, textarea:focus:invalid {
background: #fff url(../images/invalid.png) no-repeat 98% center;
box-shadow: 0 0 5px #d45252;
border-color: #b03535
}
You can do three things:
1) Create a rule in Select 2 and copy the multiple rule. That rule adds the little x in that position so you can replace with an asterisk instead for yours. This is most likely the least preferable of the three options so I won't go into details.
2) You can use CSS on your select2 container like:
select2-container:after {
content: "*";
display: block;
position: absolute;
right: 10px;
top: 5px;
color: red;
}
select2-container { position: relative; }
Not exact code but you get my drift. If this is for specific fields you might have to add a class to that field and reflect that in your CSS or add a wrapper element around your select and target that as well.
3) If compatibility is an issue you could always use jQuery.
$(".select2-container").append("<span class="asterisk">*</span>");
Again using your selectors to get the exact element you want and add CSS for the asterisk span.
I don't know enough about select2 so if there's an option in the init for adding a required indicator that's going to be your best best so I would start researching there to see if this is something provided out of the box and if not then you can try one of the above options.
I had same problem. And i found out 2 acceptable (for me) solutions.
Use ajax to validate field is it unset\set incorrectly.
Use simple JavaScript to validate is there any information in the input.
It can be done when clicking the button (submit) or after change focus from input.

Onclick submit, in a form, get inputbox value into variable and output it into another inputbox

I am developping a job appliance form, that outputs its results into a CRM (Customer Relationship Management).
My problem is that my client wants the fields "Street Adress, Postal Code and locality" outputted all together in one single field in CRM, after inputted in different fields on the form.
So i thought i should use php for when the user clicks on submit it gets the value of each one of the fields and inputs it into an hidden field i created.
for some reson, as i am not a php expert, it isn't working.
It is really not outputting any value into the CRM
this is what i've got so far:
<form action="https://service.capsulecrm.com/service/newlead" method="post">
<input name="morada" id="morada" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 397px;" value="" type="text">
<input name="postal" id="postal" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 437px;" value="" type="text">
<input name="local" id="local" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 467px;" value="" type="text">
<?php if ($_POST['submit']) {
$addressmorada = $_POST['morada'];
$addresspostal = $_POST['postal'];
$addresslocal = $_POST['local'];
echo "<input type='hidden' name='STREET' value='".$addressmorada. " " .$addresspostal. " " .$addresslocal."' />";
}
?>
<input name="submit" type="submit" value="Submit" style="
border-style: none; border-width: 0px; border-color: #888888; position: absolute; margin: 0px; padding: 0px; width: 93px; height: 33px; z-index: 1;
left: 433px; top: 1773px;
text-indent: 99999999999999px;
background: green;
cursor: pointer;
">
</form>
I will try to explain my PHP code,
the if statement it's calling the function on submit.
the variables get the different input values.
I echo an input field type hidden, calling the variables, so that all the information is outputted into the CRM in one single field
I think the major problem may be on number 3! i'l apreciate any help.
Sory if my english is bad, it's not my mother language.
two issues:
1,
if ($_POST['submit'])
will throw an error: Notice: Undefined index: submit in ... if there is no $_POST data.
it should be changed to
if (isset($_POST['submit']))
2, $_POST['submit'] will never occur as there is no name attribute called submit. You should add this to your "submit" button:
<input type="submit" value="Submit" name="submit" style="[STYLES_HERE]">
You don't have a name for you button right.
<input type="submit" value="Submit" name = "submit" >
You have
if ($_POST['submit']).
So the name of the button should be submit. Otherwise it will never go in your if statement.
For debugging purpose
Try just printing all the values in the if statement. Its helpful.
You're confusing server-side and client-side code. Here's a simple explanation, though you should document yourself more about HTTP and the way web servers work:
when a user A requests a page, a HTTP request is sent to the server
this request is handled by a web server B
according to some rules (file extension, file location, ...), the web server can decide to simply fetch the file and send it to the client A (e.g. for a plain HTML file), or run it through a (PHP) interpreter first and then send the result to the client.
in any case, the client A receives only the HTML: the PHP has already been executed by B at this point.
So the user requests the page with the form. At this point he didn't submit anything yet, so the PHP interpreter gets nothing in $_POST and the hidden input is empty.
The user fills the form and submit it. That triggers a new HTTP request with the data. But of course the hidden input is still empty, and will never get filled because the PHP has already been executed (and won't ever be client-side).
What you need to do is either
modify the target script (https://service.capsulecrm.com/service/newlead) if you have access to it, in order to join the fields together
if you can't, add an intermediate PHP script which is the target of the form, join the fields together and send a HTTP request itself (e.g. with curl) to newlead
or if you don't want to spend too much time on that, use client-side code, that is Javascript, to fill the hidden input. However you can't be sure the form will be submitted correctly (the user can choose to disable Javascript, or the client may not handle it at all: this is common for Braille devices).

HTML overflow:hidden doesn't format text correctly

I'm working on a website for an American Football team. They have these newsitems on their front page which they can manage through a CMS system. I have a problem with alligning the text inside those news items. Two of the news items look like this:
As you can see, the right newsitem text are displayed nicely. But the left cuts it off really bad. You can only see the top half of the text at the last sentence. I use overflow: hidden; to make sure the text doesn't make the div or newsitem bigger. Does anyone have any idea how to solve this through HTML and CSS or should I cut it off serverside with PHP?
Here's my code (HTML):
<div class="newsitem">
<div class="titlemessagewrapper">
<h2 class="titel" align="center"><?php echo $row['homepagetitel']; ?></h2>
<div class="newsbericht">
<?php echo $row['homepagebericht']; ?>
</div>
</div>
<div class="newsfooter">
<span class="footer_author"><?php echo get_gebruikersnaam_by_id($row['poster_id']); ?></span> <span class="footer_comment">Comments <span>todo</span></span>
Lees meer
</div>
</div>
And here is the CSS:
.newsitem{
float: left;
height: 375px;
width: 296px;
margin: 20px 20px 0px 20px;
background-color: #F5F5F5;
}
.newsitem .titel{
color:#132055;
font-size:1.2em;
line-height:1.3em;
font-weight:bold;
margin:10px 5px 5px 5px;
padding:0 0 6px 0;
border-bottom:1px dashed #9c0001;
}
.titlemessagewrapper{
height: 335px;
overflow: hidden;
}
.newsitem .newsbericht{
padding:5px 5px 5px 5px;
font-size: 0.8em;
margin-bottom: 5px;
}
.newsitem .newsfooter{
width: 100%;
height: 25px;
background-color: #132055;
margin: 0px auto;
font-size: 0.8em;
padding-top: 5px;
margin-top: 10px;
border: 1px solid #9c0001;
}
You should not rely on the user to enter <cut> !
User Input = error
What if the user forgets to enter <cut>? Will your news item now look unprofessional?
What would be the point of a user creating a news item to find that some of it was cut off?
If the div can only fit a fixed string length you should validate the max length of the news item Input body instead of relying on <cut>. This can be simply achieved using maxlength attribute.
<textarea id="userinput" maxlength="150">Enter your news</textarea>
If you do use <cut> you should also add in overflow: hidden; to ensure that the content is not unprofessionally displayed if no cut tag is present.
If you want to display the all text and keep the div the same fixed height
Replace
overflow: hidden;
with
overflow:auto;
(Scroll bar won't appear when content is smaller than the div)
Otherwise validate the length of the string / content in your div or remove the CSS height attribute to allow all the content appear with no scroll bars.
Hope this helps
Remove the height attribute on the .titlemessagewrapper. Its this height attribute which is causing the cut off.
If you want the boxes to remain the same height: Take the whole string, perform substr and save in a new variable and echo that.
Eg.
<?php
$str = "abcdefghijkl";
$new_strsubstr($str, 0, 8); // abcdef
// will return abcdefhi
?>

Categories