PDA

View Full Version : Novice needs help please


Pieter Nel
07-21-2014, 03:07 PM
Hi-I am new to php. I need to add check boxes to my php file to send options. I have added it to the html but idon't have a clue what to add to the php in order to recieve the correct info via email.
Here is the HTML:

<form action="sendmail.php" method="post" class="form-contact" id="form-contact">

<p>
<label for="name"><span>Name</span></label>
<input type="text" name="name" id="name" class="col01" />

<label for="email"><span>Email</span></label>
<input type="text" name="email" id="email" class="col01" />

<label for="phone"><span>Phone Number</span></label>
<input type="text" name="phone" id="phone" class="col01"/>

<label for="event" accesskey="S">Type of Event</label>
<select name="event" id="event">
<option value="pselect">- Select -</option>
<option value="Wedding">Wedding</option>
<option value="Birthday ">Birthday</option>
<option value="Corporate">Corporate</option>
<option value="Other">Other</option>
</select>


<label for="date"><span>When is your Event?</span></label>
<input type="text" name="date" id="date" class="col01"/>

<label for="venue"><span>Where is your Event?</span></label>
<input type="text" name="venue" id="venue" class="col01"/>

<label for="guests"><span>For how many Guests?</span></label>
<input type="text" name="guests" id="guests" class="col01"/>

<label for="package" accesskey="S">Please choose your Package</label>
<select name="package" id="package">
<option value="pselect">- Select -</option>
<option value="2 hours R3450">2 hours R3450</option>
<option value="3 hours R3950">3 hours R3950</option>
<option value="4 hours R4500">4 hours R4500</option>
<option value="More than 4 hours">More than 4 hours</option>
</select>
</p>
<label for="extra"><span>Will you be needing any additional services?</span></label><br />
<input name="additional services[]" type="checkbox" class="form-contact" id="CheckboxGroup1_0" value="Fridge Magnet" />
Fridge Magnet

<input name="additional services[]" type="checkbox" class="form-contact" id="CheckboxGroup1_1" value="Guestbook" />
Guest Book
<label for="message"><span><br />
Anything else you would like us to know?</span></label>
<textarea cols="30" rows="8" name="message" id="message" class="col01"></textarea>

<input type="hidden" name="action" value="send">

</p>
<p class="cf-sb">
<input type="submit" name="sendbutton" id="sendbutton" class="sendbutton" value="Send" />
</p>
</form>and here is the PHP
<?php

/**
* Define the email witch send the email written by the user
*/
define('TO', 'me@gmail.com');

/**
* Define the from email
*/
define('FROM', 'noreply@email.com');

/**
* define the body of the email. You can add some shortcode, with this format: %ID%
*
* ID = the id have you insert on the html markup.
*
* e.g.
* <input type="text" name="email" />
*
* You can add on BODY, this:
* email: %email%
*/
define('BODY', 'Hi you have a Photobooth query from:<br /><strong> %name% </strong><br />
email: %email% <br />
Phone no: <strong>%phone% </strong><br />
Event Type: <strong>%event% </strong>on <strong>%date%</strong> for <strong>%guests%</strong> guests at <strong>%venue%</strong> <br />
Package Selected: <strong>%package%</strong> <br />
Extras Required: <strong>%CheckboxGroup1_0%</strong> <br />
<br />
<strong>Message: </strong><br /> %message%');

// NO NEED EDIT
function sendemail()
{
if ( isset( $_POST['action'] ) AND $_POST['action'] == 'send' )
{
$body = BODY;

if( !is_string( $_POST['name'] ) OR $_POST['name'] == '' )
return '<p class="error">Insert correct name</p>';

if( !is_email( $_POST['email'] ) OR $_POST['email'] == '' )
return '<p class="error">Insert correct email</p>';

$subject = $_POST['subject'];

if( !is_email( $subject ) OR $subject == '' )
$subject = 'Website Response.';

foreach( array_map( 'stripslashes', $_POST ) as $id => $var )
{
if( $id == 'message' ) $var = nl2br($var);
$body = str_replace( "%$id%", $var, $body );
}


$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: noreply@booth.co.za\r\n";

$sendmail = mail(TO, $subject, $body, $headers);

if ($sendmail)
return '<p class="success-box">Thank you, We will respond to you shortly.</p>';
}
}

function is_email($email)
{
if (!preg_match("/[a-z0-9][_.a-z0-9-]+@([a-z0-9][0-9a-z-]+.)+([a-z]{2,4})/" , $email))
{
return false;
}
else
{
return true;
}
}

echo sendemail();

?>When the contact form is submitted I get this error:

Warning: stripslashes() expects parameter 1 to be string, array given in /home/photo/public_html/sendmail.php on line 49

I do receive the contact form with all the info required except for the check-box information.

any help will be appreciated

Ricky55
07-21-2014, 09:32 PM
Looking at the script don't you just add %your-id% and then match this to the id your element.

So for a check box you'd add


<form>
<input type="checkbox" name="vehicle" value="Bike" id="bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car" id="car">I have a car
</form>



I have a bike: <strong>%bike%</strong>
I have a car: <strong>%car%</strong>

Ricky55
07-21-2014, 09:41 PM
does work like that. Working fine on my server.

http://www.qwerty-demos.co.uk/test.html

Pieter Nel
07-22-2014, 08:07 AM
Thanks Ricky - I have changed the html as follows:
<input type="checkbox" name="extra" value="Fridge Magnet" id="magnet">Fridge Magnet<br>
<input type="checkbox" name="extra" value="Guest Book" id="book">Guest Book

and the PHP to include the id:
define('BODY', 'Hi you have a Photobooth query from:<br /><strong> %name% </strong><br />
email: %email% <br />
Phone no: <strong>%phone% </strong><br />
Event Type: <strong>%event% </strong>on <strong>%date%</strong> for <strong>%guests%</strong> guests at <strong>%venue%</strong> <br />
Package Selected: <strong>%package%</strong> <br />
Extras Required: <strong>%magnet% </strong><strong>%book% </strong> <br />
<strong>Message: </strong><br /> %message%');

and the email I receive looks like this:

Event Type: Corporate on 15 December for 22 guests at Durban
Package Selected: More than 4 hours
Extras Required: %magnet%%book%
Message:
nil

so all the other fields are populated using the id tag but not the info relative to the tick boxes

any suggestions?

Pieter Nel
07-22-2014, 08:18 AM
Thanks Ricky

I have made your suggestion <input type="checkbox" name="extra" value="Fridge Magnet" id="magnet">Fridge Magnet<br>
<input type="checkbox" name="extra" value="Guest Book" id="book">Guest Book and the php
Extras Required: <strong>%magnet% </strong><strong>%book% </strong> <br />
but the mail i receive:

Phone no: 79992252
Event Type: Corporate on 15 December for 22 guests at Durban
Package Selected: More than 4 hours
Extras Required: %magnet%%book%
does not include the tickbox actual value only the =id, any ideas?

edbr
07-23-2014, 02:29 AM
try in your form

<input type="checkbox" name="extra[]" value="Fridge Magnet" id="magnet">Fridge Magnet<br>
<input type="checkbox" name="extra[]" value="Guest Book" id="book">Guest Book

Ricky55
07-23-2014, 10:42 PM
Have you got this sorted?

Seemed to work fine on my server.

Might have something to do with the version of PHP you are running. Not sure TBH.

I have another script if you can't get this one working.

Mail me if you want it richard@qwerty-design.co.uk

Works fine with all types of forms.