Jump to content

Archived

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

muchomacho

Accepting multiple selections

Recommended Posts

Hello,

Does anyone know how to make a SELECT box accept multiple choices and be able to access those values via php?

I have tried using multiple, and indeed it does let a user select more than one value - but only one value is passed to the action script.

When the PHP script processes the html form, only the word "Array" is returned when the form is submitted.

Here's my HTML code:

[color=red]select multiple name="Positions[]"[/color]

Here's what I have so far for the receiving variables in my action script:

[color=red][font=Verdana]@$Positions = $_POST['Positions'];
@$Positions = @implode("," , $Positions);
@$Company = addslashes($_POST['Company']);
@$Positions = addslashes($_POST['Positions']);[/font] [/color]

Any help will be greatly appreciated.

Thanks  :D

Share this post


Link to post
Share on other sites
$Positions is an array containing the selections
[code]<?php
foreach ($Positions as $pos) {
  echo $pos. '<br />';
}
?>[/code]

Share this post


Link to post
Share on other sites
Thanks so much for the reply! The code is now taking the multiple selections and displaing them nicely on the browser, but what I am looking for is (1) Post the array into mySQL table and (2) sending an email with the information.

I think my real problem is not knowing how to incorporate [code]($Positions as $pos)[/code] into my existing code:

[code]
@$Company = addslashes($_POST['Company']);
@$Positions = addslashes($_POST['Positions']);
@$FirstName = addslashes($_POST['FirstName']);
@$MiddleInitial = addslashes($_POST['MiddleInitial']);
[/code]

[code]
$pfw_header = "From: $Email\n"
  . "Reply-To: $Email\n";
$pfw_subject = "Job Application";
$pfw_email_to = "email";
$pfw_message = "Company: $Company\n\n"
. "Positions: $Positions\n\n"
[/code]

Share this post


Link to post
Share on other sites
replace
[code]
"Positions: $Positions\n\n"
[/code]

with
[code]
"Positions: " . join(', ', $Positions) . "\n\n"
[/code]

Share this post


Link to post
Share on other sites
Again! Thank so much. You solved my first problem. I am getting back all selections on an email, however, mySQL  is only showing the word "array" for the Positions column. How can I save all selections into that cell?

Here's my code to save the record into mySQL:

[code]@$pfw_strQuery = "INSERT INTO `X`(`Company`,`Positions`,`FirstName`,`MiddleInitial`,`LastName`,`StreetName`,`City`,`State`,`ZipCode`,`Phone`,`BestTimetoCall`,`Email`,`HighSchool`,`College`,`HowDidYouFindUs`,`ReferralName`,`RaceEthnicOrigin`,`VietnamVeteran`,`Decline`)VALUES (\"$Company\",\"$pos\",\"$FirstName\",\"$MiddleInitial\",\"$LastName\",\"$StreetName\",\"$City\",\"$State\",\"$ZipCode\",\"$Phone\",\"$BestTimetoCall\",\"$Email\",\"$HighSchool\",\"$College\",\"$HowDidYouFindUs\",\"$ReferralName\",\"$RaceEthnicOrigin\",\"$VietnamVeteran\",\"$Decline\")" ;[/code]

Share this post


Link to post
Share on other sites
Same way
[code]
$Pos = join(', ', $_POST['Positions']);

$Pos = addslashes($Pos);[/code]

Then put $Pos into the database table

Share this post


Link to post
Share on other sites
[code]

