PHP Forums Archive

PHP/xajax contact form upgrade

Tags: tutorial, contact form, xajax

Olaf posted on 2008-09-13 06:16:20 #

Hi,

Some user told me about a code bug in the contact form tutorial. In Internet explorer the form fields become empty if the form was submitted and not send because of some missing fields. The example below is changed to fix that. The following code is also updated to use the NEW xajax version (so the old version I used the tutorial will not work).

<?php
require_once('xajax/xajax_core/xajax.inc.php');
require_once('PHPMailer/class.phpmailer.php');

function myFunction($get) {
	$error = '';
	$data = '';
    $objResponse = new xajaxResponse();
	$show_form = true;
	if (!empty($get['email']) && !empty($get['msg']) && !empty($get['name'])) {
	    if (preg_match("/^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i", trim($get['email']))) {
			$email = preg_replace("/\r\n/", "", $get['email']);
			$from = preg_replace("/\r\n/", "", $get['name']);
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Host = "smtp.yourserver.com";
            $mail->SMTPAuth = true;
            $mail->Username = "postmaster@yourserver.com";
            $mail->Password = "password";
            $mail->From = "postmaster@yourserver.com";
            $mail->FromName = "Webmaster";
            $mail->AddAddress("admin@yourserver.com");
            $mail->AddReplyTo($email, $from);
            $mail->Subject = "contact form using Xajax and phpmailer";
            $mail->Body = $get['msg'];
            if ($mail->Send()) {
                $error = "The form is submitted and the mail is send.";
                $show_form = false;
            } else {
                $error = "There was a problem while sending the mail, please try again";
            }
		} else {
			$error = "The entered e-mail address is not valid.";
		}
	} else {
		$error = "At least one of the fields is empty...";
	}
    if (!$show_form) $objResponse->assign('contact_result', 'innerHTML', '');
    $objResponse->assign('contactMsg', 'innerHTML', $error);
    return $objResponse;
}
$xajax = new xajax();
//$xajax->configure('debug',true);
$xajax->register(XAJAX_FUNCTION, 'myFunction');
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Ajax example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php $xajax->printJavascript('/xajax/'); ?>
<style type="text/css" media="screen">
<!--
label {
	width: 130px;
	padding-right:10px;
	display:block;
	float:left;
}
-->
</style>
</head>
<body>
  <h2>AJAX Mail DEMO</h2>
  <div id="contact_result">
    <form id="cform">
        <div>
          <label for="name">Name</label>
          <input name="name" type="text" id="naam" value="" size="25" />
        </div>
        <div>
          <label for="email">E-mail</label>
          <input name="email" type="text" id="email" value="" size="25" />
        </div>
        <div>
          <label for="msg">Message</label>
          <textarea name="msg" id="msg" cols="45" rows="5"></textarea>
        </div>
        <div style="padding-top:5px;">
          <label for="subbtn"></label>
          <input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues('cform'));" />
        </div>
      </form>
      <p id="contactMsg"></p>

  </div>
</body>
</html>

Comments / discussions

wRk posted on 2008-09-18 18:41:41 #

Tnx Olaf, it's working fine now!!

Just a little Q:
when I give the id, contactMsg, for the error message(<p id="contactMsg"> )some properties like a border and background color. I would like to hide them when no error's occuring..Do you have any idea how to realize this.

Olaf posted on 2008-09-18 18:50:33 #

Hi,

Just create a second container around the message element, use a different ID. Then style the inner element and use the new (outer) ID to show / hide the content.

wRk posted on 2008-09-18 19:11:29 #

Hi,

YEah i was also thinking in that direction. Im doing now some like(without the outer div):

echo ($error != '')? '<p id="contactMsg">': '' ;

but that is not gonna work..

Olaf posted on 2008-09-18 20:12:42 #

no it's more like:

the php code

if ($error != '') $objResponse->assign('contactMsg', 'innerHTML', '<p class="error">'.$error.'
');

the html

</p><div id="contactMsg"></div>

and use this css code for the style:

.error {
  background-color:#ccc;
}