Home > Enterprise >  How can I make a single page application with php?
How can I make a single page application with php?

Time:08-14

I am trying to make a form on php for registration. There are multiple pages on the form. Right now, for the user to navigate between pages, I have created a next button that changes the header. The problem with this is that the loading speed is very slow and inefficient. Is there a way for me to make this into a single page application so that the html just has to load once?

Below is how I currently navigate between pages. Something similar to this is included on every single one of my pages.

<?php
if (isset($_POST["next"])){
  header("location: page2.php");}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
</head>
<input type="submit" name="next" value="Next">
</html>

CodePudding user response:

There are many ways to do this:

First, you can do this with many PHP frameworks (ex- Yii, CodeIgniter), but I think for your case if you don't want a big whole website you can just use ajax and jQuery, and with both jQuery and ajax, you can easily create the kind of form site where the whole page will not reload upon every submission.

But if you want the whole website, using a framework will be the best option, and you can try PHP with CodeIgniter and React to build Single Page Application (It's My personal choice, you can try any framework you want). You check out this article for it too.

CodePudding user response:

To write a single page application with PHP you have to use "$_SERVER['PHP_SELF']" in your form tag.

If the "Next-Button" is clicked you stay on the same page and you can define whatever you want what happens next.

In the example below "hello" is printed:

<?php
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
</head>
<?php
print "<body>";
print "<FORM  action='{$_SERVER['PHP_SELF']}'  method='POST' enctype='multipart/form-data'>";

print "<input type='submit' name='next' value='Next'>";
if (isset($_POST["next"]))
{
  print "hello";
  }
print "</form>";
print "</body>";
print "</html>";
?>
  • Related