8, September 2008

Safe contact form using xajax & PHP - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Safe contact form using xajax & PHP
« previous next »
Pages: [1] 2 Print

Author Topic: Safe contact form using xajax & PHP  (Read 2727 times)
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« on: Jun 01, 2007, 10:07:10 AM »

Hey people

I'm totally new to ajax/xajax, and not all that hot on php either... sorry 'bout that  Embarrassed

I've had a long hard look at the tutorial to make the safe contact form (http://www.finalwebsites.com/tutorials/php_ajax_contact_form.php Visit through proxy), but I can't work out where I need put all the different bits of code. The tute suggests to put the form and function 'before all the html code' - but if I put it in my page, anyone viewing the source will be able to see the passwords for the mail server.

Could someone please give me a clue here?

Many thanks
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jun 01, 2007, 10:44:28 AM »

Hi,

are your sure that php is working well?

only output is parsed by the browser, can you post some code?


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« Reply #2 on: Jun 01, 2007, 11:06:28 AM »

Shortly after posting I realised I had to save my file as contact.php, not contact.html (duh), which has solved the issue of people viewing the source.
However, there seems to be an issue with the following code:


$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;"> --&gt; </label>
              <input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues('cform'));" />
            </div>
          </form>';


When I run the page through a browser, it doesn't like the following line:


<input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues('cform'));" />


At this point the browser tells me it happened across an unexpected T_STRING. I changed ('cform') to ("cform") which fixed that error, but now the form doesn't do anything...
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jun 01, 2007, 11:17:47 AM »

you need to escape the single quotes (not replacing them with double quotes)


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« Reply #4 on: Jun 01, 2007, 01:28:53 PM »

Thanks very much, that's fixed it.. until it hits the next bug that is!

Now I get a popup message telling me that the XML Response received from the server is invalid - and it goes on to display the entire contents of my <html></html> tags.
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jun 01, 2007, 02:08:38 PM »

Thanks very much, that's fixed it.. until it hits the next bug that is!

Now I get a popup message telling me that the XML Response received from the server is invalid - and it goes on to display the entire contents of my <html></html> tags.

check the "php error" in this box and try to fix that


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« Reply #6 on: Jun 01, 2007, 03:41:21 PM »

Problem is that I can't see the entire message, because it's too long - it goes past the bottom of my screen and I can't move it up enough to see the end!

The email gets sent, but the form doesn't disappear.

I'm trying to fiddle with it a bit to see if I can hooky it back together, I think it's something to do with the following code:

$objResponse->addAssign('contact_result', 'innerHTML', $data);
return $objResponse;

Do I need to define 'innerHTML' somewhere?
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jun 01, 2007, 03:44:48 PM »

use the debug option from Xajax:

$xajax->bDebug = true;


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« Reply #8 on: Jun 01, 2007, 04:06:34 PM »

OK, I set debug=true, and the debug told me that the response was too long.
So I seriously trimmed the HTML code to try and make it short enough to appear on the browser message, and it had advised me that it 'cannot modify header information'
Headers already sent by my .php page, but a new header is requested by xajax.inc.php
I'm guessing I can modify this somewhere?
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Jun 01, 2007, 08:09:06 PM »

OK, I set debug=true, and the debug told me that the response was too long.
So I seriously trimmed the HTML code to try and make it short enough to appear on the browser message, and it had advised me that it 'cannot modify header information'
Headers already sent by my .php page, but a new header is requested by xajax.inc.php
I'm guessing I can modify this somewhere?

there is a php error somewhere...undefined variable or something


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Gender: Male
Posts: 6
40 credits
Members referred : 0


« Reply #10 on: Jun 04, 2007, 02:59:38 PM »

Thanks for your help, I'm working on the php now
OMG!I am geek
**
Gender: Male
Posts: 57
370 credits
Members referred : 0



« Reply #11 on: Aug 01, 2007, 02:31:36 AM »

you need to escape the single quotes (not replacing them with double quotes)
Seems i have same trouble. How to escape the single quotes? Can you write simple example? ... and ..hmm, is it javascript code? They asking to put this code before html and i have no glue why nothing working but now all is same.
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Aug 01, 2007, 06:56:17 AM »

you need to escape the single quotes (not replacing them with double quotes)
Seems i have same trouble. How to escape the single quotes? Can you write simple example? ... and ..hmm, is it javascript code? They asking to put this code before html and i have no glue why nothing working but now all is same.

escaping (single) quotes:

\' or \"

easy or not?


Last blog : Is your website is down? Know before your visitors do!
OMG!I am geek
**
Gender: Male
Posts: 57
370 credits
Members referred : 0



« Reply #13 on: Aug 01, 2007, 12:53:50 PM »