<?php
// Receiving variables
[foreach ($Positions as $Pos)
@$Company = addslashes($_POST['Company']);
@$Pos = addslashes($_POST['Pos']);
@$FirstName = addslashes($_POST['FirstName']);
@$MiddleInitial = addslashes($_POST['MiddleInitial']);
@$LastName = addslashes($_POST['LastName']);
@$StreetName = addslashes($_POST['StreetName']);
@$City = addslashes($_POST['City']);
@$State = addslashes($_POST['State']);
@$ZipCode = addslashes($_POST['ZipCode']);
@$Phone = addslashes($_POST['Phone']);
@$BestTimetoCall = addslashes($_POST['BestTimetoCall']);
@$Email = addslashes($_POST['Email']);
@$HighSchool = addslashes($_POST['HighSchool']);
@$College = addslashes($_POST['College']);
@$resume_Name = $_FILES['resume']['name'];
@$resume_Size = $_FILES['resume']['size'];
@$resume_Temp = $_FILES['resume']['tmp_name'];
@$resume_Mime_Type = $_FILES['resume']['type'];
@$HowDidYouFindUs = addslashes($_POST['HowDidYouFindUs']);
@$ReferralName = addslashes($_POST['ReferralName']);
@$RaceEthnicOrigin = addslashes($_POST['RaceEthnicOrigin']);
@$VietnamVeteran = addslashes($_POST['VietnamVeteran']);
@$Decline = addslashes($_POST['Decline']);

function RecursiveMkdir($path)
{
  if (!file_exists($path))
  {
      RecursiveMkdir(dirname($path));
      mkdir($path, 0777);
    }
  }


// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $Email))
{
header("Location: error.php");
exit;
}

if( $resume_Size == 0)
{
header("Location: error.php");
exit;
}
if( $resume_Size >2000000)
{
//delete file
unlink($resume_Temp);
header("Location: error.php");
exit;
}
if( $resume_Mime_Type != "application/msword" AND $resume_Mime_Type != "application/pdf" AND $resume_Mime_Type != "application/vnd.ms-excel" )
{
unlink($resume_Temp);
header("Location: error.php");
exit;
}
$uploadFile = "resumes/".$resume_Name;
if (!is_dir(dirname($uploadFile)))
  {
    @RecursiveMkdir(dirname($uploadFile));
  }
else
  {
  @chmod(dirname($uploadFile), 777);
  }
@move_uploaded_file( $resume_Temp , $uploadFile);
@chmod($uploadFile, 644);
$resume_URL = ";

//Sending Email to form owner

$pfw_header = "From: $Email\n"
  . "Reply-To: $Email\n";
$pfw_subject = "Job Application";
$pfw_email_to = "x@x.com";
$pfw_message = "Company: $Company\n\n"
."Positions: " . join(', ', $Positions) . "\n\n"
. "First Name: $FirstName\n\n"
. "Middle Initial: $MiddleInitial\n\n"
. "Last Name: $LastName\n\n"
. "Address: $StreetName\n\n"
. "City: $City\n\n"
. "State: $State\n\n"
. "Zip Code: $ZipCode\n\n"
. "Phone: $Phone\n\n"
. "Best Time to Call: $BestTimetoCall\n\n"
. "Email: $Email\n\n"
. "High School: $HighSchool\n\n"
. "College: $College\n\n"
. "Resume: $resume_URL\n\n"
. "How Did You Find Us: $HowDidYouFindUs\n\n"
. "Referral Name: $ReferralName\n\n"
. "Race Ethnic Origin: $RaceEthnicOrigin\n\n"
. "Vietnam Veteran: $VietnamVeteran\n\n"
. "Decline: $Decline\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//Sending auto respond Email to visitor
$pfw_header = "From: x@x.com"
. "Reply-To: x@x.com";
$pfw_subject = "Thanks for submitting your resume";
$pfw_email_to = "$Email";
$pfw_message = "$FirstName $LastName\n"
. "\n"
. "Thank You! Your application has been received.";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//saving record to MySQL database
@$pfw_strQuery = "INSERT INTO `x`(`Company`,`Positions`,`FirstName`,`MiddleInitial`,`LastName`,`StreetName`,`City`,`State`,`ZipCode`,`Phone`,`BestTimetoCall`,`Email`,`HighSchool`,`College`,`HowDidYouFindUs`,`ReferralName`,`RaceEthnicOrigin`,`VietnamVeteran`,`Decline`)VALUES (\"$Company\",\"$Pos\",\"$FirstName\",\"$MiddleInitial\",\"$LastName\",\"$StreetName\",\"$City\",\"$State\",\"$ZipCode\",\"$Phone\",\"$BestTimetoCall\",\"$Email\",\"$HighSchool\",\"$College\",\"$HowDidYouFindUs\",\"$ReferralName\",\"$RaceEthnicOrigin\",\"$VietnamVeteran\",\"$Decline\")" ;
@$pfw_host = "localhost";
@$pfw_user = "x";
@$pfw_pw = "x";
@$pfw_db = "x";
$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
die('Could not connect: ' . mysql_error());
}
$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
if (!$pfw_db_selected) {
die ('Can not use $pfw_db : ' . mysql_error());
}

