Sublime directory Surf the web anonymous Pagerank Monitor


Safe contact form using xajax & PHP

outlando
Fri 1 June 2007, 09:07 am GMT +0200
Hey people

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

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), 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

olaf
Fri 1 June 2007, 09:44 am GMT +0200
Hi,

are your sure that php is working well?

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

outlando
Fri 1 June 2007, 10:06 am GMT +0200
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...

olaf
Fri 1 June 2007, 10:17 am GMT +0200
you need to escape the single quotes (not replacing them with double quotes)

outlando
Fri 1 June 2007, 12:28 pm GMT +0200
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.

olaf
Fri 1 June 2007, 01:08 pm GMT +0200
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

outlando
Fri 1 June 2007, 02:41 pm GMT +0200
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?

olaf
Fri 1 June 2007, 02:44 pm GMT +0200
use the debug option from Xajax:

$xajax->bDebug = true;

outlando
Fri 1 June 2007, 03:06 pm GMT +0200
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?

olaf
Fri 1 June 2007, 07:09 pm GMT +0200
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

outlando
Mon 4 June 2007, 01:59 pm GMT +0200
Thanks for your help, I'm working on the php now

droom
Wed 1 August 2007, 01:31 am GMT +0200
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.

olaf
Wed 1 August 2007, 05:56 am GMT +0200
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?

droom
Wed 1 August 2007, 11:53 am GMT +0200
This is easy, thank you.. i just think this working only in perl?

droom
Wed 1 August 2007, 12:21 pm GMT +0200
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
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..

olaf
Wed 1 August 2007, 12:54 pm GMT +0200
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
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)

droom
Wed 1 August 2007, 01:13 pm GMT +0200
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!

olaf
Wed 1 August 2007, 03:20 pm GMT +0200
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

thewis
Mon 22 October 2007, 04:24 am GMT +0200
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">
<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

olaf
Mon 22 October 2007, 09:16 am GMT +0200
place this code above all html
         <?php
      $xajax = new xajax();
      $xajax->registerFunction('myFunction');
      $xajax->processRequests();

      ?>

thewis
Mon 22 October 2007, 02:30 pm GMT +0200
Thanks but I have tried that before but the form doesn t work

http://www.seogta.com/fmail.php

olaf
Mon 22 October 2007, 04:20 pm GMT +0200
Thanks but I have tried that before but the form doesn t work

http://www.seogta.com/fmail.php

I need to see the whole script

thewis
Tue 23 October 2007, 02:11 am GMT +0200
Here is the whole script


<?
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>';

//onsubmit="xajax_sendmail(xajax.getFormValues('form1'))

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;
}
$xajax = new xajax();
$xajax->registerFunction('myFunction');
$xajax->processRequest();

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


           </head>

           <body>
         <?php

      echo '<div id="contact_result">'.$form.'</div>';
      ?>


      </body>
      </html>

olaf
Tue 23 October 2007, 06:35 am GMT +0200
looks good, do you get any error message?
maybe some variable is not filled, enable error reporting in your .htaccess file:

php_value display_errors On

thewis
Wed 24 October 2007, 01:21 am GMT +0200
I have done that, but I can not see any errors, can anybody have it working forward me a copy to test ... the only reason not to have it working is my host (assuming my code is correct), but this host a standard one (Cpanel Linux)

olaf
Wed 24 October 2007, 08:34 am GMT +0200
sorry all code looks fine, the only things which is over is the host :(

who is your hosting provider?

thewis
Thu 25 October 2007, 04:47 am GMT +0200
I have 4 hosts, I tried the scripts on a shared account with HostForWeb.com and Dedicated server from HostForWeb as well but it did not work .. I can try it on the other two host, but I dont think it is a host issue .. the form doesn respond at all to the submit button. I would like to repeat my request for you to send me ready to use files to try them .. may be I missed something

olaf
Thu 25 October 2007, 06:13 am GMT +0200
I have 4 hosts, I tried the scripts on a shared account with HostForWeb.com and Dedicated server from HostForWeb as well but it did not work .. I can try it on the other two host, but I dont think it is a host issue .. the form doesn respond at all to the submit button. I would like to repeat my request for you to send me ready to use files to try them .. may be I missed something
if your dedicated host doesn't work it shouldn't be a hosting problem. I will try your example on my own server and we will see...

olaf
Thu 25 October 2007, 06:22 am GMT +0200
hey wait...

I think you're using a diff. xajax version:
require ('xajax/xajax_core/xajax.inc.php');

I remember be that the author of xajax has changed several things in a newer version.

Try using xajax ver. Version 0.2.4 (stable release)

thewis
Fri 26 October 2007, 05:09 am GMT +0200
Thanks for your time, I give up :)  .. I will look for something else

Archive for SMF v1.00 by N.P. Valid XHTML 1.0 Transitional