This is easy, thank you.. i just think this working only in perl?
OMG!I am geek
**
Gender: Male
Posts: 57
370 credits
Members referred : 0



« Reply #14 on: Aug 01, 2007, 01:21:25 PM »

one more question. I change this first peace of script from php to perl, i mean i just change <?php to <script type=.... etc. you see, in this tutorial at
http://www.finalwebsites.com/tutorials/php_ajax_contact_form.php Visit through proxy
they dont explain which code is for js and which for php, so i just trying different ways. So, when i change to javascript, at least my page start work a bit.

Now i have next trouble. I've got fatal error with this line:
<?php $xajax->printJavascript('xajax/'); ?>
i read article at xajax tutorial and normaly this line is like that:
"6. Between your tags, tell xajax to generate the necessary JavaScript:
<?php $xajax->printJavascript(); ?>"

is this some link to xajax library? Why they use ('xajax/') instead of (). If this is link then i have to change some other things...

finaly .. i just thinking, maybe i have to just put simple mail form and forget about xajax..
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #15 on: Aug 01, 2007, 01:54:00 PM »

one more question. I change this first peace of script from php to perl, i mean i just change <?php to <script type=.... etc. you see, in this tutorial at
http://www.finalwebsites.com/tutorials/php_ajax_contact_form.php Visit through proxy
they dont explain which code is for js and which for php, so i just trying different ways. So, when i change to javascript, at least my page start work a bit.

Now i have next trouble. I've got fatal error with this line:
<?php $xajax->printJavascript('xajax/'); ?>
i read article at xajax tutorial and normaly this line is like that:
"6. Between your tags, tell xajax to generate the necessary JavaScript:
<?php $xajax->printJavascript(); ?>"

is this some link to xajax library? Why they use ('xajax/') instead of (). If this is link then i have to change some other things...

finaly .. i just thinking, maybe i have to just put simple mail form and forget about xajax..

I guess this tutorial is not for php beginners (don't know why your try to translate php code to perl)


Last blog : Is your website is down? Know before your visitors do!
OMG!I am geek
**
Gender: Male
Posts: 57
370 credits
Members referred : 0



« Reply #16 on: Aug 01, 2007, 02:13:52 PM »

yea, this really not for beginners. Just usually till this script was easy to use some php script or perl script into my pages. This is first where i cannot find solution.

i dont want to translate to perl, i just didnt now what php have same backward slash function. This is why. Anyway, i will try to find solution and if i will, i post here. I thought this mail form is very useful!
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #17 on: Aug 01, 2007, 04:20:43 PM »

yea, this really not for beginners. Just usually till this script was easy to use some php script or perl script into my pages. This is first where i cannot find solution.

i dont want to translate to perl, i just didnt now what php have same backward slash function. This is why. Anyway, i will try to find solution and if i will, i post here. I thought this mail form is very useful!

you don't need to learn so much about php to use this tutorial but you need to know some basics


Last blog : Is your website is down? Know before your visitors do!
Kill the googlebot
*
Posts: 6
36 credits
Members referred : 0


« Reply #18 on: Oct 22, 2007, 05:24:29 AM »

Hi Guys, that is my first post I hope I can find some help.
I have many difficulties running this ajax form here is my code:

<?
require_once('phpmailer/class.phpmailer.php');
require ('xajax/xajax_core/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;"> --&gt; </label>
              <input type="button" id="subbtn" value="Submit" onclick="xajax_myFunction(xajax.getFormValues(\'cform\'));" />
            </div>
          </form>';

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;
}



          ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd Visit through proxy">
<html>
 <head>
 <title>xajax example</title>
<?php $xajax->printJavascript('xajax/'); ?>


           </head>

           <body>
         <?php
      $xajax = new xajax();
      $xajax->registerFunction('myFunction');
      $xajax->processRequests();
      echo '<div id="contact_result">'.$form.'</div>';
      ?>


      </body>
      </html>

But is doesnt work, here is the error message

Fatal error: Call to a member function on a non-object in /home/xxx/public_html/fmail.php on line 71


This line caused a problem for me

<?php $xajax->printJavascript('xajax/'); ?>

Any idea what is wrong

Best regards
Wisam
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #19 on: Oct 22, 2007, 10:16:38 AM »

place this code above all html
         <?php
      $xajax = new xajax();
      $xajax->registerFunction('myFunction');
      $xajax->processRequests();

      ?>


Last blog : Is your website is down? Know before your visitors do!
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6687
Tags : xajax php Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Safe contact form using xajax & PHP
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 08, 2008, 07:29:07 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.302
Total Topics: 7.479
Total Members: 3.907
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: phpprofit

14 Guests, 3 Users online :

6 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.