1

Okay so I have an html form in Add.html. When I click submit, I would like the data to be added to my database via php and then return to the same form with "instance added" or "failed blah blah."

The only way I know how is to set the form action to a separate php file and call that - but then the php file renders and I do not return to the same form.

I would like to not have to add a "return to form" button and would prefer to return to the form on submit with a status message.

Any better ways to do this?

Dan
  • 10,614
  • 5
  • 24
  • 35
Kyle Brown
  • 303
  • 4
  • 14

5 Answers5

1

You can do a redirect in php, to the html form - and you can set a "flash message" - to show "instance added" by saving "instance added" to the session and showing that value when you redirect to html.

kingtut007
  • 89
  • 8
  • Can you explain, or link an explanation on how to redirect in php? I assume you mean set the form action to a separate file, run the db stuff, then redirect with flash message – Kyle Brown Dec 02 '15 at 20:14
  • are you using a php framework? Its easier on php frameworks. Read up on http://php.net/manual/en/function.header.php – kingtut007 Dec 02 '15 at 20:16
1

A very simple way to do is to do following :

yourpage.php

<?php
if(isset($_POST)){
  //data posted , save it to the database
  //display message etc
}

?>
<form method="post" action="yourpage.php" >....
thepiyush13
  • 1,321
  • 1
  • 8
  • 9
  • This breaks MVC pattern conventions. – kingtut007 Dec 02 '15 at 20:36
  • care to explain, please ? – thepiyush13 Dec 02 '15 at 20:37
  • Its not preferable to have template code alongside php code. The following script up there wont work first of all because you indicated that you are running it from an .html file (and not a php file) - so you cant basically parse php syntax in that file. Moreover, by mvc coding standards - it means Model View Controller - you should have the template (view) solely responsible for displaying data given to it from the controller and not mishmashing everything together. – kingtut007 Dec 02 '15 at 20:38
  • I agree but on the other hand I did not see OP describing that he is using a MVC framework like CI , in addition if you notice the file name is yourpage.php so it will work just fine, anyway good points, thanks – thepiyush13 Dec 02 '15 at 20:42
0

you can use this trick

<?php if (!isset $_POST['Nameofyourinput']){

?>

<form method="post" action="add.html">
  // your inputs here along with the rest of html
  
  
  
  </form>
<?php 

}

else
{
// Update you database and do your things here
//in your request variable you can add the error you want if things didn't go well, for example
$result = mysqli_query($connection, $sql) or die('Instance not added !'.$req.'<br>'.mysql_error());
// and then 
echo (" instance added")
};
0

The action attribute will default to the current URL. It is the most reliable and easiest way to say "submit the form to the same place it came from".

Just give nothing to the action attribute. It will refer to your current page.

<form method="post" action="">

Other way to do this are:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Or just add '#'

<from method="post" action="#">

To handle php code. Write your code inside it.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // write your code here.
}

You should change your file extension from .html to .php .

Hemant Parihar
  • 732
  • 1
  • 6
  • 19
0

Well you can employ old school AJAX. For instance,let's say we have a form that takes in a number N,and once we click the calculate button we should see the result the of 2^N displayed on the same page without the page being refreshed and the previous contents remaining in the same place. Here's the code

<html>

<head>


  <title> Simple Math Example</title>

  <script type="text/javascript">

     var request = new XMLHttpRequest();


     function createAjaxObject(){


           request.onreadystatechange = applyChange;
           request.open("POST","calculate.php",true);
           request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
           request.send("N="+document.getElementById('N').value);

     }

     function applyChange(){

        if(request.status == 200 && request.readyState == 4){

           document.getElementById('resultSpace').innerHTML = request.responseText;
        }


     }




  </script>


</head>

<body>

  <fieldset>         


        <legend>Enter N to get the value of 2<sup>N</sup> ::: </legend>

        <input type="text" name = "N" id = "N">
        <br>
        <input type="button" value = "Calculate" onclick="createAjaxObject()">



  </fieldset>   

  <div id="resultSpace">

  </div>
</body>

The file calculate.php is the same file with the above code. When the calculate button is clicked, it calls a function createAjaxObject which takes in a value N and sends the value to the same file via the POST method. Once the calculation is done, a response will be sent. And if the response is successful, it will be sent to a function called applyChange which will render it to the same page via JavaScript.