I have an HTML form as:
<form action="something.php" method="POST" enctype="multipart/form-data" id="color">
<fieldset>
<legend><h2><span style="color: #993300; text-shadow: #808080 2px 1px 2px; font-size: 25px;">Color Preferences:</span></h2></legend>
<p><table>
<tbody>
<tr>
<td width="350px;"><span style="color: #993300;">*</span> Colors you consider to use to your new Site:</td>
<td>
<br/>
<input type="checkbox" name="color[]" value="Black" /> Black<br/>
<input type="checkbox" name="color[]" value="Blue" /> Blue<br/>
<input type="checkbox" name="color[]" value="Brown" /> Brown<br/>
<input type="checkbox" name="color[]" value="Gray" /> Gray<br/>
<input type="checkbox" name="color[]" value="Green" /> Green<br/>
</td>
</tr>
</tbody>
</table></p>
</fieldset>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
To execute the color form, I used a PHP as:
$color = $_POST['color'];
if (!isset($color))
{
unset($_GET['do']);
$message = "Error = Colors are required. Please try again.";
break;
} else {
$N = count($color);
for($i=0; $i < $N; $i++){
echo($color[$i] . " ");
}
}
But when I test my code over the output area it only giving an "Array" as a result.
Can you please help me to fix this code?
(I need to get the selected colors as an output instead of "Array".)
I even changed my codes into:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
I even changed my codes into:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
Try this:
PHP Code:
if (isset($_POST['submit'])) {
if (!isset($_POST['color'])) {
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$color = $_POST['color'];
$N = count($color);
for ($i = 0; $i < $N; $i++) {
echo($color[$i] . " ");
}
}
}
with color[], you will receive an array ($_POST['color']). You can test if it is isset() or it is empty()
if it is NOT empty, then you can iterate over the values for instance like this:
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
(untested. Code taken from here: http://www.html-form-guide.com/php-form/php-form-checkbox.html)
Related
can you help me with this problem, i want make matrix with 2 forms, the first form is for how much is row and column is needed and second form is use for input value of array,
I was stuck in how to display it but if there are other mistake please tell me... thx u :))
this for input row and column
<form method="POST" action="prak_10.php">
Masukkann n : <input type="text" name="bilangan_n">
<br>
Masukkann m : <input type="text" name="bilangan_m">
<input type="submit" name="submit" style="background: red;color:white;border-style: none;padding: 5px;" name="" value="Hasil">
and this for input value of matrix
<?php
$n = $_POST['bilangan_n'];
$m = $_POST['bilangan_m'];
if (isset($_POST['submit'])){
?><form method="POST" action="prak_10.php">
<?php
for ($i = 0; $i < $n; $i++) {
for ($j=0; $j < $m; $j++) {
echo "Masukkan nilai array baris : ".$i." kolom : ".$j.": <input type='text' name='nilai[][]'><br>";
}
}
?>
<input type="submit" value="Hasil">
</form>
<?php }
for ($i = 0; $i < count($_POST['nilai']); $i++) {
for ($j=0; $j < count($_POST['nilai']); $j++) {
echo $_POST['nilai'][$i][$j]." ";
}
echo "<br>";
}print_r($_POST['nilai']);
?>
Change This line
echo "Masukkan nilai array baris : ".$i." kolom : ".$j.": <input type='text' name='nilai[".$i."][".$j."]'><br>"
i want make a view of array, in the first i need fill length of array for first form (this for how much is show) and then i use that to fill value of array then i show that.
<form method="POST" action="prak_9_1.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
second code is different file
<?php
$bilangan = $_POST['bilangan'];
$bil = $bilangan;
$array = array();
$n = 0;
?>
<form method="POST" action="prak_9_1.php">
<?php
for ($i=0; $i < $bilangan ; $i++) {
echo "input value of array : <input type='text' name='nilai_".$i."'><br>";
}
?>
<input type="submit" value="Hasil">
</form>
<?php
for ($i=0; $i < $bil ; $i++) {
$array[$n] = $_POST['nilai_'.$i];
$n++;
}
print_r($array);?>
sorry if my english is bad... thanks for ur helps...
first file :
<form method="POST" action="second.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
</form>
and second file :
<?php
if (isset($_POST['bilangan'])) {
echo '<form method="POST">';
for ($i = 0; $i <= $_POST['bilangan']; $i++) {
echo "input value of array : <input type='text' name='nilai[]'><br>";
}
echo '<input type="submit" value="Hasil">';
echo '</form>';
}
if (isset($_POST['nilai'])) {
print_r($_POST['nilai']);
}
?>
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
I am creating form where the user can input a integer and get the multiplications (1-10) of that number. Here is my web form for user input:
<html>
<head>
<title>Assignment 9.1</title>
</head>
<body bgcolor="black" text="white">
<form method="post" action="table.php"
<strong>Enter No:</strong>
<input type="text" name="num" size="10">
<input type="submit" value="Get Table">
</form>
</body>
</html>
The table I have created is this:
<?php
$num = $_POST['num'];
if($num)
(
for ($i=1; $i<=10; $i++)
(
$mul = $num * $i;
echo "$num * $i = $mul<br>";
)
)
else
(
echo "Invalid Entry!";
)
?>
I am getting an error for the table. The error is for line 5 (FOR). I have no idea why I am getting this error. Can anyone Help?
Blocks of code are marked with curly brackets { ... }, you are currently using parentheses ( ... )
As far as I can tell, that's pretty much the only thing wrong with it. You might want to add a bit of validation:
$num = isset($_POST['num']) ? intval($_POST['num']) : 0;
But that'd just a touch-up. The brackets are your actual problem.
You need to use {} instead () in the for and if.
Like this:
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
Hope it helps you!
You are using parentheses when you should be using braces. Try this...
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
save the file as table.php and run working 100%
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul </center><br>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul <br></center>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
I have html form that looks like:
<form method="post" action="?a=up">
...some mysql query...
while ($i = mysql_fetch_array($result)) {
<input name="name[]" type="text" value="<?=$i['name'];?>" />
<input name="years[]" type="text" value="<?=abs($age);?>"/>
<input name="to[]" type="checkbox" value="<?=$i['id'];?>" />
}
<input name="" type="submit" value="go" />
</form>
The problem I have is that I can not get the values of the form fields such as "name" and "years".
I can only get a list of the ids (value of "to" checkbox).
The php code looks like:
$cnt = 0;
for($p = 0; $p <= (sizeof($to)-1); $p++)
{
echo $to[$p].$name[$p].$years[$p]"<br>";
$cnt++;
}
$tm = array($cnt);
What I'm doing wrong?
You're expecting a checkbox to be a successful control even if it isn't checked (and the specification says that it must not be so).
You should probably do something along the lines of:
<input name="name[<?php echo htmlspecialchars($i['id']); ?>]"
value="<?php echo htmlspecialchars($i['name']); ?>" />
<input name="years[<?php echo htmlspecialchars($i['id']); ?>]"
value="<?php echo abs($age);?>"/>
Update
Here is how you can get checkboxes that are checked using isset:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
if (isset($_POST['to'][$p]))
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
}
$tm = array($cnt);
}
You are not getting the fields from POST array, here is how your code should be:
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
$tm = array($cnt);
Make sure that above code executes when form is submitted by putting it in this condition:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
$tm = array($cnt);
}
And finally a little suggestion that you should avoid using short php tags <?=?> for they have posed security issues and can easily be embedded into images or xml. (Make sure that they are also turned on from php.ini if you want to use them)
Why dont you just try
$cnt = 0;
foreach ( $_POST['to'] as $k => $to ){
echo $_POST['to'][$k] . $_POST['name'][$k] . $_POST['years'][$k] . "<br />";
$cnt ++;
}
$tm = array ($cnt);