1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

Ajax With Multiple Forms And One PHP Processing File

Ajax and PHP scripts For This Lesson

Create a file and call it index.php. Here is the code to put into that file.

<html> 
<head> 
<title>Ajax With Multiple Forms And One PHP Processing File</title> 
</head> 
<body> 
<input type="text" id="name" size="50" placeholder="Name"> 
<button type="button" id="sub" onclick="ajax_post();">GO</button><br/>
<input type="text" id="buddy" size="50" placeholder="Your Buddy"> 
<button type="button" id="sub" onclick="ajax_post_buddy();">GO</button><br/>
<div id="status">You are:</div> 
<div id="statustwo">Your buddy is: </div> 
<script> 
function ajax_post(){ 
 var hr = new XMLHttpRequest(); 
 var url = "processor.php"; 
 var name1 = document.getElementById("name").value; 
 var vars = "namee="+name1; 
 hr.open("POST", url, true); 
 hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
 hr.onreadystatechange = function() 
 { if(hr.readyState == 4 && hr.status == 200) { 
var return_data = hr.responseText; document.getElementById("status").innerHTML = return_data; 
 } 
} 
hr.send(vars); // After Check Steps are done execute the request
 document.getElementById("status").innerHTML = "processing..."; } 

function ajax_post_buddy(){ 
 var hr = new XMLHttpRequest(); 
 var url = "processor.php"; 
 var buddy1 = document.getElementById("buddy").value; 
 var vars = "buddy="+buddy1; 
 hr.open("POST", url, true); 
 hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
 hr.onreadystatechange = function() 
 { if(hr.readyState == 4 && hr.status == 200) { 
var return_data = hr.responseText; document.getElementById("statustwo").innerHTML = return_data; 
 } 
} 
hr.send(vars); // After Check Steps are done execute the request
 document.getElementById("statustwo").innerHTML = "processing..."; } 
</script>
</body>
</html>

Now create a file and call it processor.php and put this code in that file.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂
<?php
if(isset($_POST['namee'])){
echo 'My Name is';
echo $_POST['namee'];
}
echo $_POST['buddy'];
?>
Ajax With Multiple Forms And One PHP Processing File was last modified: June 11th, 2017 by Maximus Mccullough

Subscribe To My YouTube Channel:

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share:
Ajax With Multiple Forms And One PHP Processing File

Pages:Previous 1 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.