Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call PHP script without changing page

Tags:

php

external

I am trying to make a website that deals with a database of students and tutors. My issue is that so far the only way I know of by which I can run a PHP script (that, for example, removes a user from the database) is putting the information in a link and letting the user click on that link. This does work, but it is annoying because it means that the user is constantly being redirected to new pages. I have also been using some $_SERVER['PHP_SELF']?other information, but this is bothersome because it builds up a long stack of the same page (ie trying to use the Back/Forward function of the browser brings you to the same page).

I would like to be able to generate links/buttons that, when clicked, pass information to a php script without redirecting/changing the page (except possibly refreshing the page to show the new result).

An example from my site, where the page generates a list of all the users and then respective links/buttons to remove each user:

//Gets the list of users and iterates through the data
while ($row = mysqli_fetch_array($data))

    {
        $fullname = $row['lastname'] . ", " . $row['firstname'];
        $username = $row['username'];
        $remove_link = "remove_user.php?username=$username";

        echo '
                  <tr>
                    <td>' . $fullname . '</td>
                    <td>' . $username . '</td>
                    <td> <a href="'. $remove_link . '">Remove this user.</a> </td>
                  </tr> 
        ';

    } 
echo '</table>';

When $remove_link is clicked, it loads a php script that removes the user and then redirects back the the original page (user_list.php). Is there a way to just call the remove_user.php script without redirecting/changing pages?

Thanks in advance.

like image 974
phil4 Avatar asked Feb 02 '26 15:02

phil4


2 Answers

The way websites work today is by using Ajax to dynamically load / change the content of a page.

The best way to use Ajax is to use jQuery's Ajax functions. There are many tutorials on the internet that will show you how to use jQuery.

If you do not want to use jQuery for your Ajax, you can use the old fashioned way of doing it. You can view a tutorial on it on w3schools.com.

Hope this helps!

like image 134
Brock B. Avatar answered Feb 04 '26 05:02

Brock B.


You need to start familiarizing yourself with javascript and AJAX. Probably look at using jQuery for your javascript framework as it maked this relatively simple and is the most popular such framework, with wide support.

like image 31
Mike Brant Avatar answered Feb 04 '26 04:02

Mike Brant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!