Tag Archives: form

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.


					

Website forms: Use Javascript to auto fill one field with the values from two other fields

Here is the simple Javascript code that I use to auto fill one field of a form with the values of one or more other fields.

Copy and paste this line to the <head> of your web page:

 

<script type="text/javascript" charset="utf-8">    
function updateUsername(){        
first = document.getElementById("first").value;        
last = document.getElementById("last").value;         document.getElementById("username").value = first+" "+last;    
} </script>

 

Next, you simply need to add the correct ID to the three fields in your form. Make sure that the IDs correspond correctly to the Javascript code we just pasted into the header.

For example:

<input type="text" name="some_name" value="" id="first" onkeyup="updateUsername();">
<input type="text" name="some_name" value="" id="last" onkeyup="updateUsername();">
<input type="text" name="some_name" value="" id="username">

As you can see, you also need to add this line of code to the field or fields whose value(s) you want to fill the third field:

onkeyup="updateUsername();"

And that is all that you need to do. Let me know if you have any questions or run into any issues.

Combine the values of two fields in a form into one variable to be inserted into MySQL database table

Here is the simple code that I use to combine the values of two fields in a form into one variable and insert that variable into a database table.

One example of how I used this trick was when I built an SMS-subscriber widget for a website. I needed people to enter their phone numbers into one field, select their phone carrier from another field, and then combine those two fields with an “@” symbol in between to create an email address that was then inserted into the database. These SMS subscribers could then receive the same notice as email subscribers.

Here is the code you use:

<script>
$('#insert').bind('submit', function(){
                        var phone = $('[name=phone]').val();
                        var email = $('[name=email]').val();
                        $('[name=subscriber]').val(phone+'@'+email);
                      });
</script>

Note: #insert in the code above needs to be changed to the id of your form. 

Insert the jQuery code above directly after the beginning of your form, which would look something like this:

<form action="insert.php" method="POST" id="insert">

Next, create a hidden field which will contain the combined values of the two fields:

<input type="hidden" name="subscriber"/>

And finally, create the two fields whose values you want to combine:

Phone #: <input type="text" size=25 name="phone">
Carrier: <select>
  <option name="email" value="vtext.com">Verizon</option>
  <option name="email" value="txt.att.net">AT&T</option>
</select>

In the php file that you use to process the form, you will only need to post the data from the hidden field (which contains the values of the two fields). For instance, using the example above, I would only insert the ‘subscriber’ data into the database:

$v_subscriber=$_POST['subscriber'];
... 
$query="insert into table_name(email) values('$v_subscriber')";

This would insert the value of the subscriber field (which is “phone@email” according to the jQuery above. For example: 1234567890@att.txt.net) into the email column of the table you specified.

Please let me know if you have questions or run into any issues at all.