HTML form multiple fields - php

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);

Related

php word check if they are the same

I need that they check my word witch i written in text field. If 2 words the same i should get message - "They are the same".
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("snow", "ball", "side");
//print_r(array_random($a));
//print_r(array_random($a, 3));
?>
<form name='form' method='post' align = "center">
<?php
$zod = (array_random($a)); echo $zod; ?> <input type="text" name="name" id="name" ><?php
if((isset($_POST['name'])) && !empty($_POST['name']))
{
$name = $_POST['name'];
echo ' '.$name;
}
?><br/><br>
<input name="select" type="submit" onclick="select()" value="select" />
</form>
A simple comparison uses the '==' operator. These can be used to compare something. You want to compare a posted value with the one from your array. which is random:
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
if ( isset ( $_POST['submit'] ) && isset ( $_POST['name'] ) ) {
if ( $_POST['compare'] == $_POST['name'] )
echo "They are the same, hurray!<br />";
else
echo "These don't match!";
}
$a = array ( "snow", "ball", "side" );
$rand = array_random ( $a );
?>
<form name='form' method='post' align = "center">
Please type the word: <?php echo $rand; ?>
<input type="text" name="name" id="name" >
<input type="hidden" name="compare" id="compare" value="<?php echo $rand ?>" >
<input name="submit" type="submit" onclick="select()" value="select" />
</form>
As you wanted, a random value is picked and compared to what the user posted.

Processing radio buttons in loop in PHP

I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.
test.php
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<form action = 'test.php' method = 'post'>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
</form>
<?php
}
?>
<input type = 'submit' value = 'Go'>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).
try this way
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
use following code: Or you can use radiogroup array it is easier than this.
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
#Cagy79 added additional submit buttons.. I have revised the code,
<form action ="" method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
This works. :)
You have 3 forms without a submit button (it needs to be in between the tags). You need to create one form with one submit button like this so all your data is posted to $_POST.
<form method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>

Creating checkboxes in HTML / PHP?

So I have the following code.
My main.html form lets me enter a value (a price for pc) and when I submit it calls the pcs.php script which displays all my pcs under the given value/price.
But I want to have an checkbox beside the information displayed by the script. ie
<br>alienware, 3000, gtx760 checkbox <br> asus rog, 2500, radeodhdxxxx checkbox <br>
and so on).
//main.html
<BODY>
<FORM METHOD="POST" ACTION="pcs.php" >
<INPUT TYPE='textbox' NAME='mypc' VALUE='' >
<INPUT TYPE="SUBMIT" NAME="Button" VALUE="Hit me">
</FORM> </BODY> </HTML>
//pcs.php
<?php
$dd = $_POST['mypc'];
$filename = "computer.txt";
$filepointer = fopen($filename,"r");
$myarray = file ($filename);
for ($mycount = 0; $mycount < count($myarray); $mycount++ )
{
$apc = $myarray[$mycount];
$price = getvalue($apc,1);
$part = explode(',', $apc);
//print $price ."<br>";
//print $str ."<br>";
if ($str <$dd )
{
for($pcount = 0; $pcount<3; $pcount++) {
print $part[$pcount] ."<br>";
}
print "<br>";
}
}
function getvalue ($text, $commaToLookFor)
{
$intoarray = explode(",",$text);
return $intoarray[ $commaToLookFor];
}
// fclose ($filepointer);
?>
<p>
</body>
</html>
// computer.txt file
alienware, 3000, gtx760<br>
asus rog, 2500, radeonhdxxx<br>
alienware, 5000, gtx titan<br>
In pcs.php you'll just need to modify your for loop to look something like:
// Create form
print '<form method="post" action="script_to_post_to.php">';
for ($mycount = 0; $mycount < count($myarray); $mycount++) {
$apc = $myarray[$mycount];
$price = getvalue($apc,1);
$part = explode(',', $apc);
if ($str < $dd) {
for($pcount = 0; $pcount<3; $pcount++) {
print $part[$pcount] ."<br>";
}
// Add checkbox and name with product type
print '<input type="checkbox" name="' . getvalue($apc, 2) . '" />';
print "<br>";
}
}
// Provide form submission button and close form element
print '<input type="submit" value="Submit" />';
print '</form>';
You'll have a form with each element having a checkbox and the ability to respond to the list of checked items in "script_to_post_to.php".

Form: make a loop to save different value of a same field name

