Php input text background color

Php – Change background-color in php using text input form

I write a program that changes the background color of a page via a form using a text input field. I am trying to assign a variable to whatever value is written in the text field.

The code works when the input value != 6 however when input value == 6 it does not work. I think the problem arises when I try to assign the value entered into the text field to $color in the activity1.php file.

"; > elseif (strlen($_POST["color"]) != 6) < echo "Please enter a valid 6 digit hex code
"; > > ?>
  
Please enter your color code:

Best Solution

The problem, as hinted in this comment is that you are outputting two body tags. This is not valid html.

To address this, I prefer to have separate html and php code, so that I can process the html with php, and also have a clear view of my html code.

"> Please enter your color code: 

In the php code, I only have the logic containing the values to be replaced in the html:

$color = '#FFFFFF'; // that way your values won't be undefined when the post is not submitetd $error = ''; if (isset ($_POST["button1"])) < if (strlen($_POST["color"]) == 6) < $color = $_POST['color']; $error = ''; >else < // no need to verify length two times $color = ''; $error = 'Please specify a correct color value'; >> $data = [ '' => $color, '' => $error, ] 

I like using strtr() to replace values in my html. there are other functions to replace content in strings, such as sprintf() , which is more powerful and faster, but a bit more constraining and complicated

$html= strtr(file_get_contents('body.html'), $data); echo $html; 

The values contained in the $data will replace the placeholders in the template. The brackets are important as strtr() will replace any string that matches any key of your array with it’s value. Having those brackets with the ‘z’ letter minimizes the possibilities of it replacing an unwanted string.

Читайте также:  PHP Form

however, directly accessing values in your $_POST is somewhat dangerous, as it has no control over what is sent to your server. Consider at least using filter_input()

$color = filter_input(\INPUT_POST, 'color', \FILTER_SANITIZE_STRING); 
Php – How to sanitize user input with PHP

It’s a common misconception that user input can be filtered. PHP even has a (now deprecated) «feature», called magic-quotes, that builds on this idea. It’s nonsense. Forget about filtering (or cleaning, or whatever people call it).

What you should do, to avoid problems, is quite simple: whenever you embed a a piece of data within a foreign code, you must treat it according to the formatting rules of that code. But you must understand that such rules could be too complicated to try to follow them all manually. For example, in SQL, rules for strings, numbers and identifiers are all different. For your convenience, in most cases there is a dedicated tool for such an embedding. For example, when you need to use a PHP variable in the SQL query, you have to use a prepared statement, that will take care of all the proper formatting/treatment.

Another example is HTML: If you embed strings within HTML markup, you must escape it with htmlspecialchars . This means that every single echo or print statement should use htmlspecialchars .

A third example could be shell commands: If you are going to embed strings (such as arguments) to external commands, and call them with exec , then you must use escapeshellcmd and escapeshellarg .

Also, a very compelling example is JSON. The rules are so numerous and complicated that you would never be able to follow them all manually. That’s why you should never ever create a JSON string manually, but always use a dedicated function, json_encode() that will correctly format every bit of data.

The only case where you need to actively filter data, is if you’re accepting preformatted input. For example, if you let your users post HTML markup, that you plan to display on the site. However, you should be wise to avoid this at all cost, since no matter how well you filter it, it will always be a potential security hole.

Android – Animate change of view background color on Android

You can use new Property Animation Api for color animation:

int colorFrom = getResources().getColor(R.color.red); int colorTo = getResources().getColor(R.color.blue); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); colorAnimation.setDuration(250); // milliseconds colorAnimation.addUpdateListener(new AnimatorUpdateListener() < @Override public void onAnimationUpdate(ValueAnimator animator) < textView.setBackgroundColor((int) animator.getAnimatedValue()); >>); colorAnimation.start(); 

For backward compatibility with Android 2.x use Nine Old Androids library from Jake Wharton.

The getColor method was deprecated in Android M, so you have two choices:

    If you use the support library, you need to replace the getColor calls with:

