Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form class="form-inline" method="post" >
- <div id="div_id_value" class="form-group">
- <input class="numberinput form-control"
- id="value" name="value"
- placeholder="Value (mmol/L)"
- required="True" type="number" step="any" min="0"/>
- </div>
- <div id="div_id_category" class="form-group">
- <select id="id_category" name="category">
- <option value="Breakfast">Breakfast</option>
- <option value="Lunch" >Lunch</option>
- <option value="Dinner">Dinner</option>
- <option value="Snack">Snack</option>
- <option value="Bedtime">Bedtime</option>
- <option value="No Category" selected="selected">No Category</option>
- </select>
- </div>
- <input type="submit" name="submit" value="Quick Add" id="quick">
- </form>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
- $("#quick").click(function() {
- var sugar2 = $("#value").val();
- var category2 = $("#category").val();
- $.ajax({
- type:'POST',
- url:'quick.php',
- data:{ 'sugar': sugar2, 'category':category2},
- dataType:'json',
- success: function(output) {
- alert(output);
- };
- });
- });
- <?php
- session_start();
- include 'conn.php';
- if (isset($_POST['sugar'])) {
- $sugar = $_POST['sugar'];
- $category = $_POST['category'];
- $email= $_SESSION['email'];
- $query = "INSERT INTO data (email, sugar, category)
- VALUES($email, $sugar, $category )";
- if(mysqli_query($link, $query)) {
- header("Location: index.php");
- };
- };
- ?>
- $("#quick").submit(function(evt) { ... });
- //or
- $("#quick").on('submit', function(evt) { ... });
- <?php
- session_start();
- include 'conn.php';
- if (isset($_POST['sugar'])) {
- $sugar = $_POST['sugar'];
- $category = $_POST['category'];
- $email= $_SESSION['email'];
- $query = "INSERT INTO data (email, sugar, category)
- VALUES($email, $sugar, $category )";
- //save the output of the result
- $result = mysqli_query($link, $query);
- //according to the docs, mysqli_query() will return false on failure
- //check for false
- if( $result === false ) {
- //the query failed
- //provide a response to the AJAX success function
- echo "Query failed. Check the logs to understand why, or try enabling error reporting.";
- }else {
- //the query worked!
- echo 'All good!'; //provide a response to the AJAX success function
- }
- //Kill PHP. Good idea with an AJAX request.
- exit;
- }
- <?php
- echo "my response";
- ?>
Add Comment
Please, Sign In to add comment