Tag Archives: php

PHP Tips: How to send an email through a contact form to multiple recipients

Here is the simple line of code you can use to send a PHP email contact form to multiple email addresses:

  $to = $this->fields['EmailAddress'].',';
	  $to .='info@yourwebsite.com';

This comes in handy when you have to send the email to one specific email address in addition to an email address which is being pulled in via a variable – for example, if you want to send a copy of the email to users themselves, as in the example above.

PHP Tips: How to convert underscores to spaces in emailed contact forms

I was recently building a PHP contact form and ran into the issue where the field names I was using in my HTML form which we were more than one word long (i.e.: “First Name”) were being emailed with underscores in place of the spaces (i.e.: “First_Name”). The PHP file I was processing my form with was replacing the spaces with underscores. Which was necessary for processing the form, BUT – I needed to convert those underscores back to spaces when the contact form data was emailed for usability purposes.

Here is the simple line of code that I added to my PHP file:

$msg = str_replace("_"," ", $msg);

Where $msg was part of my mail function:

 mail($to, $subject, $msg, $headers)

And had already been defined:

  $msg = "User message: \n\n";
          foreach($this->fields as $key => $field)
                $msg .= "$key:  $field \n";

I added the first line of code (which used the str_replace function) right after the code which defined $msg.

You will want to replace the variable $msg in the code above with the name of the variable you are using for the body of the email in your mail function.


					

WordPress Tricks: Add a div or other element to one specific page or page type

 

Here is the code I use to add a div or image or other element to just one page or category page of my WordPress sites:

This code will add a div with the id “featured_content” only on the home page:

<?php if ( is_home() ) { ?>
<div id="featured_content">
&nbsp;
</div>
<?php } ?>

 

This code would add a div with the id “featured_recipe” to all posts in the category “recipes.”:

<?php if ( is_category('recipes') ) { ?>
<div id="featured_recipe">
&nbsp;
</div>
<?php } ?>

 

This code would exclude “myimage.jpg” only on the Contact page:

<?php if ( !is_page('contact') ) { ?>
<img src="myimage.jpg">
<?php } ?>

 

As you can see, there are many uses and ways implementations of this code. Let me know if you have any questions or need further assistance.

Concrete5: How to display a page’s unique ID

Here is the line of code I use to display a page’s unique ID.

<?php echo $c->getCollectionID() ?>

This is really useful for styling individual pages.

A simple PHP script to find your web root directory

Here is a simple line of code you can use to find the web root and path of your website.

Copy and paste this code into a text file:

<?php
print ($_SERVER['DOCUMENT_ROOT']);
?>

Save the file as findwebroot.php or whatever name you want, as long as it has a php extension.

Upload the file to your server, navigate to the file in a browser, and voilà - you will see the full path to that file!

Developing a custom store locator map for your website using Google maps, Javascript, XML, and PHP

Here is a comprehensive series of posts which takes you through the entire process of developing a custom store locator map for your website using Google maps, Javascript, XML, and PHP.

Here are the basic steps to developing your store locator map:

Create a spreadsheet of your store locations

Create and populate the MySQL table

 Create a PHP file which will be used to connect to the database

Create a PHP file which will output the XML file results of a search

Create the HTML page which contains the store locator map

 

 And here are some hints and tips to help create and customize your store locator map:

Using your own custom markers for the locations in place of Google’s default markers

How to prevent the map from zooming in to close on a single location