Solved

Form Checkboxes Dynamically returned

Posted on 2009-04-06
7
154 Views
Last Modified: 2012-05-06
Hi Experts,

I have a form which I have a number of checkboxes for as search criteria for cars, rather than manually insert the checkboxes I am pulling them from my database so it returns checkboxes like:-

Vauxhall
Ford
Seat

 etc etc

I originally named the checkboxes a value (vehicles) but when returning the results of the selected vehicles it only returns the latest selection.

I have given my checkbox name and values my vehicleid to identify the selections but can anyone help me as to how to get all the id's selected in a built up string with comma's so something like this:-

1,2,3 (1,2,3 being the vehicle id) so I can use this string in my SQL statement.

Thanks
Tim
The code:-

$sql='SELECT vehicle_id, display_name FROM vehicles';

$sqlresult=mysql_query($sql);

$sqlcount=mysql_num_rows($sqlresult);
 
 

The checkboxes:-

while($vehiclelist=mysql_fetch_array($sqlresult,MYSQL_NUM))

{

echo '<input type="checkbox" name="'.$vehiclelist[0].'" value="'.$vehiclelistt[0].'" />'.$vehiclelist[1].'<br/>';

}

Open in new window

0
Comment
Question by:EzEApostle
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24076806
I'm not quite sure on what you need?! ... do you want the boxes to be checked or do you want to pull out a list with only the selected boxes?
0
 
LVL 1

Author Comment

by:EzEApostle
ID: 24076857
On the user filling out the search form I need the ID's (name) of the checkboxes that were selected :) by default they're all unselected
0
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24076953
Ok, i think there is a smarter way retrieving the content of the $_post, but the follwing should work
$sql='SELECT vehicle_id, display_name FROM vehicles';

$sqlresult=mysql_query($sql);
 

$sqlnew='SELECT vehicle_id, display_name FROM vehicles WHERE ';

$firstruntrough=0;
 

while($vehiclelist=mysql_fetch_array($sqlresult))

{

   if($_POST["yourchecknames"]==1){

      if($firstrunthrough==0){

         $firstrunthrough=1;

         $sqlnew.="vehicle_id = $_POST["yourcheckvalue"] ";

      } else {

         $sqlnew.="OR vehicle_id = $_POST["yourcheckvalue"] ";

      }

   }

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:EzEApostle
ID: 24076996
I dont think that will work because $_POST["yourchecknames"] is set to different ID's i.e. the Vehicle ID
0
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24077025
Ok then this should work
$sql='SELECT vehicle_id, display_name FROM vehicles';

$sqlresult=mysql_query($sql);

 

$sqlnew='SELECT vehicle_id, display_name FROM vehicles WHERE ';

$firstruntrough=0;

 

while($vehiclelist=mysql_fetch_array($sqlresult))

{

   if($_POST["yourchecknames"] != 0){

      if($firstrunthrough==0){

         $firstrunthrough=1;

         $sqlnew.="vehicle_id = $_POST["yourchecknames"] ";

      } else {

         $sqlnew.="OR vehicle_id = $_POST["yourchecknames"] ";

      }

   }

}

Open in new window

0
 
LVL 1

Author Comment

by:EzEApostle
ID: 24077565
Again, I can't see it working because, this value $_POST["yourchecknames"] isn't a single value but many

I refer to my previous statement:-
echo '<input type="checkbox" name="'.$vehiclelist[0].'" value="'.$vehiclelistt[0].'" />'.$vehiclelist[1].'<br/>';
0
 
LVL 1

Accepted Solution

by:
EzEApostle earned 0 total points
ID: 24085006
I've fixed it myself


			$sql='SELECT vehicle_id, display_name FROM vehicles';

			$sqlresult=mysql_query($sql);

			$sqlcount=mysql_num_rows($sqlresult);

			

				while($vehiclelist=mysql_fetch_array($sqlresult,MYSQL_NUM))

				{				

					for ($i=0; $i <= $sqlcount; ++$i) {

					   if(isset($_POST[''.$vechiclelist[$i].''])) {echo $_POST[''.$vehiclelist[$i].''].',';}

					}

				}				

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 64
Mysql sync between 3-4 mysql db 4 25
How do I randomize photo images before i move them to the path file? 9 22
mysql between clause 2 10
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now