Skip to content

Commit

Permalink
AsyncPHP added
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Sep 27, 2024
1 parent 78b0a8f commit 3947632
Show file tree
Hide file tree
Showing 19 changed files with 683 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Async-PHP/Backend/db_connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$port = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $port);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}




//else{
// echo "Connected successfully";
//}
// Make sure to stop connection in files where you will use this


?>

58 changes: 58 additions & 0 deletions Async-PHP/Backend/multiple_async_get_outputs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


require_once '../lib/AsyncRunner.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "Received POST request.<br>"; // Debug statement
$path = '../async_scripts';
$scripts = ["$path/script1.php", "$path/script2.php", "$path/script3.php"];

// Run the scripts asynchronously
$outputs = runScriptsAsync($scripts); // Call the function directly
echo "Scripts executed. Outputs:<br>"; // Debug statement

// Initialize variables for individual outputs
$output1 = '';
$output2 = '';
$output3 = '';

// Assign outputs to individual variables
if (isset($outputs[0])) {
$output1 = trim($outputs[0]); // Script 1 output
}
if (isset($outputs[1])) {
$output2 = trim($outputs[1]); // Script 2 output
}
if (isset($outputs[2])) {
$output3 = trim($outputs[2]); // Script 3 output
}

// Output the individual outputs
echo "Output 1: " . $output1 . "<br>";
echo "Output 2: " . $output2 . "<br>";
echo "Output 3: " . $output3 . "<br>";

// Initialize the sum
$sum = 0;

// Calculate the sum only for non-empty outputs
if ($output1 !== '') {
$sum += floatval($output1);
}
if ($output2 !== '') {
$sum += floatval($output2);
}
if ($output3 !== '') {
$sum += floatval($output3);
}

// Output the total sum
echo "<strong>Total Sum: " . $sum . "</strong>";
echo "ok";
}




?>
8 changes: 8 additions & 0 deletions Async-PHP/Backend/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php


echo "hellow from AsyncPHP";



?>
18 changes: 18 additions & 0 deletions Async-PHP/Backend/template_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once '../lib/AsyncRunner.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$path = '../async_scripts';
$scripts = ["$path/db_script1.php", "$path/db_script2.php", "$path/db_script3.php"];

$outputs = runScriptsAsync($scripts); // Call the function directly

foreach ($outputs as $output) {
echo "<pre>$output</pre>";
}
}

echo "Script executed successfully.";

?>
25 changes: 25 additions & 0 deletions Async-PHP/app/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect any requests with .php extension to the URL without the extension
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]

# Internally rewrite requests without .php to the actual .php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]


# Route /app to app/index.php
RewriteRule ^app/?$ /app/index.php [L]

# If you want to handle all requests under /app (e.g., /app/something)
RewriteRule ^app/(.*)$ /app/index.php [L,QSA]

</IfModule>

# Enable the rewrite engine
Options +FollowSymLinks

23 changes: 23 additions & 0 deletions Async-PHP/app/reuseable_views/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();
var url = $(this).data('url'); // Get the URL from the data attribute
$.ajax({
url: url,
method: 'POST',
success: function(data) {
$('#output').html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
});
});
</script>
</body>
</html>

31 changes: 31 additions & 0 deletions Async-PHP/app/reuseable_views/footer_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
document.addEventListener('DOMContentLoaded', () => {
const button = document.querySelector('.runScripts');
button.addEventListener('click', (e) => {
e.preventDefault();
const url = button.getAttribute('data-url'); // Get the URL from the data attribute
fetch(url, {
method: 'POST', // Change to POST
headers: {
'Content-Type': 'application/x-www-form-urlencoded' // Set content type
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.text();
})
.then(data => {
document.getElementById('output').innerHTML = data;
})
.catch(err => {
console.error('Error fetching output:', err);
document.getElementById('output').innerHTML = 'Error fetching output: ' + err.message;
});
});
});
</script>
</body>
</html>
38 changes: 38 additions & 0 deletions Async-PHP/app/reuseable_views/footer_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();
// Get the URL from the data-url attribute of the button
var url = $(this).data('url');
// Get the form associated with the button that was clicked
var form = $(this).closest('form');
// Check if a form was found
if (form.length) {
// Gather form data using FormData object
var formData = new FormData(form[0]);
$.ajax({
url: url,
type: 'POST',
data: formData,
processData: false, // Do not process the data into a query string
contentType: false, // Do not set content type
success: function(data) {
$('#output').html(data); // Show response in the output div
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
} else {
$('#output').html("Error: No form associated with this button.");
}
});
});
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions Async-PHP/app/reuseable_views/footer_form.php.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();

// Get the URL from the data-url attribute of the button
var url = $(this).data('url');

// Gather form data using FormData object
var formData = new FormData($('#userForm')[0]);

$.ajax({
url: url,
type: 'POST',
data: formData,
processData: false, // Do not process the data into a query string
contentType: false, // Do not set content type
success: function(data) {
$('#output').html(data); // Show response in the output div
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
});
});
</script>
</body>
</html>
31 changes: 31 additions & 0 deletions Async-PHP/app/reuseable_views/footer_json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
$(document).ready(function() {
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();
var url = $(this).data('url'); // Get the URL from the data attribute
console.log("AJAX URL:", url); // Log the URL to console
$.ajax({
url: url,
method: 'POST',
success: function(data) {
console.log("AJAX Success:", data); // Log the response
$('#output').html(data); // Directly output the raw response data
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("AJAX Error:", textStatus, errorThrown); // Log error details
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
});
});
</script>


</body>
</html>

11 changes: 11 additions & 0 deletions Async-PHP/app/reuseable_views/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo isset($pageTitle) ? $pageTitle : 'Default Title'; ?></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body class="container">

17 changes: 17 additions & 0 deletions Async-PHP/app/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
$pageTitle = 'Index'; // Set the page title
include 'reuseable_views/header.php'; ?>


<h1 class="mt-5">Logs</h1>
<button class="runScripts" class="btn btn-primary mb-3" data-url="../Backend/template.php">Run Scripts</button>
<h2>Script Outputs:</h2>
<div id="output" class="bg-light p-3"></div>



<?php include 'reuseable_views/footer.php'; ?>




19 changes: 19 additions & 0 deletions Async-PHP/async_scripts/db_connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$port = ;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $port);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}



?>

Loading

0 comments on commit 3947632

Please sign in to comment.