0

I have a little problem here.

The data inserts into the database, but it do insert when I open the page.

I want to insert the data when I push the submit button, not when the page is loaded/refreshed. Hope someone can help me!

Here is the code:

<html>
  <head>
  </head>

  <body>
    <h2>Arbeidsliste</h2>

    <div id ="insert">
      <form action="index.php" method="post">
        <p>
          <label for="dato">Dato: </label>
          <input type="date" name="dato" class="field">
        </p>
        <p>
          <label for="start">Start: </label>
          <input type="time" name="start" class="field">
        </p>
        <p>
          <label for="slutt">Slutt: </label>
          <input type="time" name="slutt" class="field">
        </p>
        <p>
          <label for="pause">Pause: </label>
          <input type="number" name="pause" class="field">
        </p>

        <p>
          <input type="submit">
        </p>
      </form>
    </div>
    <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "timer";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "INSERT INTO meny (dato, start, slutt, pause)
                VALUES ('2016-04-01', '12:00', '20:00', '30')";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $conn->close();
    ?>
  </body>
</html>
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
user3097182
  • 33
  • 1
  • 7

2 Answers2

2
if(isset($_POST['submit'])){ 

    // execute query

}

this based on an input of the same name attribute and a POST method for your form.

<input type="submit" name="submit" value="Submit">

Plus, if you're getting that from user input, you will need to check for empty() fields and use a prepared statement.

Edit: as per your edit.

Change <input type="submit"> to what I included up above.

Basic check for empty fields is:

if(!empty($_POST['dato']) && !empty($_POST['start'])){

    $dato = $_POST['dato'];
    $start = $_POST['start'];

   // execute the query

}

and apply that same logic for the other fields.

Oh, and just to be sure... I'll add some extra conditional here:

if(isset($_POST['submit'])){ 

    if(!empty($_POST['dato']) && !empty($_POST['start'])){

        $dato = $_POST['dato'];
        $start = $_POST['start'];

       // execute the query

    }

}

and you can add a header to redirect.

Read up on the function:

and make sure you're not outputting before header. If you do get a warning about it, consult the following on Stack:

Or, how to redirect in PHP:

You have another option and that is to use two separate files.

One for your HTML form and one for your PHP/MySQL, but you should still use a header to redirect after a successful query.

  • Tokens/sessions is another viable option.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • They want to submit the form without reloading the page. This doesn't fix that. – 4castle Apr 02 '16 at 21:57
  • @4castle `if(isset($_POST['submit'])){ // execute query }` what.. you want me to write out the code for them? Instructions are in there for them to execute the query. That won't fire anything up until they hit the submit button. The other part to check for empty fields is an additional conditional on top of the isset(). – Funk Forty Niner Apr 02 '16 at 21:58
  • @4castle OP: *"The data inserts into the database, but it do insert when I open the page. "* - That tells me that the query is firing on initial page load. Look at their code again. Everything is in the same page and there's no conditional statement for it. What am I not grasping? and OP: *"I want to insert the data when I push the submit button, not when the page is loaded/refreshed."* – Funk Forty Niner Apr 02 '16 at 21:59
  • 1
    @4castle As I read it he doesn't want to submit the form without reloading the page, he wants to reload the page without submitting the form. – Darwin von Corax Apr 02 '16 at 22:02
  • I suppose we interpreted the question differently. I was thinking along the lines of telling them to put the PHP in a separate file, and then using AJAX so that the page didn't have to be redirected. – 4castle Apr 02 '16 at 22:03
  • @4castle If the user disables JS, then that won't work. I for one and many others am sure, disable JS in their browser by default. I always opt for a server-side solution and if I were to use JS, then I would still keep server-side active. – Funk Forty Niner Apr 02 '16 at 22:06
  • Certainly, my answer doesn't remove the ability for it to work without JS, it only allows people with JS to not have to refresh the page. I'm removing my answer (for those curious what my answer was, it suggested using AJAX) – 4castle Apr 02 '16 at 22:10
  • @Fred-ii- You're right. How I interpret the question is that he would like to submit the form with a submit button. The reason why is, because his code will now exucute by itself because it has no check in the code or something like that. – Giesburts Apr 02 '16 at 22:47
0

a if (isset($_POST['submit'])) { your php code } will do the job if I interpret your question right.

<html>
  <head>
  </head>

  <body>
    <h2>Arbeidsliste</h2>

    <div id ="insert">
      <form action="index.php" method="post">
        <p>
          <label for="dato">Dato: </label>
          <input type="date" name="dato" class="field">
        </p>
        <p>
          <label for="start">Start: </label>
          <input type="time" name="start" class="field">
        </p>
        <p>
          <label for="slutt">Slutt: </label>
          <input type="time" name="slutt" class="field">
        </p>
        <p>
          <label for="pause">Pause: </label>
          <input type="number" name="pause" class="field">
        </p>

        <p>
          <input type="submit">
        </p>
      </form>
    </div>
    <?php

 if (isset($_POST['submit'])) {

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "timer";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "INSERT INTO meny (dato, start, slutt, pause)
                VALUES ('2016-04-01', '12:00', '20:00', '30')";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $conn->close();
}        
?>
  </body>
</html>
Giesburts
  • 6,879
  • 15
  • 48
  • 85