Member Avatar for Member #710047

If I have a page of 50 lines and I want to run 10-20 lines again if i click on some button..

Is there any way to do this??

Dani AI

Generated

@Member710047 asked how to re-run only part of a PHP page. @Member585571’s idea to separate that block into a callable unit is on the right track. The important distinction: PHP runs on the server, so re-running PHP requires another HTTP request. Options that avoid repeating the whole page include: an HTML form POST (full reload), a link with a parameter (simple reload), or an asynchronous JavaScript request (AJAX/fetch) to a small endpoint that runs just the needed logic and returns an HTML fragment or JSON.

A simple, modern pattern is to make a dedicated endpoint that performs only the 10–20 lines and call it from the browser with fetch. Use POST for actions that change state, validate input server-side, and protect with a CSRF token. Return JSON or a safe HTML snippet and update the page DOM on success.

Example (client-side + endpoint skeleton):

/* JavaScript - trigger the server task without reloading */
document.getElementById('run-part').addEventListener('click', function () {
  fetch('run_part.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ id: 42, _csrf: window.csrfToken })
  })
  .then(r => r.json())
  .then(data => {
    if (data.success) document.querySelector('#result').innerHTML = data.html || '';
    else console.error(data.error);
  })
  .catch(console.error);
});
<?php
// run_part.php - minimal server-side pattern
session_start();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['success'=>false]); exit; }
$input = json_decode(file_get_contents('php://input'), true);
// validate CSRF and params here
// perform the isolated logic, capture output, then return JSON

Troubleshooting notes: check the Network tab for request/response, set correct Content-Type headers, avoid mixing HTML and JSON, and use POST+Redirect+GET if a full reload is used to prevent duplicate execution on refresh. If the isolated code depends on earlier page variables, pass them as parameters or store needed state in session or the database. This complements @Member585571’s server-side approach and gives a smooth, reload-free option when appropriate.

Recommended Answers

All 4 Replies

Member Avatar for Member #585571

You can create a function for the 10-20 lines.
Then, on the page print a link such that it has an extra HTTP GET attribute at the end.
Like if your URL is page.php then append ?e=1
Now in your PHP script, check for value of $_GET if it is 1, 2, etc. (use switch-case if there are a lot) and call the function you created.

Member Avatar for Member #710047

You can create a function for the 10-20 lines.
Then, on the page print a link such that it has an extra HTTP GET attribute at the end.
Like if your URL is page.php then append ?e=1
Now in your PHP script, check for value of $_GET if it is 1, 2, etc. (use switch-case if there are a lot) and call the function you created.

Sorry, but i have not understood the logic..
Can you explain..
And how to call function on click to some text or link

Member Avatar for Member #585571
function code_to_be_executed() {
// This is the code to be executed on a special action
// The 10-20 lines
}

// Now the general flow:

echo '<a href=page.php?e=1>....</a>';

if(isset($_GET['e']) and $_GET['e'] == 1) {
code_to_be_executed();
exit; // exit after our special code is executed.
}

// Some first ten lines
// Then call the function to execute the 10-20 lines we had
code_to_be_executed();
Member Avatar for Member #710047

Thanks for explaining..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.