Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,174,022 members, 7,890,358 topics. Date: Monday, 15 July 2024 at 01:00 PM

Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax (1243 Views)

How Do I Add Google Captcha To A Form And Send All Details To A Specific Email.. / Help On Html Code That Displays Form Details After Submitting Form / Help With Attendance Form And Report With Microsoft Access (2) (3) (4)

(1) (Reply) (Go Down)

Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 12:37pm On Jul 07, 2016
This is actually an answer to a stackoverflow question


Index.php

<div id="ajax-test">
<form method="POST" onsubmit="return false;">
<textarea onkeyup="loadDoc()" name="someText" id="someText" placeholder="Type something here"></textarea>
<button type="button" onclick="loadDoc()">Submit</button>
</form>
<div id="ajax-text"></div>
</div>
<script type="text/javascript" src="main.js"></script>



Main.js file

function _(x) {
return document.getElementById(x);
}
function ajaxObj ( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200) {
return true;
}
}
function loadDoc() {
var someText = _( "someText" ).value;
var ajax = ajaxObj( "POST", "ajax/ajax.php" ) ;

ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
_('ajax-text').innerHTML = ajax.responseText;
console.log(ajax.responseText);
}
}
ajax.send("someText="+someText);
}




ajax/ajax.php file

if(isset($_POST['someText'])){
$someText = $_POST['someText'];
echo "\"$someText\"" . ' is what you wrote';
exit();
} else {
echo "An error occured";
exit();
}


So in a nutshell, what I'm doing is just getting an errands boy (AJAX) to deliver a XmlHttp request to a PHP script and bring back the response and display it on the page. This technique is being used by developers for instant messaging or chatting, just that the programming languages involved might differ.

I hope you newbies understand this else comment below.
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by VenantCode(m): 10:23am On Jul 08, 2016
Yeah, but that is kinda old already. With the notion of websockets you don't need to continously poll the server unecessarily.

1 Like

Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 6:17pm On Jul 08, 2016
VenantCode:
Yeah, but that is kinda old already. With the notion of websockets you don't need to continously poll the server unecessarily.

Chill man, every beginner has to start somewhere... Using websockets is more advanced, at least after learning how to achieve the goal with AJAX newbies can venture into websockets.
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by ANTONINEUTRON(m): 12:22pm On Jul 10, 2016
I Just Got Confuse,

Will Ur Ajax Make The Page Not Reload Before Displaying The User Message "$sometext"
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Laolballs: 10:45pm On Jul 10, 2016
Men, with jquery you hlcan reduce the line of code to letsay 8 or leass, i mean jquery ajax is far way better than this, it handles all the browser compatibility issues for ya... Lets embrace technology, let use libraries, it makes live easy and gets the job done faster, i would suggest doing this with jquery, it helps beginners build something meanigul in the shortest possible time, and hey the new html5 server sent event , is as cool as websocket, and not as painful to implement as websocket.......
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 11:31pm On Jul 10, 2016
Interesting
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by sleepingdemon: 11:45pm On Jul 10, 2016
Laolballs:
Men, with jquery you hlcan reduce the line of code to letsay 8 or leass, i mean jquery ajax is far way better than this, it handles all the browser compatibility issues for ya... Lets embrace technology, let use libraries, it makes live easy and gets the job done faster, i would suggest doing this with jquery, it helps beginners build something meanigul in the shortest possible time, and hey the new html5 server sent event , is as cool as websocket, and not as painful to implement as websocket.......
if yiu understand server sent events well, you should know that unlike websockets, headers are sent alongside the events. also, server sent events are not exactly exactly real time, as they would need to sleep while indirectly polling your database, collect the reply and deliver to your web page, but looking at the nature of websockets, expecially those done with node.js or tornado (python) because of its asynchronous nature, even if you are inserting to database immediately the message is passed through the socket, your reply is gotten instantly as it dosent wait for the request to complete before replying you with another thread.
i hope you understand
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 7:33am On Jul 11, 2016
Exactly, plus if you are dealing with websockets - once you are connected, you do need to call the server at intervals to know if anything is happening, the server itself will call you.
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 8:37am On Jul 11, 2016
ANTONINEUTRON:
I Just Got Confuse,

Will Ur Ajax Make The Page Not Reload Before Displaying The User Message "$sometext"

Yes, exactly.
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 8:37am On Jul 11, 2016
dhtml18:
Exactly, plus if you are dealing with websockets - once you are connected, you do need to call the server at intervals to know if anything is happening, the server itself will call you.
Re: Submitting PHP Form And Receiving Instant Response Asynchronously Using Ajax by Nobody: 8:47am On Jul 11, 2016
Laolballs:
Men, with jquery you hlcan reduce the line of code to letsay 8 or leass, i mean jquery ajax is far way better than this, it handles all the browser compatibility issues for ya... Lets embrace technology, let use libraries, it makes live easy and gets the job done faster, i would suggest doing this with jquery, it helps beginners build something meanigul in the shortest possible time, and hey the new html5 server sent event , is as cool as websocket, and not as painful to implement as websocket.......

If you have eyes, and you no how to examine code well, you would see that what I have done will benefit you and your website instead over-loading your website with JQuery scripts/plugins.

The Ajax code can be transferred to a seperate .js file and called from anywhere you need Ajax
 
ajaxObj( 'method', 'script')
. Plus if it's because of
document.getElementById(); ==> $ 
you prefer JQuery, i replaced mine with an underscore. So
document.getElementById('goat'); is the same as _('goat')

Although JQuery has many core advantages, but using native JavaScript in your projects is an advantage.

Note: I'm not against JQuery oo, me self i'd probably launch one plugin for Jquery soon :-) .

(1) (Reply)

Website Designer, Web Developer In Asaba Delta State / 5 Reasons Why Java Is The Best Programming Language / PHP Script Wanted

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.