473,586 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX&PHP form validation doesn't work

26 New Member
Hi, I recently learned how to use AJAX, but I'm having a problem with it. What I'm trying to do is validate a form with PHP, and write the output of the validation in the same page. From the last question I have asked in bytes.com, I was introduced to AJAX(well, I always knew it, but I never used it). So, I wrote the following code accordingly:

index.html:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.  
  3. <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.  
  6. <title> Unimail </title>
  7.  
  8. <script type="text/javascript" src="/scripts/main.js"></script>
  9. <link rel="shortcut icon" href="/images/favicon.ico" />
  10. <link rel="stylesheet" type="text/css" href="/styles/main.css" />
  11.  
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  13. <meta property="og:description" content="Free one-time use email" />
  14. <meta property="og:title" content="Unimail email service" />
  15. <meta name="author" content="Unimail" />
  16. <meta property="og:site_name" content="Unimail" />
  17.  
  18. </head>
  19.  
  20. <body>
  21.  
  22. <div id="wrapper">
  23. <div id="top">
  24.     <div class="header">
  25.         <img src="/images/email_logo4.jpg" alt="Unimail" />
  26.     </div>
  27.     <div class="navig">
  28.         <ul class="navbar">
  29.             <li><a href="http://localhost/index.html"><span>Home</span></a></li>
  30.             <li><a href="#">About us</a></li>
  31.             <li><a href="#">FAQ</a></li>
  32.             <li><a href="#">Contact</a></li>
  33.             <li><a href="#">Other services</a></li>
  34.         </ul>
  35.     </div>
  36. </div>
  37.  
  38. <div id="body">
  39.     <br />
  40.     <p> Hello! Welcome to Unimail! With Unimail, you can send emails without registering. No inbox, no pre-chosen address, no name. One-time email! </p>
  41.  
  42.     <br /><br />
  43.  
  44.     <form name="sendmail" action="/scripts/send.php" method="post" class="sender">
  45.  
  46.         <p>Recipient's address </p> <input name="recipient" type="text" id="recipient" /><br />
  47.         <p> Title </p><input name="title" type="text" id="title" />
  48.         <p> Body </p><textarea rows="15" cols="65" name="body" id="body" ></textarea><br />
  49.         <input name="submit" type="submit" value="Send!" onclick="getObject();"/>
  50.         <span id="error"> </span>
  51.  
  52.     </form>
  53.     <p> Remember! You will not be able to receive any reply! </p>
  54. </div>
  55. <div id="footer">
  56.     <p> This site was created for occasional emailing. Please do not attempt to use this site to spam emails. </p>
  57. </div>
  58.  
  59. </div>
  60.  
  61. </body>
  62.  
  63. </html>
  64.  
send.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require_once('class.phpgmailer.php');
  4.  
  5. $emailRep = "";
  6. $titleRep = "";
  7. $bodyRep = "";
  8.  
  9. function verifyEmail($email)
  10. {
  11.     if (filter_var($email, FILTER_VALIDATE_EMAIL) == true)
  12.     {
  13.         return true;
  14.     }
  15.  
  16.     $emailRep = "Error: Invalid email address!";
  17. }
  18.  
  19. function verifyTitle($title)
  20. {
  21.     if (strlen($title) > 0)
  22.     {
  23.         return true;
  24.     }
  25.  
  26.     $titleRep = "Error: Empty titles are not allowed!";
  27. }
  28.  
  29. function verifyBody($body)
  30. {
  31.     if (strlen($body) > 19)
  32.     {
  33.         return true;
  34.     }
  35.  
  36.     $bodyRep = "Error: Your email must consist of at least 20 character!";
  37. }
  38.  
  39. $to = $_POST['recipient'];
  40. $subject = $_POST['title'];
  41. $message = $_POST['body'];
  42.  
  43. if (verifyEmail($to) && verifyTitle($subject) && verifyBody($message))
  44. {
  45.     $mail = new PHPGMailer();
  46.     $mail->Username = 'unimail.auto@gmail.com';
  47.     $mail->Password = '***********';
  48.     $mail->From = 'unimail.auto@gmail.com';
  49.     $mail->FromName = 'Unimail';
  50.     $mail->Subject = $subject;
  51.     $mail->AddAddress($to);
  52.     $mail->Body = $message;
  53.     $mail->Send();
  54.  
  55.     echo "Message sent!";
  56. }
  57.  
  58. else
  59. {
  60.     echo $emailRep . "<br />" . $titleRep . "<br />" . $bodyRep;
  61. }
  62.  
  63. ?>
  64.  
main.js:
Expand|Select|Wrap|Line Numbers
  1. function getObject()
  2. {
  3.     var http;
  4.  
  5.     try
  6.     {
  7.         http = new XMLHttpRequest();
  8.     }
  9.  
  10.     catch(e)
  11.     {
  12.         try
  13.         {
  14.             http = new ActiveXObject("Msxml2.XMLHTTP");
  15.         }
  16.  
  17.         catch(e)
  18.         {
  19.             http = new ActiveXObject("Microsoft.XMLHTTP");
  20.         }
  21.     }
  22.  
  23.     function getServer()
  24.     {
  25.         if (http.readyState == 4)
  26.         {    
  27.             if (http.responseText.match("Error") == "Error")
  28.             {
  29.                 document.getElementById("error").style.color = "red";
  30.                 document.getElementById("error").innerHTML = "  " + http.responseText;
  31.             }
  32.  
  33.             else
  34.             {
  35.                 document.getElementById("error").style.color = "green";
  36.                 document.getElementById("error").innerHTML = "  " + http.responseText;
  37.             }    
  38.         }
  39.     }
  40.  
  41.     var email = document.getElementById("recipient");
  42.     var title = document.getElementById("title");
  43.     var body = document.getElementById("body");
  44.  
  45.     http.open("POST", "send.php", true);
  46.     http.onreadystatechange = getServer();
  47.     http.send(null);
  48. }
  49.  
