PHP Forums Archive

A (safe) contact form using Xajax and PHPmailer

Tags: ajax, phpmailer, contact form

parscon posted on 2008-01-24 21:19:46 #

Hello

Thank you for your Best site .

I have a request , I need your A (safe) contact form using Xajax and PHPmailer sample.

Can you give me like your Demo (Can download Sample like your Demo). I need it really .

Hope help me .

Thank you very much .

Mahmoud Sheikhian

Comments / discussions

Olaf posted on 2008-01-24 21:25:37 #

Hi,

I send you already the information by mail.

don't accept that I will offer custom examples for free. Donate (enough) and I will think about to post something here ;)

the8track posted on 2008-05-08 21:31:07 #

Hello, I am an amature web designer with little knowledge of PHP. I know HTML and Flash but PHP is still relatively gibberish to me. Is there any way I can download the demo from you?

Olaf posted on 2008-05-09 19:17:17 #

Quote from: the8track
"Hello, I am an amature web designer with little knowledge of PHP. I know HTML and Flash but PHP is still relatively gibberish to me. Is there any way I can download the demo from you?"

check my previous answer, the tutorial has all code you need.

the8track posted on 2008-05-13 15:28:09 #

My code should look like this...correct?

<?php
require_once('phpmailer/class.phpmailer.php');
require_once('xajax/xajax.inc.php');?>
$form = '
<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="border-top:1px solid #CCCCCC;padding-top:5px;">
              <label for="subbtn" style="text-align:right;"> --> </label>
              <input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues('cform'));" />
            </div>
          </form>';
          ?>
          <?php
          function myFunction($get) {
    global $form, $error;
    $error = '';
    $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...";
    }
    $data = (!$show_form) ? '<p class="contactMsg">'.$error.'</p>' : '<p class="contactMsg">'.$error.'</p>'.$form;
    $objResponse->addAssign('contact_result', 'innerHTML', $data);
    return $objResponse;
}
?>
<html>
<head>
<?php $xajax->printJavascript('xajax/'); ?>
</head>
<body>
<?php
      $xajax = new xajax();
      $xajax->registerFunction('myFunction');
      $xajax->processRequests();
      echo '<div id="contact_result">'.$form.'</div>';
      ?>
</body>
</html>

the8track posted on 2008-05-13 15:28:48 #

My code should look like this...correct?

<?php
require_once('phpmailer/class.phpmailer.php');
require_once('xajax/xajax.inc.php');?>
$form = '
<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="border-top:1px solid #CCCCCC;padding-top:5px;">
              <label for="subbtn" style="text-align:right;"> --> </label>
              <input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues('cform'));" />
            </div>
          </form>';
          ?>
          <?php
          function myFunction($get) {
    global $form, $error;
    $error = '';
    $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...";
    }
    $data = (!$show_form) ? '<p class="contactMsg">'.$error.'</p>' : '<p class="contactMsg">'.$error.'</p>'.$form;
    $objResponse->addAssign('contact_result', 'innerHTML', $data);
    return $objResponse;
}
?>
<html>
<head>
<?php $xajax->printJavascript('xajax/'); ?>
</head>
<body>
<?php
      $xajax = new xajax();
      $xajax->registerFunction('myFunction');
      $xajax->processRequests();
      echo '<div id="contact_result">'.$form.'</div>';
      ?>
</body>
</html>