Jump to content

Multi-Select Listbox and PHP


ss345

Recommended Posts

Hi guys, I wonder if any of you can help me. I am webmaster for a company that develops sensors. I have been asked to create a quotation request form for the new item, in which I have achieved successfully. (I copied and pasted from a form used previously within the website). When a user submits a form, the data they input is emailed to the company.

 

However, when multiple values are selected from within the listboxes, only one value is output to the email instead of all of the values the user has selected. Here is my coding of the form on the output page, could some take a look and see why the listboxes are only outputting one value and perhaps modify the script so that it does?

 

The name of the first listbox is: dataoutput

The name of the second listbox is: actionrq

 

<?php

if (!$_POST['email']) { exit("<b><center>You did not supply an E-Mail address. We need this to reply to your E-Mails.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['thename']) { exit("<b><center>You did not supply a name. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['tel']) { exit("<b><center>You did not supply a contact number. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['fax']) { $fax='<i>Not Supplied</i>'; }

if (!$_POST['companyname']) { exit("<b><center>You did not supply a company name. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['address']) { exit("<b><center>You did not supply an address. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['postcode']) { exit("<b><center>You did not supply a postal code. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

if (!$_POST['weburl']) { $weburl='<i>Not Supplied</i>'; }

if (!$_POST['country']) { exit("<b><center>You did not supply a Country. We need this to contact our distributor in order to calculate shipping costs. Please go back and try again.<br /><br /><a href='JavaScript:history.go(-1)'> &lchev; Go Back</a></center></b>"); }

else { $name=$_POST['thename']; }

$email=$_POST['email'];

$tel=$_POST['tel'];

$fax=$_POST['fax'];

$companyname=$_POST['companyname'];

$address=$_POST['address'];

$postcode=$_POST['postcode'];

$weburl=$_POST['weburl'];

$country=$_POST['country'];

$load=$_POST['load'];

$ouvolt=$_POST['ouvolt'];

$oucurr=$_POST['oucurr'];

$dataoutput=$_POST['dataoutput'];

$dataother=$_POST['dataother'];

$environment=$_POST['environment'];

$envirother=$_POST['envirother'];

$temprangefrom=$_POST['temprangefrom'];

$temprangeto=$_POST['temprangeto'];

$actionrq=$_POST['actionrq'];

$requirement=$_POST['requirement'];

$source=$_POST['source'];

$subj=$_POST['subj'];

if ($subj=='') { $subj="Not Supplied"; }

if (!$_POST['message']) { $mess='<i>Not Supplied</i>'; }

else { $mess=$_POST['message']; }

$mess="<b><u>Name:</u></b> ".$name."<br /><b><u>Telephone Number:</u></b> ".$tel."<br /><b><u>Fax Number:</u></b> ".$fax."<br /><b><u>Company Name:</u></b> ".$companyname."<br /><b><u>Address:</u></b> ".$address."<br /><b><u>Postcode:</u></b> ".$postcode."<br /><b><u>Country:</u></b> ".$country."<br /><br /><b><u>Website:</u></b> ".$weburl."<br /><br /><b><u>DETAILS:</u></b><br /><b><u>Load or Force:</u></b> ".$load."<br /><b><u>Analogue Output Voltage:</u></b> ".$ouvolt."<br /><b><u>Analogue Output Current:</u></b> ".$oucurr." <br /><b><u>Data Output/Display Required:</u></b>".$dataoutput."<br /><b><u>Other (If specified by user):</u></b> ".$dataother." <br /><b><u>Environment:</u></b> ".$environment." <br /><b><u>Other (If specified by user):</u></b> ".$emvirother." <br /><b><u>Temperature Range:</u></b> From ".$temprangefrom."°C - ".$temprangeto."°C<br /><b><u>Requested Action(s):</u></b> ".$actionrq." <br /><b><u>Urgency of Requirement:</u></b> ".$requirement."<br /><b><u>How did they find our website?</u></b> ".$source." <br /><br /><b><u>Description of application and other details:</u></b> ".$mess;

mail("EMAIL REMOVED FOR SECURITY REASONS",$subj,$mess,"Content-type:text/html\r\nFrom:".$email);

echo "<b><center>Your message has been successfully sent. <a href=../../index.html title=Return to our homepage>Return To The Homepage</a></b></center>";

?> 

</p>

 

The HTML for dataoutput is:

<select name="dataoutput" id="Select1" size="4" multiple="multiple">
<option value="Digital Panel Meter Display">Digital Panel Meter Display</option>
<option value="RS232 Output">RS232</option>
<option value="USB Output">USB</option>
<option value="CAN Bus Output">CAN Bus</option>
<option value="Software">Software</option>
</select>

 

The HTML code for actionrq is:

<select name="actionrq" id="Select2" size="4" multiple="multiple">
<option value="Quotation">Quotation Request</option>
<option value="Email More Information">Information Request</option>
<option value="Demonstration">Demonstration Visitation</option>
<option value="Telephone Call-Back">Telephone Call-Back</option>
</select>

 

Thanks folks. :)

Link to comment
Share on other sites

$dataoutput = $_POST['dataoutput'];
$actionrq = $_POST['actionrq'];

 

In a multi-select list, I believe the options a user selects is returned as an array -- not a string. If you want to get all the values in one string, do something like this:

 

$dataoutput = implode(', ', $_POST['dataoutput']);
$actionrq = implode(', ', $_POST['actionrq']);

Link to comment
Share on other sites

Hi there, I've just tried replacing the two red highlighted lines with your code suggestion but get the following error message:

 

Warning: implode(): Bad arguments. in /home/site/public_html/contacts/pages/contactus_email_load.php on line 95 Warning: implode(): Bad arguments. in /home/site/public_html/contacts/pages/contactus_email_load.php on line 101
Link to comment
Share on other sites

I added it below, like this, is this correct?

 

$dataoutput = implode(', ', $_POST['dataoutput']);

var_dump($_POST['dataoutput']);

 

This is what happened when I added the dump variable...

 

Warning: implode(): Bad arguments. in /home/site/public_html/contacts/pages/contactus_email_load.php on line 95 string(8) "Software" Warning: implode(): Bad arguments. in /home/site/public_html/contacts/pages/contactus_email_load.php on line 102
Link to comment
Share on other sites

Okay. I think it will just return a string if one value is selected; if multiple values are selected, it will return an array. Try this:

 

if (is_array($_POST['dataoutput'])) {
  $dataoutput = implode(', ', $_POST['dataoutput']);
} else {
  $dataoutput = $_POST['dataoutput'];
}
// Then, do the same thing with $actionrq (copy this, and replace $dataoutput with $actionrq)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.