However, it is not working. From what I understand, after checking for flow in each of the files, there's a problem with my JS code. I may be wrong, but from what I tested it seems that it's a problem with the JS.

What's wrong?? I can't figure it out!

Thanks! :D
Dec 12 '10 #1
26 2684
Dormilich
8,658 Recognized Expert Moderator Expert
there are several problems:

- index.html #49. whatever getObject() does, after it is done the form will submit, the page reload and thus no JavaScript output visible.

- main.js #46. a DOM-0 event handler expects a function to be passed, not a function return value. i.e. http.onreadysta techange = getServer;
Dec 12 '10 #2
liams
26 New Member
Thanks for the reply!

How could I fix the problem with getObject() so that I will be able to display what I want, the way I want(in the same page). I know this is a simple question, but I'm not very familiar with how AJAX works :P
Dec 12 '10 #3
Dormilich
8,658 Recognized Expert Moderator Expert
either use a click button or prevent the form from submitting (either by preventing the default action or by explicitly returning false from the event attribute)
Dec 12 '10 #4
liams
26 New Member
I did this according to this example:
http://www.tizag.com/ajaxTutorial/aj...ttprequest.php

Why does it stay on the same page for them, and not for me? I would really like to avoid doing such things like preventing the form from submitting.

Edit:

I removed the action="/scripts/send.php" from index.html. However, it is still not working. I'm getting no output.
Dec 13 '10 #5
Dormilich
8,658 Recognized Expert Moderator Expert
Why does it stay on the same page for them, and not for me?
because they don’t use a submit button.
Dec 13 '10 #6
liams
26 New Member
Thanks for the reply:D

Can you give me any example for a code that actually works? I have no idea how to work with event attributes(I only recently started doing websites.. I'm more of a desktop guy).
Dec 13 '10 #7
Dormilich
8,658 Recognized Expert Moderator Expert
I have no idea how to work with event attributes(I only recently started doing websites.. I'm more of a desktop guy).
how much do you want to understand JavaScript Events?

Can you give me any example for a code that actually works?
do you have a working page, so that I can check whether the AJAX itself works?
Dec 13 '10 #8
liams
26 New Member
I want to understand JS events as much as I can. My goal is to be good.
About the page, I don't use any external hosting. I run it with my Apache server.
Dec 13 '10 #9
Dormilich
8,658 Recognized Expert Moderator Expert
I want to understand JS events as much as I can.
this will not be a piece of cake …

on another thread I wrote a short summary about Events.

you should additionally read some resources:
- Peter Paul Koch’s Quirksmode.org (Intro, ToC)
- the DOM-Events specifications: DOM-2-Events, DOM-3-Events (there should be something for IE on the MSDN website, too). note that these documents are technical, they exactly describe how events work, i.e. it is not meant as tutorial. still, the introduction section is worth a read to understand the event flow.
Dec 13 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
9548
by: k.mitz | last post by:
Hi, I have a PHP application that allows users to generate a .pdf report of their database content. Normally, I've had to refresh a page to call the script to generate the report, so there's a second or so when the browser goes blank. I was wondering if it was possible to use AJAX to call the script to generate the report, then begin the...
3
1775
by: joeakabloo | last post by:
I've been working on an AJAX web app, my first one actually. Run into an issue I'm not sure how to resolve (been googling and reading trying to find a fix). Basic premise: Using AJAX to handle calls to PHP scripts, which return DOM-creating javascript to the browser. This javascript usually creates forms. No issue with performance, works...
6
1388
by: yonido | last post by:
hey my final goal: to add a link to an email message, that will contact my server & mark the email as "important", with no popup windows/annoying messages. id like to do this in ajax, but the problem is javascript support in emails. i want it to work in every computer, even if its viewed in gmail.
7
1809
by: BeeRich | last post by:
Hi folks. I built an ajax.updater reply in a website on a Mac, and she works fine. It's a simple reply, the html supplied is correct, and I also supply the target DIV as well. Just wondering why a PC won't show anything. Cheers
1
2409
by: Samuel | last post by:
Hi, I have ASP.NET user control that implements ICallbackEventHandler. Partial postback works perfectly when I put control on a regular webform. But when I put it on page that has master page control doesn't work. I'm getting following error :"The target <namefor the callback could not be found or did not implement ICallbackEventHandler."...
2
2940
by: sebastian.janoschka | last post by:
Hi, I build my first Drag & Drop with JavaScript and I would like to drag the pictures when I click on it. When I create a normal div tag with some text the script works, but when I put a image in the div tag it works not correctly. Here is my Code... I integrated three pictures and one text, then you can see the differences.
0
5546
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
1404
by: Tony Sedgwick | last post by:
I have installed the AJAX extensions and partial page rendering works ok from a new ajax enabled web site I created in VS. The problem is that I've manually entered the necessary entries into the web.config into an existing project and the same asp.net file doesn't work from the migrated web.config project. When I compared the html source...
0
3159
by: jrnail23 | last post by:
I have a user control which contains an UpdatePanel, which contains a MultiView inside, with a GridView in one of the views. In my GridView, I have a ButtonField which is supposed to trigger a server-side operation, using the RowCommand functionality. In IE, this all works great, but in Firefox (v2.0.0.6), clicking the above mentioned...
5
1504
by: krg | last post by:
Hi, I started writing this blog some time back and it would be great if I could get an audience here and even better if we could have a conversation about developments possible on the techniques I have discussed in my blog. Automatic Javascript Bug Reporting Using AJAX http://krahulg.wordpress.com/2007/12/24/automatic-javascript-bug-...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.