This question already has answers here:
PHP Text Field value shows incomplete data, only 1 word
(5 answers)
Closed 11 months ago.
I want to create a page which allows a user to select colours (among the listed ones, via checkboxes). When the user clicks on the submit button, all the selected colours should get displayed with their corresponding colours as their font colour.
Here is my code:
<?php
if(!isset($_POST['button'])) {
?>
Colours<br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post>
<input type="checkbox" name="colours[]" value="Red">Red<br>
<input type="checkbox" name="colours[]" value="Blue">Blue<br>
<input type="checkbox" name="colours[]" value="Green">Green<br>
<input type="checkbox" name="colours[]" value="Yellow">Yellow<br>
<input type="checkbox" name="colours[]" value="Pink">Pink<br>
<input type="submit" name="button" value="Show"> </form>
<?php
} else {
if (isset($_POST['colours'])) {
echo "Colours selected are:<br><UL type=circle>";
foreach($_POST['colours'] as $color)
echo "<LI><font color='echo $color'; >$color</font>";
echo"</UL>";
} else {
echo "No colour selected";
}
}
?>
There is a bug somewhere, the font colors are not as expected.
I want it like this image below:
Simply write
echo "<li><font color='$color'> $color </font></li>"; // complete li tag
other mistakes:-
a) form method attrubute should be method='post'
b) UL type=circle should be ul type='circle'
When appending a string to variable or another string, you must concatenate the values with a . (dot). for example:
echo("<li><font color=".$color.">".$color."</font>");
Try this;
echo "Colours selected are:<br><UL type=circle>";
foreach($_POST['colours'] as $color) {
echo " <LI><font color='".$color."'>'".$color."'</font></LI>";
}
echo"</UL>";
Related
This question already has answers here:
two values for one name in input
(5 answers)
Closed 1 year ago.
I am trying to send radio button value with radio button text to php.
I'm able to send radio button value, i want to send the text after the radio button too without changing the checkbox information, however i cannot send the text to php.
<input name="list" type="checkbox" value="15.00" />Shirt
<input name="list" type="checkbox" value="19.00" />Trousers
<input name="list" type="checkbox" value="16.00" />Shorts
<input name="list" type="checkbox" value="14.50" />Skirt
I'm trying to send the following text(s);
Shirt,
Trousers,
Shorts,
Skirt.
When each checkbox is selected i want to send the text to php too.
Looks like you want this...
<?php
if(isset($_POST['li1'])){ echo $_POST['li1']." Shirt "; }
if(isset($_POST['li2'])){ echo $_POST['li2']." Trousers "; }
if(isset($_POST['li3'])){ echo $_POST['li3']." Shorts "; }
if(isset($_POST['li4'])){ echo $_POST['li4']." Skirt "; }
?>
But your html code should be like
<input name="li1" type="checkbox" value="15.00" />Shirt
<input name="li2" type="checkbox" value="19.00" />Trousers
<input name="li3" type="checkbox" value="16.00" />Shorts
<input name="li4" type="checkbox" value="14.50" />Skirt
You cannot use the same name for inputs unless
(1) You're trying to create an array from the inputs
or (2) Your input is a radio button or a select > option
So I'm doing this website with Joomla and I don't have much experience with Joomla.
I just want to get the values of some checkboxes and print them into a text area.
I have the script already, I just need to implement it.
Here the raw Html code of the checkboxes:
<div class="checkboxShop">
<form action="" method="post">
<input type="checkbox" value="1" id="checkboxShopInput" name="shop" />
<label for="checkboxShopInput"></label>
</form>
</div>
And Here is the raw Html code of the submit button:
<form action="/Flex/index.php/shop" method="post">
<input type="submit" value="Buy">
</form>
All I want is to get all the values of the checkboxes and add them to a textarea in index.php/shop. I wrote this code to get the values but i don't know where to add this:
<?php
if(!empty($_POST['shop'])) {
foreach($_POST['shop'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
echo "<input type='text' name='message' value='" . $check . "'>"
}
}
?>
I've read that i need this but I'm not sure how: <?php include "name_of_script.php";?>
Thanks for the help
This question already has answers here:
Getting checkbox values on submit
(10 answers)
Closed 6 years ago.
i am knew to php, and i want to know if there is a way to show the input based on the names that were checked, for example i have cat, dog and fish, if i choose dog and fish, i have to type the names of those animals.
here is the code to ask what animal do you have, and by checking and submit, it appears on the other page
<body>
<?php
$check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:"";
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
?>
<form action="names.php" method="POST">
<h1>choose the animals you have, and type their names</h1>
<input type="checkbox" name="check_list[]" value="cat">
cat
<br>
<input type="checkbox" name="check_list[]" value="dog" >
dog
<br>
<input type="checkbox" name="check_list[]" value="fish" >
fish
<br>
<input type="submit" value="next">
</form>
and here is the other page, that show what animals you have selected, and on this page i want to show the inputs based on the checked
<body>
<?php
echo '<h3>You have select following animals</h3>';
foreach ( $_POST ["check_list"] as $animals )
echo '<p>' . $animals . '</p>';
?>
So in your code,
just replace $check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:""; with $check_list = isset($_POST["check_list"]) ? $_POST["check_list"] : ""; and you should be good.
Also your variable $animals always carry one animal so it should be named animal.
and this:
$check_list[]=isset($_GET["check_list"])?$_GET["check_list"]:"";
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
can be improved to:
$check_list = isset($_GET["check_list"]) ? $_GET["check_list"] : null;
if ($check_list) {
foreach($check_list as $animal) {
echo "<p>My animal is: $animal</p>";
}
}
Next step is to learn more about security checks on user data!
Respectfully, good luck for you learning. PHP is beautiful ;)
This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 7 years ago.
I have an HTML/PHP form that lists multiple inputs in which users can change values. I have a PHP while loop create the fields like so:
...
$result = mysql_query($query);
while (list($a,$b,$c) = mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>$a</td>';
echo '<td><input name="b" type="text" value="'.$b.'"</input></td>';
echo '<td><input name="c" type="text" value="'.$c.'"</input></td>';
echo '</tr>';
}
...
For this example I can have multiple lines of a,b,c and want to get all of the values when I submit the form via POST. Only $b and $c are input values that can be changed. Do I create variables $a,$b,$c as arrays, and if so, how do I set that up so that all of the values will be stored?
submit your inputs with name like b[] & c[]
HTML
<input type="hidden" name="a[]" value="a_value" />
<input type="text" name="b[]" value="b_value" />
<input type="text" name="c[]" value="c_value" />
PHP
<?php print_r($_POST['a']); ?>
<?php print_r($_POST['b']); ?>
<?php print_r($_POST['c']); ?>
I am using Zend framework. In this i create a model and put my database connection in this model.
Here is my code so far :-
public function getTagusers(){
try {
$stat = $this->db->query("select a.tagCode child, b.tagCode parent " .
"from tag a, tag b where a.tagParentId=b.tagId");
$aResultData = $stat->fetchall();
}
catch(Exception $e){
error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '
. $e->getMessage());
}
return $aResultData;
}
Now I am using action in controller. My code is so far :-
public function listAction()
{
$tagusers =new Admin_Model_DbTable_Tagusers();
$this->view->taguser =$tagusers->fetchall();
}
Now finally i want to echo my data in view list.html. My code is so far :-
<script>
<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
} else {
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}
// End -->
</script>
<?php foreach($this->taguser as $taguser) ?>
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<input type="checkbox" name="check_list" value="1">
<?php echo $this->escape($taguser->tagCode);?><br>
<input type="checkbox" name="check_list" value="2">
<?php echo $this->escape($taguser->tagParentId);?><br>
</form>
But I am not able to echo the data properly. Can anyone explain me what I can do to echo the result according to my query.
first if you using Zend Framework as a framework as it appears... your first error is it normal for viewscripts to have the .phtml extension (i know you may have changed this).
next your php is incorrect:
<?php foreach($this->taguser as $taguser): //need to colon for alternate loop syntax ?>
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<input type="checkbox" name="check_list" value="1">
<?php echo $this->escape($taguser->tagCode);
//if this causes errors use
//array notation $taguser['tagCode']?><br>
<input type="checkbox" name="check_list" value="2">
<?php echo $this->escape($taguser->tagParentId);?><br>
</form>
<?php endforeach //need to end the foreach statement alternate syntax?>
I'm not going to critique your form, if you want built a separate form for each record that's your business.
Well, you're printing N forms (where N = count($this->taguser)), each one containing 3 checkboxes with the same value ('yes', '1' and '2', respectively), which makes no sense at all.
If I'm correct, your form should look like this:
<form name="myform" action="checkboxes.asp" method="post">
<b>Select Allowed keywords below:</b><br>
<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
<br>
<?php foreach($this->taguser as $taguser): ?>
<input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagCode);?>">
<br>
<input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagParentId);?>">
<br>
<?php endforeach; ?>
Still, you should read about Zend_Form. It will be hard to understand it in the beginning, but it's totally worth it.