ContextCompat.getColor(this, R.color.red); 

Источник

Changing input background color depending on the input in PHP

Solution 1: First save your value in variable and check/compare value before apply in , And also create style code in php variable according to condition. You have to terminate echo before using if Solution 2: 1st: Simple break the previous echo using 2nd : simple use ternary operator

Changing input background color depending on the input in PHP

You can do this with CSS3 attribute selectors. For example, if you wanted the background to be red if the input is empty or green otherwise.

Note that CSS3 attribute selectors are not fully compatible with IE8 and lower. All other modern browsers will support them.

Also if you want it to update dynamically (ie. change from red to green when the user enters something into the input box) you would have to use Javascript.

i wrote you a litte example

css

.placeholder input[type=text], input[type=password] < background:#eee; >input[type=text]:focus, input[type=password]:focus

js

$("#username").focus(function() < if($(this).val() == "Benutzername") < $(this).val(""); $(this).removeClass('placeholder'); >>); $("#username").blur(function() < if($(this).val() == "") < $(this).val("Benutzername"); $(this).addClass('placeholder'); >>); $("#password").focus(function() < if($(this).val() == "Passwort") < $(this).val(""); $(this).prop("type", "password"); $(this).removeClass('placeholder'); >>); $("#password").blur(function() < if($(this).val() == "") < $(this).val("Passwort"); $(this).prop("type", "text"); $(this).addClass('placeholder'); >>); 

html

Check it out in JSFiddle http://jsfiddle.net/hk8k4vew/

Php change background color on different value, Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives Code sample$f .= ‘‘;$last_deck = $value;Feedback

Change background color of div based on a value

Put the code for changing the background inside the success: of the Ajax call.

 success: function(data) < $.each(data, function(k, v) < $(".in-cart").text(v); >); // Update the JS variable that keeps track // of the number of items in the cart. // if that variable is > 0, set the background to blue. // else, set the background to transparent. >, 

This is more a job for javascript / jquery than php . You can set the initial state of the basket in php, but should make live changes of the basket’s state from javascript.

Change your basket’s look inside the successCallback of your ajax.

function refreshMiniBasket() < $.ajax(< url: '../modules/basket_mini_nav_refresh.php', dataType: 'json', success: function (data) < //. if (parseInt($(".in-cart").text()) >0) $(".in-cart").show(); else $(".in-cart").hide(); >, error: function (data) < //. >>); > 

Modify PHP code to change background color, Currently I generated JPG images with black background color. I need to modify it so you can change (from the code) background color of the generated image in JPG format. All other things are working properly.

Change td bg color based on php echo value

= 20)< $style = 'style="background-color:#FF0000; text-align: center;border-color: #999999"'; >elseif($value > 0 && $value < 20)< $style = 'style="background-color:#FFFF00; text-align: center;border-color: #999999"'; >elseif($value ?> % 

First save your value in variable and check/compare value before apply in , And also create style code in php variable according to condition. Now apply these two variables in final html code.

from the info you gave us, I think this should work:

 %";?> 

Changing input background color depending on the, If it is empty or already in use or the some another rule that I defined, the background color should change. Juts I need an example on simple form like: I want to use PHP and CSS codes for to do that. Feel free to use div classes and other stuff. Thanks. php. input[value=»»] < background: red; >Note that CSS3 …

How to change table cell color depending on values in php

You can’t write if in echo. You have to terminate echo before using if

  
Optional table caption. # Store Id Item Name Quantity Status -- "; if($row['quantity'] "; > else "; > echo ""; echo " -- "; echo " -- "; echo " -- "; echo " -- ";> ?>

1st: Simple break the previous echo using ;

2nd : simple use ternary operator

Change background color of div based on a value, I am trying to make my basket button have a circular div background turn blue after an item is added to the basket, so far the add to basket function is working perfectly with ajax I just can’t som

Источник

Оцените статью