Jump to content

[SOLVED] multiple select box


barnes

Recommended Posts

Your results with come in the $_POST as an array.

Since storing as an array in the same way as php in a database doesn't work, I generally use a character in between all the values, and use that character to make it an array again using explode when I need it.

 

$array = $_POST["name"];

$counter = 0;
foreach($array as $item){
//you can use any character you want, I'll use a comma for this example
if($counter <> 0){$separator = ",";}
$string_to_db .= "$separator$item";
++$counter;}

 

When you want to make it back into an array after retrieving it from the database, simply use:

$array = explode(",",$row["string_from_db"]);

 

Again, you can use any other character besides ",".

An array is a list of items, but stored in a single variable.

To get to all the items in the array, you need to use various methods of getting each one.

Using foreach or a while are the easiest ways.

 

The foreach I made above will loop through each item (the selected items from your multi select box) and put them in a single piece of text.

 

For example, a user selects "Germany" and "England" and "France".

The foreach loop I made above will put all those in a single string, which should look like: "Germany,England,France".

 

That's what you put in the database, and if you want to put it back into an array when you retrieve it from the database, the explode(); function above will do just that.

 

Also, don't forget about http://www.php.net, they got a lot of stuff on Arrays.

many thanks thierry,thanks for ur patience..

but i am getting error at

$string_to_db .= "$separator$item";

Notice: Undefined variable: separator in c:\easyphp1-8\www\register.php on line 25

 

Notice: Undefined variable: string_to_db in c:\easyphp1-8\www\register.php on line 25

Make sure the first value used in the foreach (the $array) has the contents of your multi select box by giving them the same name in the select box and the $_POST.

 

This means that the name and id below

<select name="whatever[]" id="whatever[]">

 

Have to match the giving $_POST variable:

$array = $_POST["whatever"]

 

Don't forget to add the "[]" behind the names in your select box, only there.

register.html

<form action="register.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="410" border="0" align="center" bordercolor="#99FFFF">
    <tr align="center" valign="middle">
      <td colspan="3"> <span class="style1">User Registration</span> </td>
    </tr>
    <tr>
      <td width="156" align="left"><div align="right" class="style4">UserName</div></td>
      <td width="22">:</td>
      <td width="218"><div align="left">
        <input name="username" type="text" />
      </div></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">Password</div></td>
      <td>:</td>
      <td><label>
        <label>
        <div align="left">
          <input name="password" type="password" id="password" />
        </div>
        </label>
        <div align="left"></div>
        <div align="left"></div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">E mail</div></td>
      <td>:</td>
      <td><label>
        <div align="left">
          <input type="text" name="email" />
        </div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">Address</div></td>
      <td>:</td>
      <td><label>
        <div align="left">
          <textarea name="address"></textarea>
        </div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">Country</div></td>
      <td>:</td>
      <td><label>
        <div align="left"><select name="country" onChange="d.updateOptions(this, 'area[]');">
<option value="">[select]</option>
<option value="usa">usa</option>
<option value="india">india</option>

</select></div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">City/Area</div></td>
      <td>:</td>
      <td><label>
        <div align="left">
          <pre><select name="area[]" size="5" multiple="multiple"></select>
</pre>
          <label></label>
        </div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">Pin Code </div></td>
      <td>:</td>
      <td><label>
        <div align="left">
          <input name="pin" type="text" id="pin" />
        </div>
      </label></td>
    </tr>
    <tr>
      <td><div align="right" class="style4">Phone Number </div></td>
      <td>:</td>
      <td><label>
        <div align="left">
          <input name="countrycode" type="text" id="counrtycode" size="3" maxlength="3" />
          <input name="phoneno" type="text" id="phoneno" />
        </div>
      </label></td>
    </tr>
    <tr>
      <td><div align="left" class="style4">
        <div align="right">Upload Your Photo </div>
      </div></td>
      <td><div align="left">:</div></td>
      <td><label>
        
        <div align="left">
          <input type="file" name="file" />
        </div>
      </label></td>
    </tr>
    <tr>
      <td colspan="3"><label>
        <div align="center">
          <input type="submit" name="Submit" value="Submit" />
        </div>
      </label></td>
    </tr>
  </table>
</form>

 

 

dSelect.js

var d = new dynamicSelect();

d.addSelect('country');

d.selects['country'].addOption('usa');
d.selects['country'].options['usa'].createOption('florida', 'florida');
d.selects['country'].options['usa'].createOption('boaraton', 'boaraton');

d.selects['country'].addOption('india');
d.selects['country'].options['india'].createOption('delhi', 'delhi');
d.selects['country'].options['india'].createOption('hyd', 'hyd');
d.selects['country'].options['india'].createOption('chennai', 'chennai');


//End setup [Edit above here]



function dynamicSelect()
{
this.selects = new Array();

this.addSelect = function(name)
{
	this.selects[name] = new selectObj();
}



this.updateOptions = function(source, target)
{
	var form = source.form;
	var target = form.elements[target];
	var value = source.options[source.selectedIndex].value;

	while(target.options.length) target.remove(0);

	if(!this.selects[source.name].options[value])
	{
		//alert('Invalid selection.'); //For debugging while you set it up
		return;
	}

	var data = this.selects[source.name].options[value].options;

	for(var x=0; x<data.length; x++)
	{
		try
		{
			target.add(data[x]);
		}
		catch(e)
		{
			target.add(data[x], null);
		}
	}

	target.selectedIndex = 0;
}

}



function selectObj()
{
this.options = new Array();

this.addOption = function(value)
{
	this.options[value] = new optionObj();
}
}



function optionObj()
{
this.options = new Array();

this.createOption = function(name, value)
{
	this.options[this.options.length] = new Option(name, value);
}
}

 

 

resiter.php

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("user",$con);
  $username = mysql_real_escape_string($_POST['username']);
  $password = mysql_real_escape_string($_POST['password']);
  $email = mysql_real_escape_string($_POST['email']);
  $add = mysql_real_escape_string($_POST['address']);
  $contry=mysql_real_escape_string($_POST['country']);
$array = array($_POST['area']);

$counter = 0;

$string_to_db = "";
$separator = "";
foreach($array as $item){

if($counter <> 0)

$separator = ",";

$string_to_db .= "$separator$item";
++$counter;
}
  $pin=mysql_real_escape_string($_POST['pin']);
  $countrycode=mysql_real_escape_string($_POST['countrycode']);
  $phoneno=mysql_real_escape_string($_POST['phoneno']);
  if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
   {
    echo "Error:". $_FILES["file"]["error"] ."<br>";
   }
else
{
  echo "upload:". $_FILES["file"]["name"] ."<br>";
  echo "type:". $_FILES["file"]["type"] ."<br>";
  echo "size:". ($_FILES["file"]["size"]/1024) ."kb<br>";
  echo "stored in:". $_FILES["file"]["tmp_name"] ."<br>";
  if (file_exists("upload/" . $_FILES["file"]["name"]))
{ 
   echo $_FILES["file"]["name"]."already exists.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
echo "stored in".$_FILES["file"]["name"];
} 
}
}
else
{
echo"invalid file";
}
  $qry="INSERT INTO user1 (username,password,email,address,country,area,pincode,countrycode,phoneno) VALUES ('{$username}','{$password}','{$email}','{$add}','{$contry}','{$string_to_db}','{$pin}','{$countrycode}','{$phoneno}')";
  mysql_query($qry,$con);
  mysql_close($con);
  ?>

please do modification..again same problem arraised ,last selected value is storing..

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.