Search code examples
phpexternal

Call PHP script without changing page


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.


Solution

  • 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!