hi there ...

i want to Handel a select field in php but i could not ..so i was wondering if someone can help....


what i want is there will numbers from 3 to 40 listed in select field and when user select 3
3 text box will appear and if he select 10 then 10 box appear ....and so on

thanks in advance...

Recommended Answers

All 17 Replies

What have you tried? Please post your existing code.

And a small hint would be for you to use the value from the select box in a for loop...

R.

<html>
	<head>
		<script language="JavaScript">
			function make_textbox(no)
			{ 
				if(no < 1)
				  return false;
				  
				var my_textbox = "";
				
				for(var i=0;i<no;i++)
				{
					my_textbox += "<br>Text Box "+eval(i+1)+": <input type='text' name='textfield["+i+"]'>";
				}
				
				document.getElementById("text_id").innerHTML = my_textbox;
			}
				
		</script>
	</head>
			
	<body>
			
			<form name="myfrm">
				
				Select Number Of TextBox : <select name="select_num_text" onChange="JavaScript:make_textbox(this.value);">
					<option value="">Select</option>
					<?php
					
						for($i=3;$i<=40;$i++)
						{
							echo "<option value='".$i."'>".$i."</option>";
						}
					?>
				</select>
				<br>
				<div id="text_id"></div>
			</form>
	</body>
</html>

dear Manish Singh

thanks a lot man for this ..it was what i am looking for thanks man ...

Always Welcome....

Do you want same out put using jquery ?
if yes i will be happy to help...

thanks to Manish Singh he totaly solve my problem

Manish Singh one more thing how i can submit these data to database? thanks in advance

Please let me know structure of your table.

<?php

if($_POST['submit'] == "Submit")
{
	$textbox = $_POST['textfield'];
	
	if(is_array($textbox))
	{
		foreach($textbox as $textkey=>$textvalue)
		{
			$textvalue = trim($textvalue);
			
			if($textvalue <> "")
			{
				//your insert query......
			}
		}
	}
	
}

?>
<html>
	<head>
		<script language="JavaScript">
			function make_textbox(no)
			{ 
				if(no < 1)
				  return false;
				  
				var my_textbox = "";
				
				for(var i=0;i<no;i++)
				{
					my_textbox += "<br>Text Box "+eval(i+1)+": <input type='text' name='textfield["+i+"]'>";
				}
				
				document.getElementById("text_id").innerHTML = my_textbox;
			}
				
		</script>
	</head>
			
	<body>
			
			<form name="myfrm" method="POST">
				
				Select Number Of TextBox : <select name="select_num_text" onChange="javascript:make_textbox(this.value);">
					<option value="">Select</option>
					<?php
					
						for($i=3;$i<=40;$i++)
						{
							echo "<option value='".$i."'>".$i."</option>";
						}
					?>
				</select>
				<br>
				<div id="text_id"></div>
				<input type="submit" name="submit" value="Submit">
			</form>
	</body>
</html>

i just want to to add the enterd value in the input boxs to simble table where the it only store the vlue of the data enterd

manish i am really thanks full to u but what if there is other fields is the code going to be disturbed

like

if i want to submit to data base the flowing:

Name (input field)
Email (input field)
Contact No (input field)
Number of cars you want to insure (this the drop box)
and if the user select from the drop down 4 the 4 input text will appear and then he will put the car registration numbers and when he press submit button the name will store in name colum in the database and email and contact no it will also and then each value of the text box will store in it is coulm like car 1 with regestation numer and the car2 with regestation number and so on

is that possible? thanks man u helped me a lot

I think you will have to make two tables (use normalization of table)

1. car_owner

id (primary key, Auto incr.)
name
email
contact

2. car_registration

id (primary key , Auto incr.)
owner_id (foreign key)
reg_no



<?php

if($_POST['submit'] == "Submit")
{
	
	$name = $_POST['name'];
	$email = $_POST['email'];
	$contact = $_POST['contact'];
	$textbox = $_POST['textfield'];
	
	$query = "insert into car_owner(name,email,contact) values ('".$name."','".$email."','".$contcat."') ";
	 mysql_query($query);
	
	$last_insert_id = mysql_insert_id();
	
	
	if(is_array($textbox))
	{
		foreach($textbox as $textvalue)
		{
			$textvalue = trim($textvalue);
			
			if($textvalue <> "")
			{
				$sub_query = "insert into car_registration(owner_id,reg_no) values('".$last_insert_id."','".$textvalue."') ";
				mysql_query($sub_query);
			}
		}
	}
	
}

?>

NOTE :

1. if you have 50 text boxes and user will fill only one text box.
rest of 49 will be wastage of storage.

2. Later if u want to display 55 boxes, then no need to change in code only increase $i<=55.

Manish Singh thanks for reply u were very help full i will try that and i will let u know

Manish Singh what if i want to use these values to send an email...

what i mean is when user submit the form it will submited to test@test.com...

thanks man for ur time...

<?php


if($_POST['submit'] == "Submit")
{

	$name = $_POST['name'];
	$email = $_POST['email'];
	$contact = $_POST['contact'];
	$textbox = $_POST['textfield'];
	
	
	$mail_body = "<html><body>";
	$mail_body .= "<b>Name : </b>".$name;
	$mail_body .= "<br><b>Email : </b>".$email;
	$mail_body .= "<br><b>Contact : </b>".$contact;
	
	
	$query = "insert into car_owner(name,email,contact) values ('".$name."','".$email."','".$contcat."') ";
	 mysql_query($query);
	
	$last_insert_id = mysql_insert_id();
	
	$cnt = 1;
	
	if(is_array($textbox))
	{
		foreach($textbox as $textvalue)
		{
			$textvalue = trim($textvalue);
			
			if($textvalue <> "")
			{
				$mail_body .= "<br><b>Registration No ".$cnt." : </b>".$textvalue;
				$cnt ++;
			}
		}
	}
	
	
	$mail_to = "manish.s@neuralit.com"; //mail id to whom you r sending mail
	$mail_to_name = "Manish Singh"; //Name to whom you r sending mail
	$subject = "Your subject";
	
	$mail_from = "from@test.com"; //Sender mail id
	$mail_from_name = "FROM"; //Sender name
	
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	

	// Additional headers
	$headers .= 'To: '.$mail_to_name.' <'.$mail_to.'>' . "\r\n";
	$headers .= 'From: '.$mail_from_name.' <'.$mail_from.'>' . "\r\n";
	$headers .= 'Reply-To: '.$mail_from_name.' <'.$mail_from.'>'."\r\n";

	// Mail it
	if(mail($mail_to, $subject, $mail_body, $headers))
		echo "mail sent successfully.....";
	else
		echo "Error!! while sending mail";
	
}

?>

Manish Singh.....hope u ok ..i have other problem, what if i want to add 2 text box instead of one..what i mean when the user select 5 ,5box will appear but in one line it will ask him his first name and last name and how i can submit these to email like the what u did in the previous code.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.