Jump to content

Listbox


pooja shona

Recommended Posts

Hi,I am trying to solve following php prob. from three days, but it fails.Please help me.

 

I have one php form with Name(textbox) and multivalue listbox with values mumbai, pune, nasik, surat, dhule. It work when user submits name and multiple cities as follows,

 

//insert.php

<?php

$con=mysql_connect("localhost","root","");

$db=mysql_select_db('state');

$name=$_POST['name'];

//$city=$_POST['city'];

$abc=$_POST['city'];

for ($i=0; $i<count($abc); $i++)

{

$city = $city." ".$abc[$i];

}

$submit=$_POST['submit'];

if(!empty($submit))

{

 

$query=mysql_query("insert into city(name,city) values('$name','$city')");

}

 

?>

 

Above code is working. But the problem is that, I want listbox on another page, this listbox should display the name of all cities, but should show all that cities which the selected while submitting the form with selected.

 

//show.php

<?php

$con=mysql_connect("localhost","root","");

$db=mysql_select_db('state');

$query=mysql_query("select city from city where id=7");

$row=mysql_fetch_array($query);

$one=$row["city"];

 

$pieces = explode(" ", $one);

 

//echo $pieces[0]; // piece1

 

?>

 

<body>

<select name="city[]" size=5 multiple="multiple">

<option value="pune"<?php if ($pieces[1] == 'pune')

{

echo

'selected="selected"';

} ?>>pune</option>

<option value="mumbai"<?php if ($pieces[2] == 'mumbai') { echo

'selected="selected"'; } ?>>mumbai</option>

<option value="nasik"<?php if ($pieces[3] == 'nasik') { echo

'selected="selected"' ;} ?>>nasik</option>

<option value="surat"<?php if ($pieces[4] == 'surat') { echo

'selected="selected"'; } ?>>surat</option>

<option value="dhule"<?php if ($pieces[5] == 'dhule') { echo

'selected="selected"'; } ?>>dhule</option>

 

</select>

</body>

 

It doen't work.Could anyone tell me that what should I do?

:'(

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/193980-listbox/
Share on other sites

For the insert.php script, how does your form look like?

 

On the show.php script, there are a few things I don't get;

Why are to splitting the $row["city"] variable? What does it hold in the database?

 

Your making something simple to complex here, my best advice is for you to try and do it in a more simpler way.

Link to comment
https://forums.phpfreaks.com/topic/193980-listbox/#findComment-1020824
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.