Jump to content


Photo

Accepting multiple selections


  • Please log in to reply
10 replies to this topic

#1 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 04:35 PM

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:

select multiple name="Positions[]"

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

@$Positions = $_POST['Positions'];
@$Positions = @implode("," , $Positions);
@$Company = addslashes($_POST['Company']);
@$Positions = addslashes($_POST['Positions']);


Any help will be greatly appreciated.

Thanks  :D

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 07:17 PM

$Positions is an array containing the selections
<?php
foreach ($Positions as $pos) {
   echo $pos. '<br />';
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 08:32 PM

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
($Positions as $pos)
into my existing code:

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

$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"


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 08:36 PM

replace
"Positions: $Positions\n\n"

with
"Positions: " . join(', ', $Positions) . "\n\n"

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 09:44 PM

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:

@$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\")" ;


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 10:00 PM

Same way
$Pos = join(', ', $_POST['Positions']);

$Pos = addslashes($Pos);

Then put $Pos into the database table
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 10:11 PM

I've already tried that and is not working. I hate to be such a pest. Any other ideas?

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 10:15 PM

Should work.
Post the code you've got now
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 10:26 PM


<?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");

?>


#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 10:41 PM

I've edited some lines

<?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");

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 muchomacho

muchomacho
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 October 2006 - 11:00 PM

MUCHAS, MUCHAS GRACIAS!!!!!  :)

Have a wonderful weekend.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users