Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to php so i'm stuck with this basic problem. Here I created one textbox and one submit button. What I wanted is that when I type for instances the word "red" in the textbox it's background will automatically change into red but unfortunately the problem is that it's not taking any effect. Any help out there are greatly appreciated. Thank you.
<html>
<head>
<title>
practice
</title>
<style type="text/css">
.red
{
background-color: red;
}
.blue
{
background-color: blue;
}
.yellow
{
background-color: yellow;
}
</style>
</head>
<body>
<?php
$fval = $_POST["val"];
if( isset( $_POST["colorme"] ) ) {
if($fval == "red")
{
echo S_POST['val'] = "red";
}
elseif($fval == "blue")
{
echo S_POST['val'] = "blue";
}
elseif($fval == "yellow")
{
echo S_POST['val'] = "yellow";
}
}
?>
<form method="post">
<input type="text" name="val" class="<?php echo $_POST['val'] ?>">
<input type="submit" name="colorme">
</form>
</body>
</html>
Your Example
<html>
<head>
<title>practice</title>
<style type="text/css">
.red{background-color: red;}
.blue{background-color: blue;}
.yellow{background-color: yellow;}
</style>
</head>
<body>
<?php
$fval = isset($_POST["val"])?trim($_POST["val"]):'';
?>
<form method="post">
<input type="text" name="val" class="<?php echo $fval ?>">
<input type="submit" name="colormoko">
</form>
</body>
</html>
try this for me its working
<html>
<head>
<title>
practice
</title>
<style type="text/css">
.red
{
background-color: red;
}
.blue
{
background-color: blue;
}
.yellow
{
background-color: yellow;
}
</style>
</head>
<body>
<?php
$fval = "";
if( isset( $_POST["colormoko"] ) ) {
$fval = $_POST['val'];
if($fval == "red")
{
echo "<red>";
}
elseif($fval == "blue")
{
echo"<blue>";
}
elseif($fval == "yellow")
{
echo"<yellow>";
}
}
?>
<form method="post">
<input type="text" name="val" class="<?php echo $fval ?>">
<input type="submit" name="colormoko">
</form>
</body>
</html>
function changeColor() {
var textBox = document.getElementById("textbox_coloring");
var color = textBox.value;
textBox.style.backgroundColor = color;
}
<!DOCTYPE html>
<html>
<head>
<title> coloring textbox </title>
<meta charset = 'utf-8' />
</head>
<body>
<form>
<input type = 'text' id = 'textbox_coloring' onkeyup='changeColor();'/>
</form>
</body>
</html>
This snippet change the color of the textBox by altering the style of the textBox by inserting the text value.
It use the "onkeyup" javascript property which permit to access the textBox after a key is pressed.
Related
I want the user to enter the desired color to be set as background color, but if it is not set, I want it to be set to silver.
It doesn't work anyway, but this is what I wrote so far and now I'm stuck!
<?php
if(isset($_GET['bgColor']))
$bgColor = $_GET['bgColor'];
else
$bgColor = 'silver';
echo "You chose the {$bgColor}" . " color.";
?>
Form:
<form action="w3.php" method="get">
<label>Background Color:</label><input type="text" name="bgColor">
<input type="submit">
</form>
Style is set to body{background-color: <?php echo $bgColor; ?>;}
You should be using the following code to change the background color - Try putting the body tag inside if else:
if (Condition)
{
echo '<body style="background-color:blue">';
}
else
{
echo '<body style="background-color:white">';
}
Updated: I've updated your code and little help from developer. The code is working. Check it:
<?php
if(isset($_GET['bgColor']))
$bgColor = $_GET['bgColor'];
else
$bgColor = 'silver';
echo "You chose the {$bgColor}" . " color.";
?>
<form action="w3.php" method="get">
<label>Background Color:</label><input type="text" name="bgColor">
<input type="submit">
</form>
<style type="text/css">
body{background-color: <?php echo $bgColor;?>;}
</style>
Just write the color name like blue, red and it'll be changing in the next page:
w3.php -
<?php
?>
<style type="text/css">
body{background-color: <?php echo $_GET['bgColor']?>;}
</style>
Done nothing here. Just grabbed the QueryString value from url that's the color name.
It seems that you do not write the code in a good order.
For example look how this code works:
<?php
if(isset($_GET['bgColor']))
$bgColor = $_GET['bgColor'];
else
$bgColor = 'silver';
echo "You chose the {$bgColor}" . " color.";
?>
<form action="w3.php" method="get">
<label>Background Color:</label><input type="text" name="bgColor">
<input type="submit">
</form>
<style type="text/css">
body{background-color: <?php echo $bgColor;?>;}
</style>
this is how to set the color after receive the form input:
<style type="text/css">
body{background-color: <?php echo $bgColor;?>;}
</style>
I'm trying to create a state capital guessing game where a state will randomly generate and then from the dropdown list which contains all the capitals the user will have to guess the capital. Anyway, he wants us to have a separate file with an array. The file is called city_info.php and the arrays look like this
$state_capitals = array(
'Alabama' => 'Montgomery',
I'm having trouble getting the drop down list populate with all the capitals.
<?php
require_once "include/session.php";
if (!isset($session->valid)) {
header("location: login.php");
exit();
}
require_once "include/city_info.php";
/* DO NOT MODIFY THE ABOVE LINES !!!!! */
$states = array_keys($state_capitals);
$capitals = array_values($state_capitals);
$cities = array_merge($capitals, $other_cities);
sort($cities);
$params = (object) $_REQUEST;
print_r($params);
if (isset($params->guess)) {
$state_capitals = $params->capitals;
$answer = $params->answer;
$cities = $params->city;
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta http-equiv="Cache-Control"
content="no-cache, max-age=0, no-store, must-revalidate" />
<title>State Capital Guess Game</title>
<style type="text/css">
body {
padding: 10px 20px;
}
#logout {
position: absolute;
top: 40px;
right: 40px;
}
div {
width: 320px;
padding: 10px;
border: 3px solid black;
border-radius: 10px;
color: black;
text-align:center;
background-color:#3399ff;
display:inline-block
}
/*
More style rules
*/
</style>
<script type="text/javascript">window.onunload = function(){}</script>
</head>
<body>
<a id="logout" href="logout.php">Log out</a>
<h2>State Capital Guess Game</h2>
<form action="program.php" method="get">
<button type="submit">Start Over</button>
</form>
<!--- computational form -->
<div>
<p>What is the Capital of PA</p>
<select name="cities">
<?php
foreach ($city as $value):
?>
<option <?php
if ($value == $params->city)
echo "selected";
?>
><?php
echo $value;
?></option>
<?php
endforeach;
?>
</select>
<input type="submit" name="choose" value="Choose" />
</form>
</div>
<?php
echo $state_capitals;
?>
</body>
</html>
The way you phrase your question, this sounds like it could be homework. Many people on SO are glad to help with homework, however, some are not; so as a courtesy when asking a question about homework follow these guidelines: https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions. You may already know this, but wanted to cover the basics just in case.
On to your question. I'll clean things up a bit so it's not so hard to follow the HTML/PHP. This should work if you substitute your current code:
<select name="cities">
<?php
$options = "";
foreach ($cities as $value){
$options .= '<option value="' . $value . '"';
if ($value == $params->city)
$options .= ' selected';
$options .= '>' . $value . '</option>';
}
echo $options;
?>
</select>
This way you're only switching between HTML/PHP once. Hope that helps!
I am echoing records from the database which are wrapped with html tags and was trying to put some effect on the echoed data. When I click the edit link, the textfield should shake. It works on the first element but when I click the edit link of the next element, the first textfield still shakes and not the other one.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wallpost</title>
<style>
.wallpost input[type="text"]{
width: 500px;
height: 20px;
}
.wallpost input[type="submit"]{
height: 26px;
}
.user{
font-weight: bold;
font-size: 20px;
}
.post{
font-family: Arial;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<?php
require_once 'dbconfig.php';
$user = $_SESSION['user'];;
echo '<form action="post.php" method="post" class="wallpost"><input
type="text" name="post" size="50"><input type="submit" name="wallpost"
value="Post"></form>';
$query = $con->query("SELECT * FROM statuspost ORDER BY id DESC");
while($i = $query->fetch_object()){
//echo $i->post.' '.$i->id.' <a href="wallpost.php?type=post&
id='.$i->id.'" >Remove</a>'.'<br/>';
echo '<span class="user">'.$i->user.'</span>'.'<br>'.'<span
class="post">'.$i->post.'</span>'.' <form action="editpost.php?type=post&
id='.$i->id.'" method="post"><span id="edit"><input type="text"
name="edit">
<br/><input type="submit" value="Edit"></span><a href="#"
onclick="showEdit();">Edit </a><a href="remove.php?type=post&
id='.$i->id.'" >Remove</a></form> '.'<br/><br/>';
//echo '<div id="post">'.$i->post.' '.$i->id.'<a href="#"
id="anchor" class="',$i->id,'" onclick="del();">Remove</a></div>
<br/>';
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0
/prototype.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0
/scriptaculous.js"></script>
<script>
function showEdit(){
Effect.Shake('edit');
}
</script>
</body>
</html>
Replace <span id="edit"> by something like <span id="edit'.$i->id.'"> to have different ids on each elements. Then of course, showEdit() must know which id it has to shake, so it has to take a parameter. Or even simpler: replace onclick="showEdit();" by onclick="Effect.Shake(\'edit'.$i->id.'\');"
Scriptaculous effects take either an ID or a JavaScript reference to a DOM element as their first argument, so if you add a classname to your multiple elements, you can shake all of them at once like this:
<span class="shake-me">...</span>
<span class="shake-me">...</span>
<span class="shake-me">...</span>
Inside an enumerator:
$$('.shake-me').each(function(elm){
Effect.Shake(elm);
});
I can use only html and php.
i have a page with 2 frames. 1 left and 1 main.
On the left i have a dropdown list and a submit button.I want when i press submit button to display the url of the option of dropdown list in the main.php.
The page that creates 2 frames.
<!DOCTYPE html>
<html>
<frameset cols="25%,*" >
<frame src="left5.php" name="leftFrame" id="leftFrame" title="leftFrame" />
<frame src="main5.php" name="mainFrame" id="mainFrame" title="mainFrame" />
</frameset>
</frameset>
<body>
</body>
</html>
LEFT frame
<!DOCTYPE html>
<html>
<body>
<form name="links" action="main5.php" method="post">
<select name="NAME" id="ex_name">
<option value="../url1.html">URL1</option>
<option value="../url2.php">URL2</option>
<option value="../url3.php">URL3</option>
</select><input type="submit" value="GO!" name="submit"/>
</form>
</body>
</html>
Main frame
<!DOCTYPE html>
<html>
<body>
<?php
$ep = $_POST['links'];
echo "$ep"
?>
</body>
</html>
I cant use javascript.I know my fault is in $_POST
For your PHP, you're going to have to use something like switch and $_get functions to call the page.
Below is how I would do it.
It does not include how to work it out with frames, but it will show you how to call a local page in PHP.
index.php
body {
margin: 0;
background-color: red;
}
.mymenu {
position: absolute;
width: 300px;
top: 0;
left: 0;
background-color: blue;
}
.mymenu a {
color: #FFF;
}
.main{
margin-left: 300px; /* because of .mymenu's width */
}
<div class="mymenu">
Link1<br />
Link2<br />
Link3
</div>
<div class="main">
<?php
include("things.php"); /* open things.php */
?>
</div>
things.php
/* get p from URL */
if (isset($_GET["p"])) { $p = $_GET["p"]; } else { $p = ""; }
function anotherThing(){
echo "anotherThing page";
}
function thatThing(){
echo "thatThing page";
}
function thisThing(){
echo "thisThing page";
}
/* called from e.g. http://yourwebsite.com/index.php?p=whatever2 */
switch($p) {
case "whatever3":
anotherThing();
break;
case "whatever2":
thatThing();
break;
case "whatever1":
default:
thisThing();
break;
}
I've been trying to do something very complicated (in my opinion, because it doesn't quite work the way I want it to). What I want, is that I have a text field where I type in something, then next to that is a button, and when you type in something in the text field, then press the button a new html element will appear (a box with in there the text you typed in the text field). Now, I've been trying this many different ways. Here's the code I'm using now:
<html>
<head>
<style>
div.cat {
width: 200px;
height: 25px;
border: 1px solid black;
}
</style>
</head>
<?php
$form = "<form method=\"post\" action=\"<?php echo $_SERVER['PHP_SELF']; ?>\"><input type=\"text\" name=\"name\"><br><input type=\"submit\" value=\"Submit\"></form>";
function newCategory() {
$categoryname = $_POST['name'];
$category = "<div class=\"cat\"> <?php $categoryname ?> </div>"
echo $category;
}
?>
<body>
<?php
echo $form;
newCategory();
?>
</body>
</html>
Now, the issue seems to be that if you press the button, the newCategory() function won't execute again. Please help!
EDIT: The issue was in the syntax errors. It's still not working the way I want it to work, though I now know what went wrong. I guess I have to use JavaScript.
try this code...
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<input type="text" id="text1"/>
<input type="button" id="btn" value="Button"/>
<div id="div1">
</div>
<script>
$('#btn').click(function(){
var gettext=document.getElementById("text1").value;
var temp=document.createElement("input");
temp.id="text2";
temp.value=gettext;
$("#div1").html(temp);
});
</script>
You have syntax error,
Replace this to your whole code with this, it is working well
<html>
<head>
<style>
div.cat {
width: 200px;
height: 25px;
border: 1px solid black;
}
</style>
</head>
<?php
$form = "<form method='post' action='".$_SERVER['PHP_SELF']."'><input type='text' name='name'><br><input type='submit' value='Submit'></form>";
function newCategory() {
$categoryname = $_POST['name'];
$category = "<div class='cat'>".$categoryname."</div>";
return $category;
}
?>
<body>
<?php
echo $form;
echo newCategory();
?>
</body>
</html>
The problem is in this line of code: $category = "<div class=\"cat\"> <?php $categoryname ?> </div>".
You cannot have php tags <?php ?> inside another set of php tags.
Just change it to this: $category = "<div class=\"cat\">".$categoryname."</div>";.
And make sure you fix all the other syntax errors as well.