Executing a PHP file in JavaScript without using the jQuery library can be achieved by making

As a beginner in php and javascript, I am seeking a straightforward method to execute a php file from javascript. While browsing through various examples (here and here), most of them rely on jQuery which I prefer to avoid.

My goal is to trigger the update process on the server similar to the second example.

var stillAlive = setInterval(function () {
    /* XHR back to server
       Example uses jQuery */
    $.get("stillAlive.php");
}, 60000);

I have considered using an AJAX approach, but all the tutorials I found focus on sending and receiving data with the request. In this case, I simply need to execute a basic php file without the need to send any data. I am unsure how to utilize AJAX in this straightforward manner.

Thank you for any suggestions offered.

Answer №1

Utilizing AJAX without the need for jQuery, this method effectively runs a PHP file every 5 seconds.

<script>
    var execute_php = function () {
      var request = new XMLHttpRequest();
      request.open("GET", "myroutine.php", true);
      request.send();
    }

    setInterval(execute_php, 5000);
</script>

Answer №2

const createFrame = () => {
   const frame = document.createElement("iframe");
   frame.src = "yourfile.php";
   frame.style.opacity = 0;
   body.appendChild(frame);
   
   frame.onload = function() {
      body.removeChild(this);
      window.setTimeout(function(){
         createFrame();
      }, 2000);
   }
   
   createFrame();

This script generates a new iframe, destroys it once loaded, and then restarts the process after a 2-second delay.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you use the syntax to refer to two separate arrays in a for loop by using a third array

Apologies if my question is unclear, allow me to clarify: I have two arrays in 2D format: const letterA = [ [...],[...],...]; const letterB = [ [...],[...],...]; const letterArray["A","B"]; I want to access them using a for loop like so: let i = 0; for ...

Unable to find a hidden JavaScript function

I'm facing an unusual challenge with my project. I'm currently developing a system using JSF2 (Java) and the Primefaces component library. I have multiple buttons triggering a JavaScript function called checkParams() on a onclick event. Now, I ne ...

Animating Text Changes with JavaScript and jQuery

In my HTML, I have an input type="text" element and I want to attach an event handler that triggers when the text is changed. The issue arises because this input's value is dynamically updated by another JavaScript function, causing the event handler ...

The getUUID() function in ngCordova is causing an error where the device is

Getting 'device is not defined' error with ngCordova getUUID() In the process of developing a mobile app using Ionic framework, I encountered an issue with retrieving the device UUID. To tackle this problem, I decided to leverage ngCordova by in ...

The issue with the serialization of the select field not functioning properly when using

Having an issue with sending multiple values from 3 select menus over ajax as an array using serialize(). Currently, it's only sending one value instead of all 3. Any insights on what might be causing this? var menu = $('select[name^="menu"]&a ...

Unable to stack a div on top of a chart's div in HTML + (NV)D3 despite using a higher z-index value

I am facing an issue with my HTML and CSS code. Here is what it looks like: <div id="container"> <svg id="chart1"></svg> <div id='logo'> <img id="logo" src="cubs_best.png";> </div> </div ...

Issues with jQuery Functions Arising After Utilizing .load() to Update div Element Text

After processing form data, I am reloading the content of "#some-div" using the ".load()" function. It successfully refreshes "#some-div" with the updated content. However, the issue I am facing is that none of my other jQuery functions (such as ".some-t ...

Diminishing sheets in the realm of C# web application development

I have been researching ways to incorporate a fading page function, but I am encountering some issues. I am unsure about the specific code that needs to be included in jquery.js and how to integrate this script into all of my web forms or alternatively int ...

Tips for properly styling Ajax Response Data?

I recently started working with Ajax and was able to successfully retrieve cross-domain data using a proxy. The output that I received is as follows: {"error":"\r\n\r\n\r\n\r\n\r\n\r\n\r&bso ...

Top option for managing an excess of pins on Google Maps

Let me walk you through my Google Maps setup process: To start, I retrieve the locations of all markers (usually under 300) from a database and send them to JavaScript in JSON format. Within JavaScript, I parse the JSON data, iterate through the marker a ...

Using jQuery to gently fade out text at the top and bottom of the page as you scroll

I am seeking a way to fade out the content of my page with an opacity/rgba color effect as it approaches a specific distance from both the top and bottom of the viewport. This is the desired outcome I want: In the example above, there is a gradient posit ...

Combining Multiple Collections in Meteor.js and MongoDB: A Comprehensive Guide

My inventory consists of various items. With an overwhelming number of products, I aim to categorize them for a more efficient organization of my publish/subscribe code (and subsequent updates to templates). Let's say I have 4 separate collections i ...

Encountering the "Not all code paths return a value" TypeScript error when attempting to manipulate a response before returning it, however, returning the response directly works without any issues

Encountering an issue where manipulating/process response and return triggers an error in TypeScript with the message "Not all code paths return a value.". Data is fetched from a backend API using RxJS lastValueFrom operator, along with lodash functions as ...

Clarifying the Usage of mockjaxClear in Asynchronous Tests with QUnit

Testing out my frontend code with qunit and mockjax. The way AJAX tests are structured in mockjax's test code is shown below (jsfiddle): var testURL = "/test/data", testData = { a: 1, b: "c" }; asyncTest("AJAX response test", 1, function() { ...

Hide multiple divs with similar ids in JQuery when clicking outside of them

Is there a way to hide all div elements with similar id if none of them are clicked on? Currently, my code only works for the first div because I am using index[0] to retrieve the id. How can I make it work for all ids? Below is the code snippet: $(win ...

Ways to incorporate ejs partials using JavaScript

I am currently developing code to determine if a user is logged in. Depending on the user's login status, the content of the "my user" section should vary. When a logged-in user navigates to the "my user" page, an if statement is executed to confirm ...

Error: The object prototype can only be an Object or null, not undefined

During development, my app runs smoothly. However, when I build the project for production using "npm run build," I encounter the following error: 2.9b72e8af.chunk.js:sourcemap:1 Uncaught TypeError: Object prototype may only be an Object or null: undefin ...

Tips for handling a multi-step form in React?

Below is the code snippet for the multistep form I have been working on: import clsx from 'clsx'; import React from 'react'; import PropTypes from 'prop-types'; import { makeStyles, withStyles } from '@material-ui/styles ...

Learn the technique in Vue to separate a string using commas and store the values in an array

As a Ruby enthusiast, I am delving into the world of Vue. In my project, users can input multiple styleCodes separated by commas. I aim to store these styleCodes in an array for flexible length calculations. See my code snippet below: <template> &l ...

Troubleshooting issues in Safari's Web Inspector while utilizing a module loader such as SystemJS

I am currently developing an Angular application using es6 modules, TypeScript, and SystemJS as the module loader. This is my current configuration: tsconfig.json: { "compilerOptions": { ... "target": "es5", "module": "system", ... } ...