Jump to content

Need help printing array


alexandervj

Recommended Posts

Here is my code...

<?php

    $to = "email@email.com"; //obviously not real
    $from = "noreply@domain.com";  //obviously not real
    $firstName = $_REQUEST['firstName'];
    $headers = "From: $from";
    $subject = "Product Sheet Request";

    /*
    $fields = array();
    $fields{"firstName"} = "firstName";
    $fields{"lastName"} = "lastName";
    $fields{"email"} = "email";
    $fields{"phone"} = "phone";
    $fields{"affiliation"} = "affiliation";
    $fields{"interest"} = "interest";
    $fields{"productSheets"} = "productSheets";
    $fields{"referredBy"} = "referredBy";
    $fields{"additional"} = "additional";
    */
    
    $firstName = $_REQUEST['firstName'];
    $lastName = $_REQUEST['lastName'];
    $email = $_REQUEST['email'];
    $phone = $_REQUEST['phone'];
    $affiliation = $_REQUEST['affiliation'];
    $interest = $_REQUEST['interest'];
    if($interest == 'inclusion'){
        $interest = 'Evaluating for inclusion in product';
    } elseif($interest == 'general'){
        $interest = 'Interested in the technology generally';
    } elseif($interest == 'research'){
        $interest = 'Performing market research';
    } elseif($interest == 'solutions'){
        $interest = 'Looking for competitive solutions';
    }
    $referredBy = $_REQUEST['referredBy'];
    $additional = $_REQUEST['additional'];
    $productSheets[] = $_REQUEST['productSheets'];
    $sheets = print_r($productSheets);
    

    /*
    $body = "A user has requested product sheet(s). Please review their information and respond:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
    */
    
    $body = "A user has requested product sheet(s). Please review their information and respond:\n
    
    First Name: ".$firstName."\r
    Last Name: ".$lastName."\r
    Email: ".$email."\r
    Phone Number: ".$phone."\r
    Affiliation: ".$affiliation."\r
    Reason for Interest: ".$interest."\r
    Referred By: ".$referredBy."\r
    Additional Questions/Comments: ".$additional."\r
    Product Sheet(s) Requested: ".$sheets."\n
    ";
    
    
    $send = mail($to, $subject, $body, $headers);

?>

I'm not quite sure how to display the values of the $productSheets array in the email message. Everytime it either outputs "Array" or "1". The $productSheets array is from a checkbox on a form. Everything else is working as expected.

Link to comment
Share on other sites

Yeah I just want to know how to display the values in the email instead of "Array" or "1". I tried print_r as seen above but that produced the output "1" instead of the names of the product sheets. Here is the code for my form if that hepls as well...

<form id="contact" name="contact" method="post" novalidate="novalidate">
    <h3>Request Product Sheets</h3>
    <hr style="margin-top:0px; margin-bottom:0px;"/>
    <p>Please fill out the form below and product sheets will be sent to the email provided.<br/>
    * indicates a required field.</p>
    
    <fieldset>
    <table>
	<tr>
	    <td>
		<input type="text" name="firstName" id="firstName" placeholder="First Name *" value="" required="">
	    </td>
	</tr>
	<tr>
	    <td>
		<input type="text" name="lastName" id="lastName" placeholder="Last Name *" value="" required="">
	    </td>
	</tr>
	<tr>
	    <td>
		<input type="text" name="email" id="email" placeholder="Email *" value="" required="">
	    </td>
	</tr>
	<tr>
	    <td>
		<input type="text" name="phone" id="phone" placeholder="Phone Number *" value="">
	    </td>
	</tr>
	<tr>
	    <td>
		<input type="text" name="affiliation" id="affiliation" placeholder="Company/Affiliation" value="">
	    </td>
	</tr>
	<tr>
	    <td>
		<label for="interest" id="interest">To assist us in understanding your interest in our products are you?</label>
		<input type="radio" name="interest" id="interest" value="inclusion" checked> Evaluating for inclusion in product<br>
		<input type="radio" name="interest" id="interest" value="general"> Interested in the technology generally<br>
		<input type="radio" name="interest" id="interest" value="research"> Performing market research<br>
		<input type="radio" name="interest" id="interest" value="solutions"> Looking for competitive solutions
	    </td>
	</tr>
	<tr>
	    <td>
		<label style="margin-top:10px;" for="checkbox" id="productSheets">Which products are you interested in?</label>
		<input type="checkbox" name="productSheets[]" value="lidar"> LiDAR - IR Laser Array Field<br>
		<input type="checkbox" name="productSheets[]" value="nui"> NUI-IR Laser Array Chip<br>
		<input type="checkbox" name="productSheets[]" value="optical"> Optical Solutions
	    </td>
	</tr>
	<tr>
	    <td>
		<input style="margin-top:10px;" type="text" name="referredBy" id="referredBy" placeholder="How did you hear about us?" value="">
	    </td>
	</tr>
	<tr>
	    <td>
		<textarea name="additional" id="additional" placeholder="Additional questions, comments, or requests:"></textarea>
	    </td>
	</tr>
	<tr>
	    <td>
		<input id="submit" type="submit" name="submit" class="btn btn-primary" value="Send">
	    </td>
	</tr>
    </table>
    </fieldset>
</form>
<div id="success">
<span style="text-align: center;">
        <p>Your request was sent successfully. We will email you shortly with the information you requested.</p>
</span>
</div>
<div id="error">
    <span>
	<p>Something went wrong, try refreshing and submitting the form again.</p>
    </span>
</div>
Link to comment
Share on other sites

Since you're using the array syntax in the HTML form (ex: name="productSheets[]"), PHP automatically creates an array for you. So there is no need for the square brackets when assigning the information to $productSheets. Try changing this:

$productSheets[] = $_REQUEST['productSheets'];
 
To this:
$productSheets = $_REQUEST['productSheets'];

 

 

Now $productSheets can be treated as a regular array. If you want to display the array as a comma-separated list, you could do something like this:

print implode(', ', $productSheets);
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.