//insert new record
$pfw_result = mysql_query($pfw_strQuery);
if (!$pfw_result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($pfw_link);

header("Location: success.php");

?>
[/code]

Share this post


Link to post
Share on other sites
I've edited some lines

[code]
<?php
// Receiving variables
#[foreach ($Positions as $Pos)                              //  *** remove this line
@$Company = addslashes($_POST['Company']);
@$Pos = join(', ', $_POST['Positions']);                    //  *** changed
@$FirstName = addslashes($_POST['FirstName']);
@$MiddleInitial = addslashes($_POST['MiddleInitial']);
@$LastName = addslashes($_POST['LastName']);
@$StreetName = addslashes($_POST['StreetName']);
@$City = addslashes($_POST['City']);
@$State = addslashes($_POST['State']);
@$ZipCode = addslashes($_POST['ZipCode']);
@$Phone = addslashes($_POST['Phone']);
@$BestTimetoCall = addslashes($_POST['BestTimetoCall']);
@$Email = addslashes($_POST['Email']);
@$HighSchool = addslashes($_POST['HighSchool']);
@$College = addslashes($_POST['College']);
@$resume_Name = $_FILES['resume']['name'];
@$resume_Size = $_FILES['resume']['size'];
@$resume_Temp = $_FILES['resume']['tmp_name'];
@$resume_Mime_Type = $_FILES['resume']['type'];
@$HowDidYouFindUs = addslashes($_POST['HowDidYouFindUs']);
@$ReferralName = addslashes($_POST['ReferralName']);
@$RaceEthnicOrigin = addslashes($_POST['RaceEthnicOrigin']);
@$VietnamVeteran = addslashes($_POST['VietnamVeteran']);
@$Decline = addslashes($_POST['Decline']);

function RecursiveMkdir($path)
{
  if (!file_exists($path))
  {
      RecursiveMkdir(dirname($path));
      mkdir($path, 0777);
    }
  }


// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $Email))
{
header("Location: error.php");
exit;
}

if( $resume_Size == 0)
{
header("Location: error.php");
exit;
}
if( $resume_Size >2000000)
{
//delete file
unlink($resume_Temp);
header("Location: error.php");
exit;
}
if( $resume_Mime_Type != "application/msword" AND $resume_Mime_Type != "application/pdf" AND $resume_Mime_Type != "application/vnd.ms-excel" )
{
unlink($resume_Temp);
header("Location: error.php");
exit;
}
$uploadFile = "resumes/".$resume_Name;
if (!is_dir(dirname($uploadFile)))
  {
    @RecursiveMkdir(dirname($uploadFile));
  }
else
  {
  @chmod(dirname($uploadFile), 777);
  }
@move_uploaded_file( $resume_Temp , $uploadFile);
@chmod($uploadFile, 644);
$resume_URL = "";                                        // *** changed

//Sending Email to form owner

$pfw_header = "From: $Emailn"
  . "Reply-To: $Emailn";
$pfw_subject = "Job Application";
$pfw_email_to = "x@x.com";
$pfw_message = "Company: $Companynn"
. "Positions: " . $Pos . "nn"                              // *** changed
. "First Name: $FirstNamenn"
. "Middle Initial: $MiddleInitialnn"
. "Last Name: $LastNamenn"
. "Address: $StreetNamenn"
. "City: $Citynn"
. "State: $Statenn"
. "Zip Code: $ZipCodenn"
. "Phone: $Phonenn"
. "Best Time to Call: $BestTimetoCallnn"
. "Email: $Emailnn"
. "High School: $HighSchoolnn"
. "College: $Collegenn"
. "Resume: $resume_URLnn"
. "How Did You Find Us: $HowDidYouFindUsnn"
. "Referral Name: $ReferralNamenn"
. "Race Ethnic Origin: $RaceEthnicOriginnn"
. "Vietnam Veteran: $VietnamVeterannn"
. "Decline: $Declinen";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//Sending auto respond Email to visitor
$pfw_header = "From: x@x.com"
. "Reply-To: x@x.com";
$pfw_subject = "Thanks for submitting your resume";
$pfw_email_to = "$Email";
$pfw_message = "$FirstName $LastNamen"
. "n"
. "Thank You! Your application has been received.";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//saving record to MySQL database

$Pos = addslashes($Pos);                                            // *** added


@$pfw_strQuery = "INSERT INTO `x`(`Company`,`Positions`,`FirstName`,`MiddleInitial`,`LastName`,`StreetName`,`City`,`State`,`ZipCode`,`Phone`,`BestTimetoCall`,`Email`,`HighSchool`,`College`,`HowDidYouFindUs`,`ReferralName`,`RaceEthnicOrigin`,`VietnamVeteran`,`Decline`)VALUES ("$Company\",\"$Pos\",\"$FirstName\",\"$MiddleInitial\",\"$LastName\",\"$StreetName\",\"$City\",\"$State\",\"$ZipCode\",\"$Phone\",\"$BestTimetoCall\",\"$Email\",\"$HighSchool\",\"$College\",\"$HowDidYouFindUs\",\"$ReferralName\",\"$RaceEthnicOrigin\",\"$VietnamVeteran\",\"$Decline\")" ;
@$pfw_host = "localhost";
@$pfw_user = "x";
@$pfw_pw = "x";
@$pfw_db = "x";
$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
die('Could not connect: ' . mysql_error());
}
$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
if (!$pfw_db_selected) {
die ('Can not use $pfw_db : ' . mysql_error());
}

//insert new record
$pfw_result = mysql_query($pfw_strQuery);
if (!$pfw_result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($pfw_link);

header("Location: success.php");

?>
[/code]

Share this post


Link to post
Share on other sites

×

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.