Jump to content

Input Boxes and Loop???


ali_2kool2002

Recommended Posts

Hi ,, im really  ??? confused  as im trying to create several input boxes for the user to enter information but was wondeering if anyone can code in php something which would create say 3 input text boxes but when the user clicks submit ,, the data can be checked if someone has entered anything. I need to use a loop thought as a random number (x) is used to loop x times, so if x is 3 it should create 3 input boxes..

 

P.s. The problem i get when doing this in a loop is the name of the text box is always the same as it goes through a loop,

and so say i wana check if the input box is empty in one ofthe textboxes , the php script would only check if all the input boxes are empty as they have the same name...the if statement i use in the handling code is

 

if (empty($_POST[quantity])) 

 

for the code input text box


echo '<td align =\"left\"> <INPUT TYPE=\"TEXT\" name =\"$quantity\" >';

 

the variable quantity is from the input text name i am using but some how need 2 get the name of the submit to change each time it goes in the loop depending on how many times it needs to loop (ie x's value) ??? ??? ??? ??? ??? ???

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/38806-input-boxes-and-loop/
Share on other sites

You could do...

 

// make inputs

$inputs = 3;

$name = 'input';

for ( $x = 1; $x <= $inputs; $x += 1 )
{
echo '<td align="left"> <input type="text" name ="' . $name . '[' . $x . ']" />';
}

 

// process the inputs

if ( ! empty ( $_POST['input'] ) )
{
foreach ( $_POST['input'] AS $key => $value )
{
	echo '$input[' . $key . '] = ' . $value . ';<br />';
}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/38806-input-boxes-and-loop/#findComment-186567
Share on other sites

why doswnt this work sorry for intrusion but what wrong with this.

 


<?php

echo"<form method='POST' action=''>
name:
<br>
<input type='text' name='result[]'>
<br>
password: 
<br>
<input type='text' name='result[]'>
<br>
email: 
<br>
<input type='text' name='result[]'>
<br>
<br>
<input type='submit' name='submit'>
<form>";


if($_POST['submit']){

foreach($result AS $key => $val);

echo "<br>name: ".$val.$key[0]."<br>";


echo "<br>password: ".$val.$key[1]."<br>";


echo "<br>name: ".$val.$key[2]."<br>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/38806-input-boxes-and-loop/#findComment-186603
Share on other sites

here u go a bit of a bum but done it have a test see what you think.

<?php

// echo the form.
echo"<form method='POST' action=''>";

//set array for the input names to echo.
$word=array("name","password","email");

// for loop 3 times and get the array. 
for($i=0; $i<3; $i++ && $word){
//echo the eords for the input boxs

//and show 4 input boxs via a array of result[].
echo"<br>$word[$i]<br>
<input type='text' name='result[]'>
<br>";
}

//show the submit box and close form.
echo"<br><input type='submit' name='submit'>
</form>";

//if the user press submit.
if($_POST['submit']){

//for loop the form for array result[].
for($i=0; $i<count($result); $i++){

//get varables to show if result exist.	
$name="name: ".$result[0]." ";
$password="password: ".$result[1]." ";
$email="name: ".$result[2]." ";
}

//if all data forom form  array[] exist. 
if($result[0] && $result[1] && $result[2]){

//echo the array[] set varables. 		
echo "<br>$name<br>$password<br>$email";

// else it result not exest. 	
}else{

// tell them that all the form must be filled in. 
echo "<br><br> sorry please fill in all the form"; 
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/38806-input-boxes-and-loop/#findComment-186652
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.