I do a form, inside I would like to do a loop for to show the same field. Each field will have different value and I would like to use sessions to take all the value.
Here is my code:
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "Numero ";
echo $i;
?>
<input type="text" name="number2" id="number2"/>
<?php
}
?>
</form>
<?php
echo $_POST['number2'];
$my_array=array($_POST['number2']);
$_SESSION['countnumb']=$my_array;
in another page:
foreach($_SESSION['countnumb'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
I can't register any number. How can I do this? thanks
Basics First - ids should be unique in a webpage.
Declaring <input type="text" name="number2" id="number2"/> in a loop is wrong.
For creating multiple input using loop try like this-
echo "<input type='text' name='number[$i]' id='number{$i}' />";
<input type="text" name="number[2]" id="number2"/>
would make $_POST['number'] an array which you can loop though server side, This is described here http://www.php.net/manual/en/faq.html.php#faq.html.arrays
foreach ($_POST['number'] as $number){
echo $number;
}
for example
this would make your code
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "<input type="text" name="number[$i]" id="number{$i}"/> ";
?>
<?php
}
?>
</form>
<?php
print_r( $_POST['number'] );
$_SESSION['countnumb']= $_POST['number'];

Compute 1st value on multiple values on a checkbox

I was trying to make a COMPUTERIZED ORDERING SYSTEM. My problem is how can I compute the 1st value on my checkbox. The second value on the checkbox will be posted on the summary of orders.
Once I've check all those three, it will compute the total amount of the menu and to display the selected menu on the summary of orders. But I can't figure out how can I display the total amount.
Can anybody guide me how to compute the total on my 1st value on the checkbox?
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" name="go" value= "Total">
</form>
<?php
//tosilog
$ts = $_POST['ts'];
$value = explode("|",$ts[0]);
echo $value[0];
echo $value[1];
//chiksilog
$cs = $_POST['cs'];
$value = explode("|",$cs[0]);
echo $value[0];
echo $value[1];
//porksilog
$ps = $_POST['ps'];
$value = explode("|",$ps[0]);
echo $value[0];
echo $value[1];
?>
<!-- compute for the 1st value on checkbox -->
<?php
$ts=$_POST['ts[0]'];
$cs=$_POST['cs[0]'];
$ps=$_POST['ps[0]'];
?>
<?php $compute = $ts[0] + $cs[0] + $ps[0];?>
<?php echo "$compute " ; ?>
If you're getting an array of checkbox elements, and they are numeric, you can use array_sum(). Not sure I understand your suggested structure, but I'll give you a code sample here based on the existing form structure. Then I'll post another bit to try to make this simpler for you. Executive summary: You do not need all the variables that are created by this form structure.
<?php // RAY_temp_user193.php
error_reporting(E_ALL);
$total = 0;
$inputs = array();
$errors = array();
if (!empty($_POST))
{
if (!empty($_POST['ts']))
{
foreach ($_POST['ts'] as $ts)
{
$inputs[] = current(explode(' |', $ts));
}
}
else
{
$errors[] = 'Tosilog';
}
if (!empty($_POST['cs']))
{
foreach ($_POST['cs'] as $cs)
{
$inputs[] = current(explode(' |', $cs));
}
}
else
{
$errors[] = 'Chiksilog';
}
if (!empty($_POST['ps']))
{
foreach ($_POST['ps'] as $ps)
{
$inputs[] = current(explode(' |', $ps));
}
}
else
{
$errors[] = 'Porksilog';
}
// IF ERRORS
if (!empty($errors))
{
echo 'UNABLE TO PRINT COMPLETE TOTAL. MISSING: ' . implode(',', $errors);
}
$total = array_sum($inputs);
if ($total) echo "<br/>TOTAL: $total <br/>" . PHP_EOL;
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;
See http://www.laprbass.com/RAY_temp_miggy.php
This is probably more along the lines of the way I would do it. The script knows what goes into the HTML and it knows exactly what to expect in the POST request. It is easy to add or remove different inputs. Often these kinds of inputs come from a data base table.
<?php // RAY_temp_miggy.php
error_reporting(E_ALL);
$total = 0;
// EXPECTED INPUTS
$inputs = array
( 'Tosilog' => 40
, 'Chiksilog' => 40
, 'Porksilog' => 45
)
;
if (!empty($_POST))
{
// ACTIVATE THIS TO SEE WHAT WAS SUBMITTED
// var_dump($_POST);
// SUM OF THE ELEMENTS
$total = array_sum($_POST);
echo "TOTAL: $total";
// SUM OF THE EXPECTED INPUTS
$expect = array_sum($inputs);
if ($total != $expect) echo " BUT THERE MAY BE INCOMPLETE INPUTS!";
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$checkboxes = NULL;
foreach ($inputs as $name => $value)
{
$checkboxes
.= '<input name="'
. $name
. '" type="checkbox" value="'
. $value
. '" />'
. $name
. '<br/>'
. PHP_EOL
;
}
$form = <<<ENDFORM
<form method="post">
$checkboxes
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;

Categories